If-else Statement in Python
Mastering If-else Statement in Python: A Comprehensive Guide
The if-else statement in Python is used to execute a block of code based on a condition. Understanding the if-else statement is fundamental to writing flexible and dynamic Python programs. In this comprehensive guide, we’ll explore the syntax and usage of the if-else statement in Python.
What is the If-else Statement in Python?
The if-else statement in Python is used to execute a block of code based on a condition. The if-else statement is also known as the if-then-else statement. The if-else statement is a selection statement, which is a type of control statement in Python.
Syntax of the If-else Statement in Python
The syntax of the if-else statement in Python is as follows:
if condition:
# code to execute if condition is True
else:
# code to execute if condition is Falseif condition:
# code to execute if condition is True
else:
# code to execute if condition is FalseThe if-else statement begins with the ifif keyword, followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The code to execute if the condition is FalseFalse is written after the elseelse keyword, on the next line, indented by four spaces.
Rules for Writing the If-else Statement in Python
The following are the rules for writing the if-else statement in Python:
- The
ififkeyword must be followed by a condition. - The condition must be followed by a colon (
::). - The code to execute if the condition is
TrueTruemust be indented by four spaces. - The
elseelsekeyword must be followed by a colon (::). - The code to execute if the condition is
FalseFalsemust be indented by four spaces. - The
elseelseclause is optional. - The
elseelseclause must be written after theififclause. - The
elseelseclause must be written before theelifelifclause. - The
elseelseclause must be written after theelifelifclause. - The
elifelifclause is optional. - The
elifelifclause must be written after theififclause. - The
elifelifclause must be written before theelseelseclause. - The
elifelifclause must be written after theelifelifclause. - The
elifelifkeyword must be followed by a condition.
There are five ways to write the if-else statement in Python:
ififstatementif-elseif-elsestatementif-elif-elseif-elif-elsestatementnested if-elsenested if-elsestatementternary operatorternary operatorstatement
If Statement
ifif Statement
The ifif statement in Python is used to execute a block of code if a condition is TrueTrue. The ifif statement is also known as the if-thenif-then statement. The ifif statement is a selection statement, which is a type of control statement in Python.
The syntax of the ifif statement in Python is as follows:
if condition:
# code to execute if condition is Trueif condition:
# code to execute if condition is TrueDiagram:
Diagram of the if statement in Python
graph TD
A[Start] --> B{Condition}
B -->|True| C[Execute Code]
C --> D[End]
B ---->|False| E[End]
if statement
The ifif statement begins with the ifif keyword, followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces.
The following example demonstrates how to use the ifif statement in Python:
# if statement
x = 10
if x > 5:
print("x is greater than 5")# if statement
x = 10
if x > 5:
print("x is greater than 5")Output:
C:\Users\Your Name> python if.py
x is greater than 5C:\Users\Your Name> python if.py
x is greater than 5In the above example, we have used the ifif statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.
If-else Statement
if-elseif-else Statement
The if-elseif-else statement in Python is used to execute a block of code if a condition is TrueTrue. If the condition is FalseFalse, a different block of code is executed. The if-elseif-else statement is also known as the if-then-elseif-then-else statement. The if-elseif-else statement is a selection statement, which is a type of control statement in Python.
The syntax of the if-elseif-else statement in Python is as follows:
if condition:
# code to execute if condition is True
else:
# code to execute if condition is Falseif condition:
# code to execute if condition is True
else:
# code to execute if condition is FalseThe if-elseif-else statement begins with the ifif keyword, followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The code to execute if the condition is FalseFalse is written after the elseelse keyword, on the next line, indented by four spaces.
Diagram:
Diagram of the if-else statement in Python
graph TD
A[Start] --> B{Condition}
B -->|True| C[Execute Code]
C --> D[End]
B ---->|False| E[Execute Code]
E --> F[End]
if-else statement
The following example demonstrates how to use the if-elseif-else statement in Python:
# if-else statement
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")# if-else statement
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")Output:
C:\Users\Your Name> python if-else.py
x is greater than 5C:\Users\Your Name> python if-else.py
x is greater than 5In the above example, we have used the if-elseif-else statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.
If-elif-else Statement
if-elif-elseif-elif-else Statement
The if-elif-elseif-elif-else statement in Python is used to execute a block of code if a condition is TrueTrue. If the condition is FalseFalse, a different block of code is executed. The if-elif-elseif-elif-else statement is also known as the if-then-elseif-then-else statement. The if-elif-elseif-elif-else statement is a selection statement, which is a type of control statement in Python.
The syntax of the if-elif-elseif-elif-else statement in Python is as follows:
if condition1:
# code to execute if condition1 is True
elif condition2:
# code to execute if condition2 is True
else:
# code to execute if condition1 and condition2 are Falseif condition1:
# code to execute if condition1 is True
elif condition2:
# code to execute if condition2 is True
else:
# code to execute if condition1 and condition2 are FalseThe if-elif-elseif-elif-else statement begins with the ifif keyword, followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The elifelif keyword is followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The code to execute if the condition is FalseFalse is written after the elseelse keyword, on the next line, indented by four spaces.
Diagram:
Diagram of the if-elif-else statement in Python
graph TD
A[Start] --> B{Condition1}
B -->|True| C[Execute Code]
C --> D[End]
B ---->|False| E{Condition2}
E -->|True| F[Execute Code]
F --> G[End]
E ---->|False| H[Execute Code]
H --> I[End]
if-elif-else statement
The following example demonstrates how to use the if-elif-elseif-elif-else statement in Python:
# if-elif-else statement
x = 10
if x > 5:
print("x is greater than 5")
elif x < 5:
print("x is less than 5")
else:
print("x is equal to 5")# if-elif-else statement
x = 10
if x > 5:
print("x is greater than 5")
elif x < 5:
print("x is less than 5")
else:
print("x is equal to 5")Output:
C:\Users\Your Name> python if-elif-else.py
x is greater than 5C:\Users\Your Name> python if-elif-else.py
x is greater than 5In the above example, we have used the if-elif-elseif-elif-else statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.
Another example of the if-elif-elseif-elif-else statement in Python:
# if-elif-else multiple statements
x = 10
if x > 5:
print("x is greater than 5")
elif x < 5:
print("x is less than 5")
elif x > 10:
print("x is greater than 10")
else:
print("x is equal to 5")# if-elif-else multiple statements
x = 10
if x > 5:
print("x is greater than 5")
elif x < 5:
print("x is less than 5")
elif x > 10:
print("x is greater than 10")
else:
print("x is equal to 5")Output:
C:\Users\Your Name> python if-elif-else.py
x is greater than 5C:\Users\Your Name> python if-elif-else.py
x is greater than 5In the above example, we have used the if-elif-elseif-elif-else statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.
Nested If-else Statement
nested if-elsenested if-else Statement
The nested if-elsenested if-else statement in Python is used to execute a block of code if a condition is TrueTrue. If the condition is FalseFalse, a different block of code is executed. The nested if-elsenested if-else statement is also known as the nested if-then-elsenested if-then-else statement. The nested if-elsenested if-else statement is a selection statement, which is a type of control statement in Python.
The syntax of the nested if-elsenested if-else statement in Python is as follows:
if condition1:
# code to execute if condition1 is True
if condition2:
# code to execute if condition2 is True
else:
# code to execute if condition2 is False
else:
# code to execute if condition1 is Falseif condition1:
# code to execute if condition1 is True
if condition2:
# code to execute if condition2 is True
else:
# code to execute if condition2 is False
else:
# code to execute if condition1 is FalseThe nested if-elsenested if-else statement begins with the ifif keyword, followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The ifif keyword is followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The code to execute if the condition is FalseFalse is written after the elseelse keyword, on the next line, indented by four spaces. The code to execute if the condition is FalseFalse is written after the elseelse keyword, on the next line, indented by four spaces.
Diagram:
Diagram of the nested if-else statement in Python
graph TD
A[Start] --> B{Condition1}
B -->|True| C[Execute Code]
C --> D{Condition2}
D -->|True| E[Execute Code]
E --> F[End]
D ---->|False| G[Execute Code]
G --> H[End]
B ---->|False| I[Execute Code]
I --> J[End]
nested if-else statement
The following example demonstrates how to use the nested if-elsenested if-else statement in Python:
# nested if-else statement
x = 10
if x > 5:
print("x is greater than 5")
if x > 10:
print("x is greater than 10")
else:
print("x is less than or equal to 10")
else:
print("x is less than or equal to 5")# nested if-else statement
x = 10
if x > 5:
print("x is greater than 5")
if x > 10:
print("x is greater than 10")
else:
print("x is less than or equal to 10")
else:
print("x is less than or equal to 5")Output:
C:\Users\Your Name> python nested-if-else.py
x is greater than 5
x is less than or equal to 10C:\Users\Your Name> python nested-if-else.py
x is greater than 5
x is less than or equal to 10Ternary Operator
ternary operatorternary operator Statement
The ternary operatorternary operator statement in Python is used to execute a block of code if a condition is TrueTrue. If the condition is FalseFalse, a different block of code is executed. The ternary operatorternary operator statement is also known as the conditional operatorconditional operator statement. The ternary operatorternary operator statement is a selection statement, which is a type of control statement in Python.
The syntax of the ternary operatorternary operator statement in Python is as follows:
condition1 if condition2 else condition3condition1 if condition2 else condition3The ternary operatorternary operator statement begins with a condition, followed by the ifif keyword. The ifif keyword is followed by a condition. The condition is followed by the elseelse keyword. The elseelse keyword is followed by a condition.
Diagram:
Diagram of the ternary operator statement in Python
graph TD
A[Start] --> B{Condition2}
B -->|True| C[Execute Code]
C --> D[End]
B ---->|False| E[Execute Code]
E --> F[End]
ternary operator statement
The following example demonstrates how to use the ternary operatorternary operator statement in Python:
# ternary operator statement
x = 10
print("x is greater than 5") if x > 5 else print("x is less than or equal to 5")# ternary operator statement
x = 10
print("x is greater than 5") if x > 5 else print("x is less than or equal to 5")Output:
C:\Users\Your Name> python ternary-operator.py
x is greater than 5C:\Users\Your Name> python ternary-operator.py
x is greater than 5In the above example, we have used the ternary operatorternary operator statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.
Another example of the ternary operatorternary operator statement in Python:
# ternary operator multiple statements
x = 10
print("x is greater than 5") if x > 5 else print("x is less than or equal to 5") if x < 5 else print("x is equal to 5")# ternary operator multiple statements
x = 10
print("x is greater than 5") if x > 5 else print("x is less than or equal to 5") if x < 5 else print("x is equal to 5")Output:
C:\Users\Your Name> python ternary-operator.py
x is greater than 5C:\Users\Your Name> python ternary-operator.py
x is greater than 5In the above example, we have used the ternary operatorternary operator statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.
Conclusion
In this guide, we explored the syntax and usage of the if-else statement in Python. We also explored the ternary operator, which is a one-line shorthand for the if-else statement. Now that you have a solid understanding of the if-else statement in Python, you can use it to write flexible and dynamic Python programs. In this guide, we explored the syntax and usage of the if-else statement in Python. We also explored the ternary operator, which is a one-line shorthand for the if-else statement. Now that you have a solid understanding of the if-else statement in Python, you can use it to write flexible and dynamic Python programs.
As you delve deeper into Python programming, experiment with different control statements, explore their applications in real-world scenarios, and use them to enhance the efficiency and clarity of your code. For more hands-on examples and in-depth tutorials, explore our resources on Python Central Hub!
Was this page helpful?
Let us know how we did
