Review-python-note2
Note 2 - body and structure
标签(空格分隔): python
1. Control statement
1.1 Condition statement Definition
#syntax
#boolean expression :a cimbination of comparsion operators and logic operators
if boolean expression :
elif boolean expression :
else :
Example:
# if
age = 20
if age < 18 :
print("youngster")
elif age == 18 :
print("18")
else:
print("adult")
1.1.1 deeper intuition
If statement exactly follows the sementic rules as if these statements looks like a short story!
if bool(true)->keep going
bool(false)->else / elif(iteration)
Extra point: input()
a = input("please input your age!")
The terminal prompts you to input age-> that means you need to type your age on the console.
1.1.2 nested if statement
Inner one if statement is nested into the outer if statement
if boolean expression :
if boolean expression :
#....
1.2 loop statement
- while
- for
keywords:
- continue
- break
- else
- pass
1.2.1 while
Example:
i = 1
while i <= 10 :
print("still in loop body",i)
i += 1
if i == 10 :
break
else :
print("jump out of loop then execute else")
We can single out some important points:
1.syntax
while boolean-expression :
loop body
continue # stop this sub-course and jump directly to next sub-course
break # terminate the whole loop and execute the next statement
else:
##else executed if the entire lopp is executed but jump over to the underneath statement if loop ##body executes break
1.2.2 for
Example:
for element in collection :
continue # same as while
break # same as while
else:
# same as while when break within loop body is executed makes else block cannot be executed
When I first learned to program with python in the way as I do in Java,especially retrieving the entire list in python.I hardly got used to code for statement in python.But a suggestion is given for this problem.
for i in range(10)[
# equivalent to ->
# for (int i = 0 ; i < a.length ; i++ )
1.2.3 pass
As result of facilitating the complement for code in the future,we can use keyword pass to occupy some space to be available later.
2.Iterator and generator(detailing it later!!)
what we learn here is just getting our feet wet.
2.1 Iterator
Iterator is for retrieving the whole element in a collection like list and so forth.
There are two crucial functions of it: iter() [exclusive]and next()[general]
a = [1,2,3,4,5,6,7,8,9,10]
iterator = iter(a)
while True :
print(next(iterator),end=",")
The terminal shows:
1,2,3,4,5,6,7,8,9,10,Traceback (most recent call last):
File "d:\workspace\python\learning_06.py", line 6, in <module>
print(next(iterator),end=",")
StopIteration
2.2 generator
pass
3.function
Back to process-oriented programming language,so many lines of code sometimes achieves one goal.Function can save it and is recalled with one line of code as well as convinent maintenance.
3.1 syntax
def functionName(parameter 1,parameter 2,...) :
... # function body
...
...
# anonymous function
x = lambda [formal args,]arg,.. : expression
#call a function
functionName(p1,p2,...)
3.2 parameter
1.parameter types when a function is called
- mandatory parameter
- keyword parameter
- default parameter
- varied length parameter
## parameter genre
def o(arg1,arg2=1086,*args3,**args4) :
print(arg1,end="\n----------------------\n") #mandatory parameter
print(arg2,end="\n----------------------\n") #default parameter
print(args3,end="\n----------------------\n") # * ->tuple
print(args4,end="\n----------------------\n") # **->dict
o(1,2,3,4,5,a=1,b=2)
The terminal shows:
1
----------------------
2
----------------------
(3, 4, 5)
----------------------
{'a': 1, 'b': 2}
----------------------
Notes:
| declare a function |
|---|
| *args (with parameter name)occurs once only |
| *(without parameter name)can accept ** parameter : unuseful! |
| call a function |
|---|
| rule 1:match all the positional parameters then take varied length parameters into account????? |
| rule 2 : parameter order-> formal ,varied length ,keyword |
2.parameter passing
- value passing
- reference passing
My guess : The difference between the two types reminds me of the issue about "is"and"==".The result is still true when "is" compares two diverse variables but with same numeber.
->Delve into it with a example:
# parameter passing
def output(a,b) :
a = 1
b[2] = 0
print("inner function :",a,"id->a is",id(a),end=" | ")
print("inner function :",b,"id->b is",id(b))
a = 0
b= [0,1,2]
print("outer function :",a,"id->a is",id(a),end=" | ")
print("outer function :",b,"id->b is",id(b))
output(a,b)
The terminal show:
outer function : 0 id->a is 2269960143056 | outer function : [0, 1, 2] id->b is 2269961535232
inner function : 1 id->a is 2269960143088 | inner function : [0, 1, 0] id->b is 2269961535232
value passing : pass only value inside function
reference passing : pass reference! inside function
range([start],end,[step]) generates a number list :start from [start],end up to end,go forward one timee with one step ↩︎

浙公网安备 33010602011771号