Python3 condition control (9) and python3 condition Control
A Python Condition Statement is a code block that is determined by the execution result (True or False) of one or more statements.
You can briefly understand the execution process of conditional statements:
If statement
In Python, the if statement is generally in the following format:
if condition_1: statement_block_1elif condition_2: statement_block_2else: statement_block_3
Note:
- If "condition_1" is True, the "statement_block_1" BLOCK statement will be executed.
- If "condition_1" is False, "condition_2" is determined"
- If "condition_2" is True, the "statement_block_2" BLOCK statement will be executed.
- If "condition_2" is False, the "statement_block_3" BLOCK statement is executed.
Used in PythonElifReplacedElse ifSo the keyword of the if statement is:If-elif-else.
Note:
- 1. Each condition is followed by a colon (:), indicating the statement block to be executed after the condition is met.
- 2. Use indentation to divide statement blocks. statements with the same number of indentation form a statement block.
- 3. There is no switch-case statement in Python.
Simple if instance:
1 #! /Usr/bin/python3 2 3 var1 = 100 4 if var1: 5 print ("1-if expression condition is true") 6 print (var1) 7 8 var2 = 0 9 if var2: 10 print ("2-if expression condition is true") 11 print (var2) 12 print ("Good bye! ")
Run the above Code and the output result is:
1 1-if expression condition is true2 1003 Good bye!
The result shows that the corresponding if statement is not executed because the variable var2 is 0.
The following operators are commonly used in if:
Instance:
1 #! /Usr/bin/python3 2 3 # This instance demonstrates the number guessing game 4 number = 7 5 guess =-1 6 print ("number guessing game! ") 7 while guess! = Number: 8 guess = int (input ("Enter the number you guessed:") 9 10 if guess = number: 11 print ("Congratulations, you guessed it! ") 12 elif guess <number: 13 print (" the guess number is small... ") 14 elif guess> number: 15 print (" the guess number is too big... ")
The instance output result is as follows:
1 digit quiz game! 2. Enter the number you guessed: 13. The number you guessed is smaller... 4. Enter the number you guessed: 95. The number you guessed is too large... 6. Enter the number you guessed: 77. Congratulations! You guessed it!
If nesting
In the nested if statement, you can put the if... elif... else structure 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
Instance:
1 #! /Usr/bin/python3 2 3 num = int (input ("enter a number:") 4 if num % 2 = 0: 5 if num % 3 = 0: 6 print ("the number you entered can be divided by 2 and 3") 7 else: 8 print ("the number you entered can be divided by 2, but not 3") 9 else: 10 if num % 3 = 0: 11 print ("the number you entered can be divided by 3, but not by 2") 12 else: 13 print ("the number you entered cannot be divided by 2 or 3 ")
Output result:
1 enter a number: 62 the number you entered can be divided into 2 and 3