if
Why should there be if the judge?
Is to determine right and wrong and true and false things, computers also need to judge things right or wrong, true and false, in order to respond differently.
Block:
python use indention home code, the same code is called a code block indentation.
Analyzing Boolean value can be directly used, it can also be used to determine the variable names, variable names as the corresponding value is True or False
There boolean value False Data: None, 0, '', [], {}
The first, if, else:
if Condition: Code 1 Code 2 ... the else : Code 1 Code 2 ...
Usage: If that is how and how, or how about how other cases.
Second, if, elif, else:
if Condition: Code 1 Code 2 ... elif conditions: Code 1 Code 2 ... the else : Code 1 Code 2 ...
Usage: If that is how and how, and if how how, or how about how other cases, elif can appear multiple times.
Note: else not be used alone, and must if, while, for with the use, if, elif, else the same level when used in conjunction, can only run a block of code.
Third, if can be nested:
if Condition: Code 1 Code 2 if Condition: Code 1 Code 2 ... elif conditions: Code 1 Code 2 ... the else : Code 1 Code 2 ... elif conditions: Code 1 Code 2 ... the else : Code 1 Code 2 ...
while
Generally need to use with the keywords:
break: layer immediately ends the present cycle (that while it belongs only active)
continue: out of this cycle, the next cycle started directly.
while + else:
Only when conditions while according to the normal end of the code will run else, if it is active by the end of the break, it will not run else's.
. 1 = n- the while n-<. 5 : IF n-<. 5 : Print (n-) n- + =. 1 the else : Print ( ' the while loop normal end ' )
while + break:
. 1 = n- the while n-<. 5 : IF n-==. 3 : BREAK Print (n-) n- + =. 1 the else : Print ( ' the while loop normal end ' )
while + continue:
. 1 = n- the while n-<. 5 : IF n-==. 3 : n- + =. 1 Continue Print (n-) n- + =. 1 the else : Print ( ' the while loop normal end ' )
nested while loop:
the while True: Code 1 Code 2 ... IF Condition: Code 1 ... the while True: Code 1 ... IF Condition: Code 1 ... BREAK Code 1 ... BREAK
for
Is defined: for loop does not depend on the index value, for a fixed loop syntax structure:
for the variable name in the type of container (can be lists, dictionaries, etc.):
Code 1
Code 2
..
Dictionary for being the value of the cycle time, the dictionary will only return key, value will not take the initiative to return, we need to get a way.
For example, the code:
d = {'name':'jason','password':'123','hobby':[1,2,3,4]} for i in d: print(d[i])
len (): Get the number of data types (types of container), the string is a special case, is the number of acquired characters in the string, can not obtain the number of digits.
How to print cycle for 1 ~ 10 (or even 1 to 1000000)?
The value required for loop type a container, this time not, so use a keyword:
range: represents the range of meaning.
Features: care regardless tail, range (1,10) only removed 1 to 9.
range python2 difference in the python3
1.python2 in
1.range is actually a list
2.xrange is equivalent to the range python3
2.python3 in
range is equivalent to an old sow, is an iterator object when you need values, it will give us value.
for + break:
While the same effect break, layer immediately ends the present cycle.
for + continue:
While the same effect continue, out of this cycle, the next cycle started directly.
for + else:
With while + else, only when the basis for the conditions of normal operation until the end of the else, if it is active by the end of the break, will not run else's.
= NAME_LIST [ ' Nick ' , ' Jason ' , ' Tank ' , ' Sean ' ] for name in NAME_LIST: IF name == ' Jason ' : BREAK Print (name) the else : Print ( ' for the end of a normal cycle ' )
nested for loop
Print 9 * 9 multiplication tables
for i in range(1,10): for j in range(1,i+1): # 内层循环的range条件是根据外层循环决定的 print('%s*%s=%s'%(i,j,i*j),end = ' ') # end = ' '打印在同一行 print() # print自带换行