Javaexercise.com

If, if-else, if-elif Statements in Python

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of if statements. Let us have a look at them in detail.

What is if statement in Python?

In python, if statement is used to implement conditional execution of a piece of code.

This refers to the cases where we want a particular snippet of code to be executed when certain conditions are met and some other piece of code to be executed when they are not satisfied. 

This flowchart would help us understand this concept in a better manner - 

Python if statement Syntax

The syntax to use an if statement is as follows using the if keyword.

An important thing to note is that the statements to be executed should necessarily be on the next line and indented, unlike other programming languages.

if (<condition>):
    <statements to be executed>

The conditions are boolean expressions or expressions that ultimately result in a boolean value - either True or False

Some common boolean expressions are made using logical operators in python, namely greater than (>), less than (<), equal to (==), not equal to (!=), greater than or equal to (>=), less than or equal to (<=), etc. These operators have operands on both sides.

For example, if you want to check whether the integer on the left of the less than (<) operator is actually less than the integer on the right of the operator, we express it using 1 < 2. The integers under consideration are 1 and 2. This expression results in a True boolean value being returned because 1 is indeed less than 2.

Running example - Python If Statement

To better understand this concept, we shall follow a simple example of a piece of code that is used to print a message based on certain conditions.

Let us assume a variable x with a value of 2. If the value assigned to this variable is less than 5 then we want to print the message indicating that the value is less than 5.

Let us look at the python code and the corresponding output for this.

Python 3 Code

# Creating a variable x and assigning a value of 2 to it
x = 2

# Printing the appropriate output message
if(x<5):
  print(“Value of x is less than 5”)

Output

Value of x is less than 5

Nested If Statement in Python

Now say you want to make the execution of your code dependent on multiple conditions instead of just one.

One thing you can use is nested if statements concept.

Let’s say we want to check if the value of a variable x is greater than 1 and less than 5 and only if this is the case, we shall print the corresponding message that the value of x is greater than 1 and less than 5.

Let us look at the python code and the corresponding output for this.

# Creating a variable x and assigning a value of 2 to it
x = 2

# Printing the appropriate output message
if(x>1):
  if(x<5):
    print(“Value of x is greater than 1 and less than 5”)

Output 

Value of x is greater than 1 and less than 5

 

Another way of performing the above operation is to use another subset of logical operators.

In this particular case, the operator will be considerably useful to us. This operator returns a True boolean value only if the boolean expressions on both its left and right return a True value on evaluation, otherwise it returns False.

Let us look at the python code and the corresponding output for this.

# Creating a variable x and assigning a value of 2 to it
x = 2

# Printing the appropriate output message
if(x>1 and x<5):
  print(“Value of x is greater than 1 and less than 5”)

Output

Value of x is greater than 1 and less than 5

Python if-else Statement

Another statement that is commonly used along with if statements are the else statement.

This is used to specify an alternate route in case the conditions of the if statement is not met.

Let us consider the case that we want to print that the value of x is less than 5 if it actually is and print an appropriate message in case it is not.

Let us look at the python code and the corresponding output for this.

# Creating a variable x and assigning a value of 2 to it
x = 2

# Printing the appropriate output message
if(x<5):
  print(“Value of x is less than 5”)
else:
  print(“Value of x is not less than 5”)

Output

Value of x is less than 5

Python elif statement

Another statement that is commonly used along with if statements are the elif statement.

This is used to specify an alternate route in case the conditions of the if statement is not met and you also want to specify other conditions for this route.

Let us consider the case that we want to print that the value of x is less than 5 if it actually is and print an appropriate message in case it is greater than 5 or another case where it is equal to 5.

Let us look at the python code and the corresponding output for this - 

# Creating a variable x and assigning a value of 2 to it
x = 2

# Printing the appropriate output message
if(x<5):
  print(“Value of x is less than 5”)
elif(x>5):
  print(“Value of x is greater than 5”)
else:
  print(“Value of x is equal to 5”)

Output

Value of x is less than 5

Conclusion

In this topic, we have learned the use and advantages of if statements in a Python program, following a running example of a simple conditional printing program, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to [email protected] in case of any suggestions