English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn how to create decisions using different forms of if..else in Python programs.
We only need to execute code when certain conditions are met, and this is when we need to make decisions.
The if…elif…else statements in Python are used for decision-making.
if test expression: statement(s)
Here, the program will evaluate and execute the statement only when the text expression 'test expression' is True.
If the text expression is False, the statement is not executed.
In Python, the body of the if statement is indicated by indentation. The body starts with indentation and ends with the first unindented statement.
Python treats non-zero values as True. None and 0 are considered False.
# If the number is positive, we print the appropriate message num = 3 if num > 0: print(num, "This is a positive number.") print("I will always print the output.") num = -1 if num > 0: print(num, "This is a negative number.") print("I will always print the output.")
When running the program, the output is:
3 This is a positive number. I will always print the output. I will always print the output.
In the above example, num > 0 is the test expression.
The body of the if statement is only executed when its value is True.
When the variablenumis equal to 3 then the test expression is true, and the statements inside the if body will be executed.
If the variablenumis equal to-1then the test expression is false, and the statements inside the if body will be skipped.
The print() statement is located outside the if block (not indented). Therefore, it will be executed regardless of the test expression.
if test expression: The body of the if code block else: The body of the else code block
The if..else statement evaluates the test expression, and the body of the if statement is only executed when the test condition is True.
If the condition is False, execute the body of else. Indentation is used to separate blocks.
# The program checks if the number is positive or negative # And display the appropriate message num = 3 # Try these values. # num = -5 # num = 0 if num >= 0: print("0 or a positive number") else: print("Negative number")
Output result
0 or a positive number
In the above example, when num is equal to3At this time, the test expression is true, and the body of if is executed, while the body of else is skipped.
Ifnumis equal to-5then the test expression is false, and the body of else is executed, while the body of if is skipped.
IfnumIf it is 0, the test expression is true, and the body of if is executed, while the body of else is skipped.
if test expression: Body of if elif test expression: Body of elif else: Body of else
elif is the abbreviation of else if. It allows us to check multiple expressions. If the condition of If is False, the next elif block's condition is checked, and so on.
If all conditions are False, the body of else is executed.
if...elif...else executes only one block among several blocks based on the condition.
An if block can only have one else block. However, it can have multiple elif blocks.
'''In this program We check if the number is positive or Negative numbers or zero and Display the appropriate message''' num = 3.4 # Try these two changes: # num = 0 # num = -4.5 if num > 0: print("Positive number") elif num == 0: print("0") else: print("Negative number")
When the variablenumFor positive, output:Positive number .
Ifnumequals 0, output:Zero .
IfnumFor negative numbers,Output: Negative number .
We can include an if...elif...else statement within another if...elif...else statement. This is called nesting in computer programming.
Any number of these statements can be nested within each other. Indentation is the only way to determine the level of nesting. They may cause confusion, so they should be avoided unless necessary.
'''In this program, we input a number Check if the number is positive or Negative numbers or zero and display Appropriate information This time we use nested if statements''' num = float(input("Enter a number: ")) if num >= 0: if num == 0: print("Zero") else: print("Positive number") else: print("Negative number")
Output1
Enter a number: 5 Positive number
Output2
Enter a number: -1 Negative number
Output3
Enter a number: 0 Zero