Conditional statements in Python
As the name suggests, conditional statements are used to handle conditions in your program. These statements direct the program to make certain decisions based on the conditions that the program implements. Conditional statements in Python are also used to execute a particular block of code based on the true value of a condition. The most common conditional statements in Python are if, elif, and else. These statements allow the program to react differently depending on whether the string of conditions is true or false.
We must first become familiar with the usual mathematical conditions. Python supports the following set of mathematical conditions
- Equals: x==y
- Does not equal: x != y
- Greater than: x > y
- Greater than or equal to: x >= y
- Less than: x < y
- Less than or equal to: x <= y
Conditional if statement in Python.
The conditional if statement in Python is used to determine whether a block of code will be executed or not. If the program finds that the condition specified in the if statement is true, it will execute the block of code inside the if statement.
How to word it like this:
if condition: #If the condition is true
#The statements thatundefined It will be executed if
# the condition is true
example:
x = 50
y = 90
if y > x: print("x is greater than y")
The output will be:
x is greater than y
In the previous example we used two variables, x and y, which are used as part of an if statement to test whether y is greater than x. Since x equals 50, and y equals 90, then it is printed that “y is greater than x”.
Use the else statement with an if conditional statement.
The if statement executes the block of code when the condition is true. Similarly, the else statement works in conjunction with the if statement to execute a block of code when the specified if condition is false.
How to word it like this:
if (condition): # Execute this block if #condition is true
else: # Execute this block if
# the condition is false
example:
a = 4
if a > 5: print("Greater than 5 a")
else: print( "less than 10 a")
The output will be:
a less than 10
Use the elif statement with if and else conditionals.
The elif statement is used to check for multiple conditions and execute the code block inside them if any of the conditions evaluates to true.
The phrase undefined elif is similar to the else statement in that it is optional. But unlike the else statement, there can be multiple elif statements in the code block after the if statement.
to execute a block of code when the specified if condition is false.
How to word it like this:
if (condition): # Execute this block if # the condition is true
elif(condition): # Execute this block if # the previous conditions are false. The program implements this condition if it is true
else: # Execute this block if # the condition is false
example:
a = 100
b = 100
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
The output will be:
a and b are equal
In the previous example a was equal to b, so the first condition is not true, but the elif condition is true, and thus prints to the screen that "a and b are equal".
How to write if and else conditional statements on one line.
If there is only one statement that must be executed in each of the if and else blocks, then the if and else conditional statements can be written. On one line.
example:
a, b = 10, 20
print("A") if a > b else print("B")
The output will be:
B
How to write conditional statements if and else. In a nested manner.
This is the way to write conditional statements if and else. In a nested manner. That is, writing if-else inside another if statement. Or in simple words, first, there is an outer if statement, and inside it there is another if – else statement,
example:
a = 60
if a > 30:
print("Greater than 30")
if a > 40:
print("Also larger than 40")
else:
print("Smaller than 40"
The output will be:
Greater than 30
Also larger than 40
Tags
Python conditional statements
Share page
About author
Ahmed Obaid
Hello, I am Ahmed Obaid, an Egyptian Arabic programmer. I would like to put my experiences learning Python on this site So that it will be a reference for you and me as well.
Sign up to leave a comment