Python3 condition Control 1, if statement <1> general form
The general form of the IF statement in Python is as follows:
If condition_1: statement_block_1elif condition_2: statement_block_2else: statement_block_3
- If "condition_1" is True, the "statement_block_1" block statement is executed
- If "condition_1" is false, the "condition_2" will be judged
- If "Condition_2" is True, the "statement_block_2" block statement is executed
- If "Condition_2" is false, the "STATEMENT_BLOCK_3" block statement is executed
used in Python elif instead of else if , so the keyword for the IF statement is: If–elif–else .
Attention:
1. Use a colon (:) after each condition to indicate the next block of statements to be executed after the condition is met.
2, using indentation to divide the statement block, the same indentation number of statements together to form a block of statements.
3. There are no switch–case statements in Python.
<2> Operators
The following are the operations operators commonly used in the IF:
operator |
Description |
< |
Less than |
<= |
Less than or equal to |
> |
Greater than |
>= |
Greater than or equal to |
== |
Equals, compares objects for equality |
!= |
Not equal to |
<3> nesting
In a nested if statement, the IF...ELIF...ELSE structure can be placed in another if...elif...else structure.
If expression 1: statement if Expression 2: statement elif Expression 3: statement- else: statement elif expression 4: statement else: statement
2. While loop <1> general form
The general form of a while statement in Python:
While judgment condition: statement
Also be aware of colons and indents. In addition, there is no do in Python. While loop.
As with C, we can implement an infinite loop by setting conditional expressions that are never false.
<2> Else statement
In the while ... else statement block that executes else when the conditional statement is false:
#!/usr/bin/python3 count = 0while Count < 5: print (count, "less than 5") count = Count + 1else: print (count, " Greater than or equal to 5 ")
<3> Simple Statement Group
Similar to the IF statement syntax, if there is only one statement in the while loop body, you can write the statement in the same row as the while, as follows:
#!/usr/bin/python flag = 1 while (flag): Print (' Sample while! ') print ("Good bye!")
3. For statement
A Python for loop can traverse any sequence of items, such as a list or a string.
<1> General Form
The general format for the For loop is as follows:
For <variable> in <sequence>: <statements>else: <statements>
Instance:
>>>languages = [' C ', ' C + ', ' Perl ', ' Python '] >>> for x in languages: ... Print (x) ... Cc++perlpython>>>
<2> Range () function
If you need to traverse a sequence of numbers, you can use the built-in range () function. It generates a sequence of numbers, such as:
>>>for I in range (5): print (i) 01234
You can also use range to specify the value of the interval:
>>>for i in range (5,9): print (i) 5678>>>
You can also make range start with a specified number and specify a different increment (even negative, sometimes referred to as ' step '):
>>>for i in range (0, 3): print (i) 0369>>>>>>for I in range ( -10, -100, -30): Print (i) -10-40-70>>>
You can also use the range () function to create a list:
>>>list (Range (5)) [0, 1, 2, 3, 4]>>>
You can combine the range () and Len () functions to iterate through the index of a sequence, as follows:
>>>a = [' Google ', ' Baidu ', ' Taobao ', ' QQ ']>>> for I in range (Len (a)): ... Print (I, a[i]) ... 0 Google1 Baidu2 Taobao3 qq>>>
4. Break and continue statements and ELSE clauses in loops
The break statement can jump out of a for and while loop body. If you terminate from a for or while loop, any corresponding loop else blocks will not execute.
The continue statement is used to tell Python to skip the remaining statements in the current loop block and proceed to the next round of loops.
Unlike C, a Python loop statement can have an ELSE clause that is executed when the loop terminates when the exhaustion list (for loop) or the condition becomes false (in a while loop), but the loop is not executed when the break terminates.
5. Pass Statement
The Python Pass is an empty statement in order to maintain the integrity of the program structure.
Pass does not do anything, generally used as a placeholder statement.
Python Learning Notes: Day sixth (flow control statement)