Conditional Statements in Python

To solve any problem in computer science, we define an algorithm, which is - a finite sequence of well-defined, computer-implementable instructions. We've performed basic mathematical and textual operations in Python. But many tasks require decision making based on some conditions.

The purpose of a programming language is to provide us ways through which we can implement these algorithms. Conditional and looping are two very important concepts. In this article, we'll discuss conditional statements in Python. There are three keywords in Python for this - if, elif, else.

If Statements

An if statement is one of the control structures(order of the execution of statements). It takes a condition and executes statements based on the result of condition evaluation.

# Example 1
student_name = 'vaibhav'

if student_name == 'vaibhav':
    print("Student's name is Vaibhav")

# Output
# Student's name is Vaibhav


# Example 2
num1 = 10
num2 = 20

if num1 == num2:
    print("num1 is equal to num2")

if num2 > num1:
    print("num2 is greater than num1")

if num1 > num2:
    print("num1 is greater than num2")

# Output
# num2 is greater than num1

Elif Statements

It is used when we want to capture cases when the previous conditions evaluate to False. There should be at least one if statement to use elif statement.

student_name = 'vaibhav'

if student_name == 'vaibhav':
    print("Student's name is Vaibhav")
elif student_name == 'rishabh':
    print("Student's name is rishabh")

# Output
# Student's name is Vaibhav

Else statements

It is used if we want to capture cases where none of the previous conditions evaluates to True. It can't be used without at least one if statement.

student_name = 'aarav'

if student_name == 'vaibhav':
    print("Student's name is Vaibhav")
else:
    print("Student's name is not Vaibhav")

# Output
# Student's name is not Vaibhav

In the above example #2, we can see that each of three conditional are executed, even if the first or second condition evaluates to True, while we know that if any of the three condition is True we don't need to check others. It can be rewritten as -

# Example 2(rewritten)
num1 = 10
num2 = 20

if num1 == num2:
    print("num1 is equal to num2")

elif num2 > num1:
    print("num2 is greater than num1")

else:
    print("num1 is greater than num2")

# Output
# num2 is greater than num1

Nested If Statements

We can nest if-elif-else statements inside any if-elif-else block.

x = 10
if not x % 2:
    if x < 100:
        print("x is an even number and less than 100")

The above example can be rewritten using logical and operator

x = 10
if (not x % 2) and (x < 100):
    print("x is an even number and less than 100")

With careful design, you'd notice, in most cases, you can avoid using nested if-elif-else block by using logical and, logical or operator.

Inline if statement

If we have only one statement inside if block, it can be written after if statement itself.

debug = True
if debug: print("In debug Mode!")

# Output
# In debug Mode!

We should not abuse this functionality by creating a complex single statement.

Inline if-else statement

num = 10
x = 'odd' if num % 2 else 'even'
print(x)

# Output
# even

It is also known as the ternary operator. It can transform multiple lines of a simple if-else block in a single compact line.

Indentation in if-elif-else block

As we know that Python identifies a block of code by its indentation level.  In other languages, a block is identified by {}, begin-end, if..fi, etc.

Hence it is advised that - be consistent when using indentation. Poorly indented code can lead to error or weird or unexpected results from programs.

Keep Coding. Never Stop Hacking!!

0 Comments

Join discussion:

Login