4

Multiline conditions with ‘if’ statements in Python

 2 years ago
source link: https://thispointer.com/multiline-conditions-with-if-statements-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

Advertisements

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
..........
..........
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
# 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')
# 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 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

condition/expression operator condition/expression operator
condition/expression operator condition/expression
...............
...............
other statements
..........
..........
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
# 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')
# 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 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
..........
..........
if condition/expression operator condition/expression ....:
   other statements
   ..........
   ..........

Example:

# Declare four variables and assign values
# 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')
# 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 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
..........
..........
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
# 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')
# 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 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
# 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')
# 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
  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.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK