The Python conditional statement determines the code block to be executed based on the execution result (True or False) of one or more statements.
You can simply understand the execution process of the conditional statement through the following figure:
Code execution process:
if statement
The general form of the if statement in Python is as follows:
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
Python uses elif instead of else if, so the keyword of the if statement is: if-elif-else.
note:
1、 A colon: should be used after each condition to indicate that the next statement block is to be executed after the condition is met.
2、 Use indentation to divide sentence blocks, and sentences with the same indentation number together form a sentence block.
3、 There is no switch-case statement in Python.
Gif demo:
Examples
The following is a simple if example:
#! /usr/bin/python3
var1 =100if var1:print("1 -if expression condition is true")print(var1)
var2 =0if var2:print("2 -if expression condition is true")print(var2)print("Good bye!")
Execute the above code, the output result is:
1 -If expression condition is true
100
Good bye!
From the result, we can see that since the variable var2 is 0, the statement in the corresponding if is not executed.
The following example demonstrates the judgment of the dog's age calculation:
#! /usr/bin/python3
age =int(input("Please enter the age of your dog: "))print("")if age <=0:print("Are you kidding me!")
elif age ==1:print("The equivalent of a 14-year-old person.")
elif age ==2:print("The equivalent of a 22-year-old person.")
elif age 2:
human =22+(age -2)*5print("Corresponds to human age: ", human)
### Exit prompt
input("Click enter to exit")
Save the above script in the dog.py file and execute the script:
$ python3 dog.py
Please enter the age of your dog: 1
The equivalent of a 14-year-old person.
Click enter to exit
The following are the commonly used operation operators in if:
Operator | description |
---|---|
< | Less than |
<= | Less than or equal to |
Greater than | |
= | Greater than or equal to |
== | Equal, compare whether two values are equal |
!= | Not equal to |
#! /usr/bin/python3
# The program demonstrates==Operator
# Use numbers
print(5==6)
# Use variables
x =5
y =8print(x == y)
The output of the above example:
False
False
The high_low.py file demonstrates numerical comparison operations:
#! /usr/bin/python3
# This example demonstrates the number guessing game
number =7
guess =-1print("Number guessing game!")while guess != number:
guess =int(input("Please enter the number you guessed:"))if guess == number:print("Congratulations, you guessed it!")
elif guess < number:print("The guessed number is too small...")
elif guess number:print("Guess the number is big...")
Execute the above script, the output result of the example is as follows:
$ python3 high_low.py
Number guessing game!
Please enter the number you guessed: 1
The guessed number is smaller...
Please enter the number you guessed: 9
The guessed number is bigger...
Please enter the number you guessed: 7
Congratulations, you guessed it!
if nesting
In nested if statements, you can put 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
# ! /usr/bin/python3
num=int(input("Enter a number:"))if num%2==0:if num%3==0:print("The number you enter can divide 2 and 3")else:print("The number you entered can divide 2 but not 3")else:if num%3==0:print("The number you entered can divide 3, but not 2")else:print("The number you entered cannot divide 2 and 3")
Save the above program to the test_if.py file, and the output after execution is:
$ python3 test.py
Enter a number: 6
The number you enter can divide 2 and 3
Recommended Posts