Python conditional Statements:
The judgment condition of the IF statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal).
If it is judged that multiple conditions need to be judged simultaneously, the or (or) can be used to indicate that two conditions are successful when a condition is established, and when using and (with), the condition is only successful if only two conditions are true.
1. If statement is used for control program execution:
If condition:
code block
Else
code block
Example: if basic usage
#!/usr/bin/python
#-*-Coding:utf-8-*-
Flag = False
name = ' Luren '
If name = = ' Python ': # judgment variable no ' python '
Flag = true # Set flag to True when condition is established
print ' Welcome boss ' # and output welcome message
Else
Output variable name when print name # condition is not valid
The output is:
>>> luren# Output Results
Example: elif usage
#!/usr/bin/python
#-*-Coding:utf-8-*-
num = 5
if num = = 3: # To determine the value of num
print ' Boss '
elif num = = 2:
print ' user '
elif num = = 1:
print ' worker '
Elif num < 0: # value is less than zero output
print ' ERROR '
Else
print ' Roadman ' # conditions are not set when the output
The output is:
>>> roadman# Output Results
2. Judgment of multiple conditions:
If condition:
code block
Elif Condition: (else if abbreviation elif)
code block
Else
code block
Cases:
num = 9
If num >= 0 and num <= 10: # Determine if the value is between 0~10
print ' Hello '
>>> hello# Output Results
num = 10
If num < 0 or num > 10: # Determines if the value is less than 0 or greater than 10
print ' Hello '
Else
print ' Undefine '
>>> undefine# Output Results
num = 8
# Determine if the value is between 0~5 or 10~15
if (num >= 0 and Num <= 5) or (Num >= and num <= 15):
print ' Hello '
Else
print ' Undefine '
>>> undefine# Output Results
3. Conditions
True False
1 > 2 n1 > N2 N1 = = N2
name = = "Alex" or name = = "Eric"
Name! = "Alex"
name = = "Alex" and pwd = = "123"
Python Loop statement:
1, Loop statement
The while loop executes the loop body when the given judgment condition is true, otherwise exits the loop body.
The For loop repeats the execution of the statement
Nested loops you can nest for loops in the while loop body
2, loop control statement:
The break statement terminates the loop during the execution of the statement block and jumps out of the entire loop
The continue statement terminates the current loop during the execution of the statement block, jumps out of the loop, and executes the next loop.
Pass Statement Pass is an empty statement in order to maintain the integrity of the program structure.
Python while Loop statement
Basic form:
While judging condition:
Execute statement ...
The judging condition can be any expression, and any value other than 0, or non-null (NULL), is true.
The loop ends when the condition false false is judged.
Cases:
#!/usr/bin/python
Count = 0
while (Count < 9):
print ' The count is: ', count
Count = Count + 1
Print "Good bye!"
The above code executes the output result:
The Count is:0
The Count is:1
The Count Is:2
The Count Is:3
The Count Is:4
The Count Is:5
The Count Is:6
The Count Is:7
The Count Is:8
Good bye!
While statement two important command Continue,break to skip the loop, continue used to skip the cycle, break is used to exit the loop, in addition, "judging condition" can also be a constant value, indicating that the loop must be set up, the following:
# Continue and break usage
i = 1
While I < 10:
i + = 1
If i%2 > 0: # Skip output when non-even
Continue
Print I # output 2, 4, 6, 8, 10
i = 1
While 1: # The loop condition is 1 must be set
Print I # output 1~10
i + = 1
If i > 10: # When I is greater than 10 o'clock jump out of the loop
Break
Infinite loops
If the conditional judgment statement is always true, the loop will execute indefinitely.
Cases:
#!/usr/bin/python
#-*-Coding:utf-8-*-
var = 1
while var = = 1: # The condition is always true and the loop will execute indefinitely
num = Raw_input ("Enter a number:")
Print "You entered:", num
Print "Good bye!"
The result of the above example output:
Enter a number:20
You entered:20
Enter a number:29
You entered:29
Enter a Number:3
You entered:3
Enter a number Between:traceback (most recent call last):
File "test.py", line 5, <module>
num = Raw_input ("Enter a number:")
Keyboardinterrupt
Looping with Else statements
In Python, for ... else means that the statement in for is no different from normal, while the statement in else is executed when the loop is executed normally (that is, for not breaking out by break), while ... else is the same.
#!/usr/bin/python
Count = 0
While Count < 5:
Print count, "is less than 5"
Count = Count + 1
Else
Print count, "is isn't less than 5"
The result of the above example output is:
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is isn't less than 5
Conditions and looping statements for Python