introduction to python 3
prompt
(>>>) indicates that the interpreter is ready to accept code
High-level Languages
-Created in the 1960s and 1970s -Support programming using formulas or algorithms
Python
-Created in the late 1980s by Guido van Rossum -Derived from an existing programming language called ABC -Python's goals include simplicity and readability, while providing power and flexibility -Python 1.0 was released in 1994 with support for some functional programming constructs derived from Lisp. -Python 2.0 was released in 2000 and introduced automatic memory management and features form Haskell and other programming languages -Python 2.7 is the most widely used version. 2.7 program cannot run on Python 3.0 or later interpreters. 2.7 has an End of Life date set for 2020. -Python 3.0 was released in 2008 to rectify language design issues. - Python 3.0 is not backwards compatible, but python 3.x versions are becoming more widely used as new projects adopt the version. -Open-source language, meaning the community of users participate in defining the language and creating new interpreters, and is supported by a large community of programmers. -The 2nd most used language behind C++
Machine Instructions
-Instructions represented as 0s and 1s. -In the 1940s, programmers originally wrote each instruction using 0s and 1s -0s and 1s are hard to comprehend. Most programmers specify the program's functionality in a high-level language, which is then automatically translated to low-level instructions.
Assemblers
-Programs used to automatically translate human readable instructions into machine instructions. -Helped programmers write more complex programs.
Disk
-Stores files and other data, such as program files, songs, movies, office documents. -Nonvolatile: contents are maintained even when powered off by orienting magnetic particles in a 0 or 1 position.
Compilers
-Support high-level languages. -Programs that automatically translate high-level language programs into executable programs. -Ensures a program is valid according to the language's rules, performs optimizations, then converts to the low-level instructions
print()
-displays variable or expression values -the primary way to print output EX: print('hello world')
Byte
8 bits Common size involve megabytes, gigabytes, and terabytes.
Memory
A circuit that can store 0s and 1s in each of a series of thousands of addressed locations.
Clock
A processor's instructions execute at a rate governed by the processor's clock The clock ticks at a specific frequency Processors have clocks that tick at rates such as 1 MHz
python interpreter
A program that accepts and then executes Python source code.
Cache
A small amount of RAM on its own chip. Accessible in one clock tick Maintains a copy of the most-used instruction/data
Newline
A special character that causes the cursor to move to the beginning of the next line.
Syntax Errors
A violation to a programming language's rules on how symbols can be combined to create a program, such as putting multiple prints on the same line.
What is the purpose of variables? a.Store values for later use b. Instruct the processor to execute an action c. Automatically color text in the editor
A. Variables are used to refer to values saved in memory by the interpreter
Keyboard
Allows a user to provide input to the computer, typically accompanied by a mouse for graphical displays. Keyboards and mice are increasingly becoming replaced by touchscreens.
Operating System
Allows a user to run other programs and interfaces with many other peripherals.
Crash
An abrupt and unintended termination of a program
ValueError
An invalid value is used - can occur if giving letters to int(). int("Thursday")
TypeError
An operation uses incorrect types - can occur if adding an integer to a string. lyric = 99 + " bottles of pop on the wall"
How are most Python programs developed? a.Writing code in the interactive interpreter b.Writing code in files
B. The Python interpreter can load and execute Python code saved in files.
Bits
Binary digits a positive voltage is a 1 a negative voltage is a 0
CPU
Central Processing Unit
Processor
Circuits that process a list of desired calculations.
Switch
Controls whether or not electricity flows through a wire.
Screen
Displays items to a user
print('Woof!") Error No Error
Error. Mismatched quotes
print("Dogs:"num_dogs) error no error
Error. Missing a comma
print(Woof!) Error No Error
Error. Needs quotes
print(num_dogs). error no error
Error. No period
Switches have gotten larger over the years True False
False
Python was first implemented in 1960 True False
False Python was conceived in the late 1980s and first implemented in the early 1990s
Comments are required in a program. True False
False. Although ignored, comments are a helpful way to explain code to a reader of the program.
A major drawback of Python is that Python code is more difficult to read than code in most other programming languages. True False
False. Python was originally designed with readability in mind. Some of the maxims of Python include "Simple is better than complex" and "Readability Counts".
Python is a high-level language that excels at creating exceptionally fast-executing programs True False
False. Python excels at providing powerful programming paradigms such as object-oriented programming and dynamic typing. Python incur additional overhead costs because the language is interpreted.
Experienced programmers write an entire program before running and testing the code. True False
False. Though experienced programmers may write more lines of code before testing, most programmers still run and test their syntax frequently.
2) What is the output of print(pet_name, 'the', pet, 'is', age)
Gerald the dog is 22
Assembly Language
Human-readable processor instructions.
Other Types of Input Devices
Microphones, speakers, printers, USB interfaces
print("Hello+friend!") Error No Error
No Error. + in a string literal is treated like any other character.
Newline Character
Output can be moved to the next line by printing \n. example: print('1\n2\n3')
SyntaxError
Python doesn't understand the instruction because you typed it incorrectly. print('Today is Monday")
RAM
Random-access memory Temporarily hold data read from storage and is designed so that any address can be accessed much faster than a disk. RAM is volatile and loses it contents when powered off.
Processor
Runs the computer's programs by reading and executing instructions from memory, performing operations, and reading/writing data from/to memory. The processor starts executing the program whose first instruction is typically at memory location 0. Commonly called the BIOS (basic input/output system). Which sets up the computer's basic peripherals.
Transistors
Smaller switches, which were integrated onto a single chip called an integrated circuit or IC.
Executable Program
Sometimes just called an executable, are a sequence of machine instructions 4
String Literal
Text enclosed in quotes. Text in string literals may have letters,number, spaces, or symbols like @ or #)
Moore's Law
The doubling of IC capacity roughly every 18 months.
IndentationError
The lines of the program are not properly indented. print("Friday, Friday")
NameError
The program tries to use a variable that does not exist. day_of_the_week=Friday
1) A bit can only have the value of 0 or 1. True False
True
A memory stores bits True False
True
A processor executes instructions such as Add 200, #9, 201, represented as 0s and 1s True False
True
The computer inside a modern smartphone would have been huge 30 years ago True False
True
The code 20*40 is an expression True False
True Expressions are code that yield a value when evaluated, such as 20*40, which yeilds the value 800.
Assume variable age = 22, pet = "dog", and pet_name = "Gerald". 1) What is the output of print('You are', age, 'years old.')
You are 22 years old.
code
a common word for the textual represetation of a program
statement
a program instruction. a program mostly consists of a series of statements, and each statement usually appears on its own line.
process
a program performs computations on the data, such as adding two values like x+y
output
a program puts that data somewhere, such as to a file, screen, or network
input
a program receives data, from a file, touchscreen, network,etc
interactive interpreter
a program that allows the user to execute one line of code at a time
script
a program whose instructions are executed by another program called an interpreter
program/application/app
a programmer-created sequence of instructions.
line
a row of text
algorithm
a sequence of instructions that solves a problem
Given the variable num_cars = 9, which statement prints 9? a.print(num_cars) b.print("num_cars")
a. print(num_cars)
Which instruction completes the program to compute the average of three numbers? x = Get next input y = Get next input z = Get next input _____ Put a to output
a= (x+y+z)/3
Which pair of statements print output on the same line? a. print('Halt!') print('No access!') b. print('Halt!',end=' ') print('No access!') c.print(Halt!,end=' ') print(No Access!,end=' ')
b. print('Halt!',end=' ') print('No access!')
Which statement reads a user-entered string into variable num_cars? a. num_cars= "input()" b. input()=num_cars c. num_cars=input()
c. num_cars=input()
Select the statement that prints the following: Welcome! a.print(Welcome!) b.print('Welcome!") c.print('Welcome')
c.print('Welcome')
Expressions
code that return a value when evaluated
program
consists of a series of related instructions, organized for a common purpose, that tells the computer what tasks to perform and how to perform them
assignment
creates a new variable such as salary= wage*hours*weeks
computational thinking
creating a sequence of instructions to solve a problem, will become increasingly important for work, and even everyday life.
comments
denoted by # optional can be used to explain portions of code to a human reader
Scripting language
execute programs without the need for compilation
Python comes pre-installed on Windows machines. true false
false python can be downloaded and installed at python.org
Insert the correct number in the code below to print a monthly salary. Then run the program. The monthly salary should be 3333.333... .
hourly_wage = 20 print('Annual salary is: ') print(hourly_wage * 40 * 50) print() print('Monthly salary is: ') print(((hourly_wage * 40 * 50) / 1)) print() # FIXME: The above is wrong. Change # the 1 so that the statement # outputs monthly salary. Annual salary is: 40000 Monthly salary is 40000.0
operating system
manages programs and interfaces with peripherals
Which instruction completes the program to compute a triangle's area? base = Get next input height = Get next input Assign x with base * height _____ Put x to output
multiply x by 1/2
Type a statement that reads a user-entered string into variable my_var without a prompt.
my_var=input()
Type a statement that converts the string '15' to an integer and assigns the result to my_var.
my_var=int('15')
variables
named references to values stored by the interpreter.
Complete the code so that new_var is equal to the entered number plus 5. my_var = int(input()) new_var =
new_var=5+my_var
disk
non-volatile storage with slower access
Write the simplest statement that prints the following on a single line: 3 2 1 Go!
print('3 2 1 Go!')
Type a statement that prints the following: Hello
print('Hello')
Write a statement that prints the value of the variable num_people.
print(num_people)
Logic Error
program runs but does the wrong thing because the program is logically flawed. A logic error is often called a bug.
Runtime Error
program's syntax is correct but the program attempts an impossible operation, such as dividing by zero or multiplying strings together (like 'Hello' * 'ABC').
clock
rate at which a processor executes instructions
cache`
relatively small, volatime storage with fastest access located on processor chip
Whitespace
spaces, tab characters, and new line characters
moore's law
the doubling of IC capacity roughly every 18 months.
Python code can be written in a simple text editor, such as Notepad (Windows). true false
true. special python ides are not required
variables
used to refer to data
ram
volatile storage with faster access usually located off processor chip
integrated development environment
where code development is usually done. Also called an IDE.