Multiline conditions with ‘if’ statements in Python

In this Python tutorial, we will learn how to style multi-line conditions in ‘if’ statements in Python.

Agenda

Let’s dive into the tutorial.

Multi-line conditions in ‘if’ statements with brackets

If you are using brackets around if statements, then there are two different styles of using brackets. Let’s see them.

Scenario-1: Using brackets and conditions in the same line

We can use brackets and conditions in a same line in Python.

format

if (condition/expression operator condition/expression operator
   condition/expression operator condition/expression
   ...............
   ...............):
   other statements
   ..........
   ..........

Example:

In this example, we will create 4 variables and check thier values.

# Declare four variables and assign values
a=34
b=51
c=56
d=90

# Check if a==34 and b==51 and c==56 and d==51
if (a == 34 and b == 51 and
    c == 56 and d == 90):
   print("All matched")
else:
   print('Not Matched')

Output:

All matched

All are matched. Therefore, if the block got executed.

Scenario-2: Using brackets and conditions on different lines.

We can also use brackets and conditions in a different lines.

format

if (
   condition/expression operator condition/expression operator
   condition/expression operator condition/expression
   ...............
   ...............
   ):
   other statements
   ..........
   ..........

Example:

In this example, we will create 4 variables and check if all are matched.

# Declare four variables and assign values
a=34
b=51
c=56
d=90

# Check if a==34 and b==51 and c==56 and d==51
if (a == 34 and
    b == 51 and
    c == 56 and
    d == 90):
   print("All matched")
else:
   print('Not Matched')

Output:

All matched

All are matched. Therefore, the if the block got executed.

Multi-line conditions in ‘if’ statements without brackets

Here, we are not using any brackets inside if statements.

Scenario 1: Without brackets in the same line

In this case, we have to specify all the conditions inside if statement on the same line without using any brackets.

format:

if condition/expression operator condition/expression ....:
   other statements
   ..........
   ..........

Example:

# Declare four variables and assign values
a=34
b=51
c=56
d=90

# Check if a==34 and b==51 and c==56 and d==51
if a == 34 and b== 51 and c == 56 and d== 90:
   print("All matched")
else:
   print('Not Matched')

Output

All matched

We can see that all the 4 conditions are on the same line.

Scenario 2: Without brackets on different lines

In this case, we have to specify all the conditions inside the if statement on a different line without using any brackets using ‘\’.

format:

if condition/expression operator \
   condition/expression ....:
   other statements
   ..........
   ..........

Example: In this example, we will specify conditions in three lines with .

# Declare four variables and assign values
a=34
b=51
c=56
d=90

# Check if a==34 and b==51 and c==56 and d==51
if  a == 34  and  \
    b == 51  and  \
    c == 56  and  \
    d == 90:
        print("All matched")
else:
        print('Not Matched')

Output

All matched

We can see that all the 4 conditions are on the different lines.

Note – If we didn’t specify \ at end of the line, it will throw an error.

Let’s demonstrate the error.

# Declare four variables and assign values
a=34
b=51
c=56
d=90

# Check if a==34 and b==51 and c==56 and d==51
if  a == 34  and
    b == 51  and
    c == 56  and
    d == 90:
        print("All matched")
else:
        print('Not Matched')

Error:

  File "temp.py", line 8
    if  a == 34 and
                  ^
SyntaxError: invalid syntax

Summary

In this tutorial, we have seen different styles of multiline if statements. Mostly, using brackets is better, based on your requirement you can use any one of the above styles.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top