Beruflich Dokumente
Kultur Dokumente
9/11/2017
Python can do math: arithmetic
Be aware of ROUNDING ERRORS!
int vs. float (floating point number: something after the decimal, an approximation to a real
number)
12//4 (integer division) vs. 12/4
(11/3 = 3)
% = remainder operator or the mod operator
10.5 % 3 = 1.5 (returns remainder)
Order of precedence:
** exponentiation
- negation
* / // % work left to right
+ - work left to right
Use parentheses to override this and to also make equations clearer
Computer memory: long list of storage locations each of which has a memory address that
starts with x
Variable: a named location in computer memory (keeps track of values)
Cant pick memory addresses
A value has a memory address
Value 8.5 has memory address x34.
A variable contains a memory address
Variable shoe_size contains memory address x34.
A variable refers to a value
shoe_size refers to value 8.5.
A variable points to a value
shoe_size points to value 8.5.
dir(_builtins_):
type, min (needs more than 1 element), max, abs, pow, help
9/13/2017
Python is designed by human beings who made decisions on how the program will work
Assignment statement form:
<<variable>> = <<expression>>
How its executed: evaluate the the expression on the right hand side to produce a value; this
value has a memory address
Store that memory address in the variable on the left hand side; create a new variable if it
doesnt exist; otherwise just reuse the existing variable
For x = 7
We say
x gets 7
x refers to the value 7
x contains the memory address id1
Memory address id1 is stored in variable x
id(x) to determine variable address
f(x) = x^2
In python:
def f(x):
Return x**2
- def: a keyword indicating a function definition
- f is the name of the function
- parameter: a variable (in this case x) that appears between the parentheses of a function
definition; parameters get their values from expressions in a function call
- : indicates what the function will do when called
- return means passing back a value
- Parameter: variable used in the function
- Argument: actual value
Function definition
def functionname(parameters):
body (always indented, typically 4 spaces)
Function calls are expressions
Rules for executing a function call:
1. Evaluate the arguments to produce memory addresses
2. Store those memory addresses in the corresponding parameters
3. Execute the body of the function
Return statement form
return <<expression>>
Executing a return statement
1. Evaluate the expression, which produces a value
2. Exit the function and produce that value as the result of the function call