if the statement is true or false to determine the conditions is satisfied, if the implementation is ture, then skip to flase
1.python use indention attribution code
2. The same indent code, the code block is called, the default indentation 4
if sentence structure
if conditional statement
Block 1
Block 2
Block 3
EL SE: # can not be used alone, must and if, while, for use with
Block 1
Block 2
Block 3
elif:
Block 1 if the else elif same level with the use of time, will point to a block of code (it will not go away if elif else,
It will not go away elif else, if not go see, go explain if elif else did not go, do not meet the conditions if elif
Block 2
Block 3
else:
Block 1
Block 2
Block 3
while syntax: statements used to determine the number of cycles
while condition:
Code 1
Code 2
Code 3
Code 4
Code 5
break: an immediate end to this cycle layer (which it belongs only effective for a while)
the Continue: out of this cycle, the next cycle started directly
while can also be nested
while + else
only when the normal end of the while loop in accordance with the conditions will go else the code
if the initiative is the end of the break, it will not go else
Small Exercise: simulate a user logs have three chances over whether the user is prompted to continue
= user_name_bd ' Kevin ' password_bd = 12345 c = 0 the while True: IF c == 3 : Choice = the INPUT ( ' three chances had run out, you still wish to try (the Y-/ N) >>:? ' ) IF Choice = = ' the Y ' : C = 0 the else : BREAK user_name_bd = INPUT ( ' Please INPUT >> your name: ' ) password_bd = INPUT ( ' Please your password_bd >> INPUT: ') IF user_name_bd == ' Kevin ' and password_bd == 12345 : Print ( ' successful login ' ) BREAK the else : C + =. 1
for loop: for circulating removed from the container ships (in list or dictionary dict) corresponding to the value of
for i in [21,23,43,45,56,89] for i in ['name':'jason','age':18,'habby':'weite'] 只取kye
len () # Get the number of data types (types of container) of the acquired character string is a special case is the number of characters in the string
for loop syntax structure
for the variable name in the type of container:
Code 1,
Code 2,
Code 3,
Code 4,
Print Pyramid:
max_level = 5 for current_level in range(1,max_level+1): # 打印空格 for i in range(max_level-current_level): print(' ',end='') # 打印信号 for j in range(2*current_level-1): print('*',end='') print()
for循环也是可以嵌套和if while else 使用
range在python2与python3中的区别 (面试常考)
python2中:
1.range其实就是一个列表
2.xrange其实就是你python3中的range
python3中:
range就是一种迭代,不占具多余的内存空间,需要时才会取出相对应的值