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 False
if condition:
# code to execute if condition is True
else:
# code to execute if condition is False
The if-else statement begins with the if
if
keyword, followed by a condition. The condition is followed by a colon (:
:
). The code to execute if the condition is True
True
is written on the next line, indented by four spaces. The code to execute if the condition is False
False
is written after the else
else
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
if
if
keyword must be followed by a condition. - The condition must be followed by a colon (
:
:
). - The code to execute if the condition is
True
True
must be indented by four spaces. - The
else
else
keyword must be followed by a colon (:
:
). - The code to execute if the condition is
False
False
must be indented by four spaces. - The
else
else
clause is optional. - The
else
else
clause must be written after theif
if
clause. - The
else
else
clause must be written before theelif
elif
clause. - The
else
else
clause must be written after theelif
elif
clause. - The
elif
elif
clause is optional. - The
elif
elif
clause must be written after theif
if
clause. - The
elif
elif
clause must be written before theelse
else
clause. - The
elif
elif
clause must be written after theelif
elif
clause. - The
elif
elif
keyword must be followed by a condition.
There are five ways to write the if-else statement in Python:
if
if
statementif-else
if-else
statementif-elif-else
if-elif-else
statementnested if-else
nested if-else
statementternary operator
ternary operator
statement
If Statement
if
if
Statement
The if
if
statement in Python is used to execute a block of code if a condition is True
True
. The if
if
statement is also known as the if-then
if-then
statement. The if
if
statement is a selection statement, which is a type of control statement in Python.
The syntax of the if
if
statement in Python is as follows:
if condition:
# code to execute if condition is True
if condition:
# code to execute if condition is True
Diagram:
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 if
if
statement begins with the if
if
keyword, followed by a condition. The condition is followed by a colon (:
:
). The code to execute if the condition is True
True
is written on the next line, indented by four spaces.
The following example demonstrates how to use the if
if
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 5
C:\Users\Your Name> python if.py
x is greater than 5
In the above example, we have used the if
if
statement to check if the value of the variable x
x
is greater than 5
5
. Since the value of x
x
is 10
10
, the condition is True
True
, and the code inside the if
if
statement is executed. The string "x is greater than 5"
"x is greater than 5"
is printed to the console.
If-else Statement
if-else
if-else
Statement
The if-else
if-else
statement in Python is used to execute a block of code if a condition is True
True
. If the condition is False
False
, a different block of code is executed. The if-else
if-else
statement is also known as the if-then-else
if-then-else
statement. The if-else
if-else
statement is a selection statement, which is a type of control statement in Python.
The syntax of the if-else
if-else
statement in Python is as follows:
if condition:
# code to execute if condition is True
else:
# code to execute if condition is False
if condition:
# code to execute if condition is True
else:
# code to execute if condition is False
The if-else
if-else
statement begins with the if
if
keyword, followed by a condition. The condition is followed by a colon (:
:
). The code to execute if the condition is True
True
is written on the next line, indented by four spaces. The code to execute if the condition is False
False
is written after the else
else
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-else
if-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 5
C:\Users\Your Name> python if-else.py
x is greater than 5
In the above example, we have used the if-else
if-else
statement to check if the value of the variable x
x
is greater than 5
5
. Since the value of x
x
is 10
10
, the condition is True
True
, and the code inside the if
if
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-else
if-elif-else
Statement
The if-elif-else
if-elif-else
statement in Python is used to execute a block of code if a condition is True
True
. If the condition is False
False
, a different block of code is executed. The if-elif-else
if-elif-else
statement is also known as the if-then-else
if-then-else
statement. The if-elif-else
if-elif-else
statement is a selection statement, which is a type of control statement in Python.
The syntax of the if-elif-else
if-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 False
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 False
The if-elif-else
if-elif-else
statement begins with the if
if
keyword, followed by a condition. The condition is followed by a colon (:
:
). The code to execute if the condition is True
True
is written on the next line, indented by four spaces. The elif
elif
keyword is followed by a condition. The condition is followed by a colon (:
:
). The code to execute if the condition is True
True
is written on the next line, indented by four spaces. The code to execute if the condition is False
False
is written after the else
else
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-else
if-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 5
C:\Users\Your Name> python if-elif-else.py
x is greater than 5
In the above example, we have used the if-elif-else
if-elif-else
statement to check if the value of the variable x
x
is greater than 5
5
. Since the value of x
x
is 10
10
, the condition is True
True
, and the code inside the if
if
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-else
if-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 5
C:\Users\Your Name> python if-elif-else.py
x is greater than 5
In the above example, we have used the if-elif-else
if-elif-else
statement to check if the value of the variable x
x
is greater than 5
5
. Since the value of x
x
is 10
10
, the condition is True
True
, and the code inside the if
if
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-else
nested if-else
Statement
The nested if-else
nested if-else
statement in Python is used to execute a block of code if a condition is True
True
. If the condition is False
False
, a different block of code is executed. The nested if-else
nested if-else
statement is also known as the nested if-then-else
nested if-then-else
statement. The nested if-else
nested if-else
statement is a selection statement, which is a type of control statement in Python.
The syntax of the nested if-else
nested 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 False
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 False
The nested if-else
nested if-else
statement begins with the if
if
keyword, followed by a condition. The condition is followed by a colon (:
:
). The code to execute if the condition is True
True
is written on the next line, indented by four spaces. The if
if
keyword is followed by a condition. The condition is followed by a colon (:
:
). The code to execute if the condition is True
True
is written on the next line, indented by four spaces. The code to execute if the condition is False
False
is written after the else
else
keyword, on the next line, indented by four spaces. The code to execute if the condition is False
False
is written after the else
else
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-else
nested 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 10
C:\Users\Your Name> python nested-if-else.py
x is greater than 5
x is less than or equal to 10
Ternary Operator
ternary operator
ternary operator
Statement
The ternary operator
ternary operator
statement in Python is used to execute a block of code if a condition is True
True
. If the condition is False
False
, a different block of code is executed. The ternary operator
ternary operator
statement is also known as the conditional operator
conditional operator
statement. The ternary operator
ternary operator
statement is a selection statement, which is a type of control statement in Python.
The syntax of the ternary operator
ternary operator
statement in Python is as follows:
condition1 if condition2 else condition3
condition1 if condition2 else condition3
The ternary operator
ternary operator
statement begins with a condition, followed by the if
if
keyword. The if
if
keyword is followed by a condition. The condition is followed by the else
else
keyword. The else
else
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 operator
ternary 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 5
C:\Users\Your Name> python ternary-operator.py
x is greater than 5
In the above example, we have used the ternary operator
ternary operator
statement to check if the value of the variable x
x
is greater than 5
5
. Since the value of x
x
is 10
10
, the condition is True
True
, and the code inside the if
if
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 operator
ternary 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 5
C:\Users\Your Name> python ternary-operator.py
x is greater than 5
In the above example, we have used the ternary operator
ternary operator
statement to check if the value of the variable x
x
is greater than 5
5
. Since the value of x
x
is 10
10
, the condition is True
True
, and the code inside the if
if
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