Menu Close

Python if else statement

Python if else statement

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.

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 use 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.

Python if statement syntax:

if test condition:
	//code to be executed

In the above Python, if statement syntax, First checks the condition, When the condition will True Then code inside if statement will execute otherwise program will be terminated.

Python if statement Example:

In this example we will print simple message when a number is greater than 0.

num = 12
if num > 0:
	print("Number is greater than 0")

print("This line is always print")

num = -1
if num < -1:
	print("Number is less than -1")
print("This line is always printed")

Output

Number is greater than 0
This line is always print
This line is always printed

In above example num > 0 is the test the condition.
if the condition evaluate True, then body of if statement will be execute.

Again we have set the value of num is -1 and test the condition using num < -1. if the condition will be True, then the body of if the statement is executed.

Python if..else statement:

Python if..else statement is used to execute two blocks of code. One is when the condition will True and another is when the condition is False.

Python if..else statement 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 Example:

num = 12
if num > 0:
	print("Number is greater than 0")
else:
	print("Number if less than 0")

Output

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.

Python if..elif..else 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 if the statement is False. It checks the next condition of elif block and so on.

Python if..elif..else statement 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 printed.
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 printed.

Python nested if statement:

We can have use if statement inside another if statement. This process is called nested if statement in computer programming.

Python nested if 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.

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply