Sunteți pe pagina 1din 3

Week 1:

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

Syntax: the rules that describe valid combinations of Python symbols


Semantics: the meaning of a combination of Python symbols

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.

Call: ask Python to evaluate a function


Argument: a value given a function
Pass: to provide to a function
Function call: function_name(parameter1, parameter2)

dir(_builtins_):
type, min (needs more than 1 element), max, abs, pow, help

help(abs): help on built-in function abs in module builtins


abs(x, /)
Return the absolute value of the argument.
(Required argument not found: expected not found)
(Takes at most 2 arguments)
Python rounds to the nearest even on 1.5

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

EQUAL VALUES SHARE THE SAME MEMORY ADDRESS!!!


Values live at a particular memory address
The variable stores the memory address
For expressions: must have operations (3*k not 3k)
Variable names:
Must start with letter or underscore
Can include letters, digits, and underscores but nothing else
Case matters (age is not the same as aGe)
Python convention: pothole_case
CamelCase is sometimes seen but not for function and variable names
Single-letter names are rarely capitalized
When in doubt, use lowercase_pothole
Choose names that will be meaningful to the individuals who read our code; programs may be
used, read, and improved on for years

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

Save all files as .py


Run the module by clicking on the play icon, making the function saved in the file available in
the shell (before executing a function in the shell, the module needs to be run)

S-ar putea să vă placă și