two kinds of elements:what and how
data:what--the stuff to manipulate
function:how--describe the rules for manipulating the data
Expreesions:display/return the value of the expressions
Interactive python interpreter automatically displayed the value of the expressions you type in.
1.Primitive Expressions:
(1)number/name:
>>> 4
4
>>> from operator import add
>>> add
<built-in function add>
(2)arithematic: operands and operators are all expressions.
infix notation / operators:+ - * ** / // %
operands:3 2
>>> 3+2
5
>>> 6*2
12
>>> 3**6
729
>>> 5//2
2
(3)字符串:
>>> 'hello world!'
'hello world!'
>>> print('hello world!')
hello world!
2.Call Expressions:apply the function to arguments,之间用逗号分隔符分开
>>> from operator import add
>>> add
<built-in function add>
>>> max(98,2,7.3,4.3) #max:opertor 98:operand whole:function
98 #return value
>>> pow(2,3)
8
>>> abs(-9)
9
>>> add(9,20)
29
Evaluating Nested Expressions:
(1)evaluate the operand and operator subexpressions
(2)apply the function that is value of the operator to the arguments that are the value of the operands.
operators and operands are all expreesions,so they all evaluate to values.
Expression Tree:In computer science, trees grow from the top down.
Functions: nested/built in or from module import ...or user-defined
(1)Pure Functions:return values
(2)Non-pure Functions:return None and have side effects.
①None indicates that nothing is returned.
②One function does not explicitly return the value will return None.
③None will not get displayed by the interpreter as the value of the expressions.
Statements:not evaluate(return a value or evaluate a function on some arguments) but execute
1.assignment statements:
(1)bind the name to the values. ex.x=5
#evaluate all expressions to the right of = from thel left to the right
#bind all names to the left of = to those resulting values in the current frame
>>> a=1
>>> b=2
>>> b,a=a+b,b
>>> a
2
>>> b
3
>>> radius=20
>>> 2*radius
40
>>> radius
20
>>> area,circ=pi*radius*radius,2*pi*radius
>>> area #evaluate the expression to get a single value and get bound to area
1256.6370614359173
>>> circ
125.66370614359172
>>> radius=10
>>> area
1256.6370614359173
#Updating the value of area requires another assignment statement.
>>> x, y = 3, 4.5
>>> y, x = x, y
>>> x
4.5
>>> y
3
(2)via import statements. ex.from operator import add,mul
①:operator 模块
>>> from operator import truediv,floordiv,add,sub,mul,mod
>>> truediv(5,2)
2.5
>>> floordiv(9,2)
4
>>> mod(8,3)
2
②:math 模块
>>> from math import pi,pow,sqrt
>>> pow(2,8)
256.0
>>> sqrt(5)
2.23606797749979
(3)bind the name to functions. ex.max(1,2,3) name max is bound to the max function
>>> f=min
>>> f=max #f is bound to max function
>>> g,h=min,max
>>> max=g
>>> print(f(2,g(h(1,5),3)),4)
3 4
>>> max(1,2,3)
3
>>> f=max #f is bound to max function
>>> f
<built-in function max> #identifying description
>>> f(1,2,3)
3
>>> max=7
#max(built-in names) is bound to 7(new values),no longer bound to any previous value/func
>>> f(1,2,max)
7
>>> max
7
>>> max=f #max is bound to f
>>> max(1,2,3)
3
>>> f=3
>>> f(1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
2.Def statement:bind name to expressions
>>> def square(x):
... return mul(x,x)
...
>>> square(4)
16
>>> def sums_squares(x,y):
... return square(x)+square(y)
...
>>> sums_squares(3,4)
25
Current environmnt: a sequence of frames
Global frame alone or
Local frame followed by the global frame
the parameter names of a function must remain local to the body of the function
If the parameters were not local to the bodies of their respective functions, then the parameter x in square could be confused with the parameter x in sum_squares.