In this Python Tutorial, we will explore the usage of Python if, if … elif, and else Statements. After completing this tutorial, you will gain sufficient knowledge of Python if, if … elif, else Statements.
Hello, & Welcome!
Part 6- Understanding and Mastering Conditional Statements in Python
You will learn following in this Tutorial-
- If statement and its syntax.
- elif statement and its syntax.
- if-elif-else statement and its syntax.
- Nested if-else and its syntax.
- Logical operators.
- Logical conditions.
If statement and its syntax
This is a conditional statement and is mostly used when you need to check a certain condition using your Python code.
If statement is used when the results of the condition mainly true or false type that is Boolean type.
Syntax of if statement is-
if test condition:
action(a)
The IF statements first check whether the condition is true or not; if the condition is true, then the intended action is executed, otherwise the subsequent statements are skipped.
Let’s have a look at the example-
age=5
if age<10:
print('You are young')
OUTPUT-
You are young
In this case, the value of age is assigned as 5 which also falls below 10. Since the condition evaluates to true, the message is displayed.
Indentation–
Indentation is important in Python. It refers to keeping blank space before starting a new line of code in Python. Without Indentation, your code does not work. For other languages, indentation is for readability, but for Python, it is crucial.
if 7 > 3:
print("Seven is greater than three!")
For instance, if your code is without proper indentation, it will result in an error.
if 5 > 2:
print("Five is greater than two!") #wrong indentation, gives error.
As a programmer, you can keep any number of blank spaces, but you should keep in mind that always keep the same amount of space for a particular block of code. It may be different from block to block.
elif statement and its syntax
Elif statements are useful when you need to check several conditions. Elif statement are typically used alongside the IF statement. When IF statement evaluates to false then program control jumps to Elif condition and executes the subsequent statements.
Syntax of elif statement is-
if test condition:
action(x)
elif test condition:
action(y)
Here in this case, the program control initially evaluates the IF condition; if it returns true then the subsequent statements are executed. If the condition is false then the control moves to the elif condition and checks – if that condition is true then the corresponding statements are executed.
Let’s take a look in the example-
x=7
y=7
if x<y:
print('x is less than y')
elif x==y:
print('x and y are equal')
OUTPUT-
x and y are equal
In this case, elif statement executed because the first if condition is not true which is a<b but the second condition a==b is true.
if-elif-else statement and its syntax.
This statement also checks multiple condition. When if and elif both statement not true (false) then the else part of the code will execute. But any of the if and elif statements get true then the else statement will not execute.
Syntax of if-elif-else-
If test condition:
action(1)
elif test condition:
action(2)
else test condition:
action(3)
As I said above, it first checks the condition of if and elif part, if both conditions are false then only else statement execute.
Let’s see in the example-
x=50
y=10
if x<y:
print('x is less than y')
elif x==y:
print('x and y are equal')
else x>y
print('x is greater than y')
OUTPUT-
x is greater than y
Nested if-else and its syntax
Nested if-else statement means using the if-else statement inside another if-else statement, there may be multiple label of nesting depending upon the application requirement. To perform nesting Indentation is critical in Python.
Syntax of Nested if-else statement-
if test condition:
if test condition:
action(a)
else:
action(b)
else
action(c)
Here, two cases-
- When the first if is true – the program control checks the first if condition. If it is true then it jumps inside the second if and checks the second if condition. If found true then execute the desired action; if not true then jump to the else part and execute the else statements.
- First If is not true-If the first if condition is not true then the control directly jumps to the last else statement and execute the associate codes.
Example of nested if-else-
age = 32
if age <= 18:
if age == 3:
print("Infant")
else:
print("Teenager")
else:
print('Adult')
OUTPUT-
Adult
In this case, the condition is false because the age is 30 which is greater than 19 because of this the control directly jumps to else part of the statement executing the else statement.
Logical operators.
There are basically two logical operators-
- AND
- OR
AND Operator
AND operator mainly use when we need to combine multiple conditional statement,
Let’s see in the example-
John=30
Alex=25
Bob= 35
if John> Alex and Bob>John:
print("Bob is elder than John and Alex")
OUTPUT-
Bob is elder than John and Alex
- The above if statement only gets true when all the logical conditions are true; if any of the conditions are false, the if statement will not execute.
OR Operator-
The OR operator is also used to combine multiple logical conditions. OR operator does not need all the conditions to be true, if any of the conditions is true then it will execute.
For example, two conditional statements are combined with the OR operator, if either of the statements is true, then the statement will execute. Unlike the AND operator it does not need all the statements to be true to execute the statement.
Let’s see in the example-
Bob=40
Alex=35
Max= 45
if Bob> Alex or Max==John:
print("Max is elder than Bob and Alex")
OUTPUT-
Max is elder than Bob and Alex
Logical Conditions-
These are the Logical conditions-
- < (less than) For eg, x<y
- > (greater than) For eg, x>y
- == (Equal) For eg, x==y
- != (not equal to) For eg, x!=y
- => (equal to or greater than) For eg, x=>y
- =< (equal to or less than) For eg, x =<y
That’s all for our discussion on if … else Statements in Python. Hopefully, now you have a clear understanding of how if…else statements in Python work.
Congratulations! You’ve successfully mastered the use of Python if…else statements in Python.
We’ll begin exploring Loop in Python in the upcoming tutorial.
Enjoy Learning Python!