In this tutorial, you will learn about Python if else statement. In this guide, we will see how to create decisions in the Python program. You can different forms of if..else statement in Python.
In the previous tutorial, we have seen details about Python Operators.
The if…elif…else
statement is used in Python for decision making.
Before going through this tutorial Let’s understand what is an if-else statement in Python why we should use it.
Headings of Contents
What is Python if else statement?
Python if else statement is used to create decisions in the Python program. Sometimes we want to create decisions in the Program for specific tasks, Then we have the best way that is if statement. You can use elif statement with the python if-else statement.
Why should use Python if else statement ?
Sometimes we want to execute a block of code when a specific condition is True, Then we can use Python if else statement. Python if the statement is very useful to make decision making in the Python program.
Indentation:
Python relies on indentation ( whitespaces at the beginning of the line ) to define the scope in the code but other programming languages use a curly bracket to define the scope.
# If statement, without indentation ( will raise en error )
a = 12
b = 10
if a > b:
print("a is greater than b") # You will get an error on this line.
Let’s understand Python if..else..elif statement using appropriate syntax and examples.
Python If Statement ?
Python if statement is used to execute a block of code when a specific condition is True.
Syntax:
if test condition:
//code to be executed
In the above Syntax, if statement, First checks the condition, When the condition evaluates True, Then code inside if statement will execute otherwise program will be terminated.
Python if statement flowchart
Example:
In this example we will print simple message when a number is greater than 0.
num = 12
if num > 0:
print("The Number is greater than 0")
num = -1
if num < -1:
print("Number is less than 0")
print("This line is always printed")
Output
The number is greater than 0
This line is always print
This line is always printed
Example Explain:
- Define a variable num with the value 12.
- Check condition whether num > 0.
- Condition evaluated True, Then Body inside if statement is executed.
- Again we define a variable num with the value -1.
- Check condition whether num < -1.
- Condition evaluated False, then if the statement is terminated.
- At the end line, we printed a simple message.
Python if..else statement:
Python if..else statement is used to execute two blocks of code. One is when the condition evaluates True and another is when the condition evaluates False.
Syntax:
if test condition:
// code to be executed
else:
// code to be executed
In the above syntax, Firstly will check the condition, if the condition will be True, Then the body of the if statement will execute, and if the condition will be False, then the body of the else statement will execute.
Python if..else statement flowchart
Example:
num = 12
if num > 0:
print("The Number is greater than 0")
else:
print("Number if less than 0")
Output
The number is greater than 0
In above Example, When the num is greater than 0, Then code of if statement will execute otherwise code of else statement will execute.
In our case the value of num is greater than 0, Then the code of if statement will be executed.
Python if..elif..else statement:
Python elif statement is used to check the condition if the previous condition is False. If we want to test the multiple conditions in your program, Then you can use elif statement.
Syntax:
if test condition:
// code to be executed
elif test condition:
// code to be executed
else:
// code to be executed
In above syntax, we have used the elif statement to check the condition, if the previous condition is False.
Python elif statement is useful for using multiple conditions in the program. If the condition of the if statement is False. It checks the next condition of elif block and so on.
Python if…elif..else flowchart
Example:
num = 10
if num < 0:
print(num,'is less than 0')
elif num == 10:
print(num,"is equal to the 10")
else:
print("Number is greater than 0")
Output:
10 is equal to the 10
Output
10 is equal to the 10
In the above example,
- If the num is less than 0, Then the code of the if the statement is executed.
- If the num is equal to 10, Then the code of elif block is executed.
- If the num is greater than 0, Then the code of the else block is executed.
Python nested if statement:
We can use if statement inside another if statement. This process is called nested if statement in computer programming.
Example:
num = int(input('Enter any number:- '))
if num > 0:
if num == 0:
print("Your entered number is zero")
else:
print("Your entered number is greater than 0")
else:
print("Your entered number is less than 0")
Short Hand If..Else Statement:
If you have only one statement to execute, one for if another is else, Then you can put all in the same line.
a = 12
b = 10
print("a") if a > b else print("b")
In above program output will be a.
Note:- This technique is called ternary operator or condition expression.
You can also use multiple if statement in same line.
a = 12
b = 12
print("a") if a > b else print("=") if a == b else print("b")
In above program output will be =.
The Python pass statement:
In Python, if statement can not be empty but if you want to keep if statement empty, Then you have best statement that is pass statement.
b = 200
if b > a:
pass
Conclusion:
In this tutorial, you have learned Python if-else statement as well as Python if..else statement with appropriate syntax and examples.
Python if-else statement is most useful for every programming language when you want to check the different conditions and execute the corresponding block of code.
I hope you will like this Python if the statement tutorial. if this tutorial will have helped you, please share with your friends who want to learn Python program.