Python conditional branches and loop statements

        There is no {} writing method in python. Generally, indentation is used to determine the code blocks that need to be executed for branches and loops.

if 需要判断的条件表达式:
    条件成立时的动作
elif 需要判断的条件表达式:
    条件成立时的动作
else:
    动作

for 变量 in 迭代对象:
    动作

Example:

while 退出条件:
    动作

Loop using else statement

        In python, for...else means: there is no difference between the statements in for and ordinary ones. The else statement will be executed after the loop is executed normally (that is, for is not interrupted by break), and the same is true for while...else. .

break, continue and pass keywords

        break and continue in python have the same function as in C language.

        break is to exit the loop, continue is to exit the current loop (without executing the code under continue below) and proceed to the next loop.

        The pass statement is an empty statement, used as a placeholder statement and does nothing. Before python2.x, the pass statement could be used in an empty function. Directly defining an empty function would result in an error. After python3.x, you can directly define empty functions.

enumerate() function

enumerate(sqe, i)

参数:
    seq:可迭代对象或迭代器
    i:下标起始位置
返回值:
    enumerate类型的对象,也称为枚举对象
  • Traverse the collection and obtain the index and the value corresponding to the index

  • Modifying the starting index does not modify the collection

  •  Return value and traversal principle

         We can see that the return value of the enumerate function is an enumerate (enumeration) object. Convert it into a list, each value is a tuple, the first element of the tuple is the index, and the second element is the value. So it can be traversed through a loop. Get the index and value.

Guess you like

Origin blog.csdn.net/weixin_57023347/article/details/132191904