Logical “and” & “or” with if statement in Python

In this Python tutorial, you will learn how to use the logical and (&&), logical or (||) operators with if statement in Python.

Table Of Contents

Let’s dive into the tutorial.

logical “and” & “or” operators in Python

The logical operators are used to check the conditions and return boolean values. If the condition satisfies, then they will return True, otherwise False. In other words, logical operators can be used to combine conditional statements like if, if-else, etc.

logical and:

Advertisements

The logical and operator will return True, when all the conditions specified are True, otherwise, it will return False. We can use the ‘and’ operator to implement logical and.

Syntax:

condition1 and condition2 and ..............

To create a condition, we can use relational operators, comparison operators, etc. For example,

if (count > 0) and (count < 100):
    pass

logical or:

The logical or operator will return True when any of the conditions specified is True, otherwise, it will return False. We can use the ‘or’ operator to implement logical or.

Syntax:

condition1 or condition2 or ..............

To create a condition, we can use relational operators, comparison operators, etc. For example,

if (count == 1) or (count == 3):
    pass

Example 1:

In this example, we will use logical and, or operators to check some conditions.

# Create two integer variables and assign values to it
a=34
b=21

# Check if a is greater than b and  a is not equal to b
print(a>b and a!=b)

# Check if a is greater than b or a is  equal to b
print(a>b or a==b)

# Check if a is less  than b and  a is not equal to b
print(a<b and a!=b)

Output:

True
True
False

In the above code, we are

  1. Checking if a is greater than b and a is not equal to b. It will return True, since both the conditions are satisfied.
  2. Checking if a is greater than b or a is equal to b. It will return True, since the first condition is satisfied.
  3. Checking if a is less than b and a is not equal to b. It will return False, since the first condition is not satisfied.

Using logical “and” with if statement

We can use these logical operators, to check the condition with if block. If the condition is True, then code inside if block is executed, otherwise, it will go to the next statements.

logical “and” is used with if condition followed by the syntax

if(condition1 and  condition2 and  ..............):
    statements inside if block
next statements
..............
..............

If all the conditions specified inside if the condition is True, then statements inside if block will be executed and the next statements are also executed. Otherwise, code inside the if-block will be skipped and only the next statements are executed.

Example 1:

Check if a is greater than b and a is not equal to b.

# Create two integer variables and assign values to it
a=34
b=21

# Check if a is greater than b and  a is not equal to b
if(a>b and a!=b):
    print("a is greater and not equal to b")

# Other statements
print("Bye")

Output:

a is greater and not equal to b
Bye

We can see that the condition specified inside if block is True, hence print() statement inside if block is executed and finally next print() statement is executed outside if block.

Example 2:

Check if a is less than b and a is not equal to b.

# Create two integer variables and assign values to it
a=34
b=21

# Check if a is less than b and  a is not equal to b
if(a<b and a!=b):
    print("a is greater and not equal to b")

#other statements
print("Bye")

Output:

Bye

We can see that the condition specified inside if block is False, hence print() statement inside if block is not executed and only the next print() statement is executed outside the if block.

Example 3:

We can also use the else block if the condition fails in if block.

# Create two integer variables and assign values to it
a=34
b=21

# Check if a is less than b and  a is not equal to b
if(a<b and a!=b):
    print("a is lesser and not equal to b")
else:
    print("a is greater and not equal to b")

Output:

a is greater and not equal to b

We can see that condition inside if block is False, so else block is executed.

Using logical “or” with if statement

The logical “or” is used with if condition followed by the syntax.

if(condition1 or  condition2 or  ..............):
    statements inside if block
next statements
..............
..............

If any of the conditions specified inside if a condition is True, then statements inside if block will be executed and the next statements are also executed. Otherwise, only the next statements are executed.

Example 1:

Check if a is greater than b or a is not equal to b.

# Create two integer variables and assign values to it
a=34
b=21

# Check if a is greater than b or  a is not equal to b
if(a>b or a!=b):
    print("a is greater or not equal to b")

#other statements
print("Bye")

Output:

a is greater or not equal to b
Bye

We can see that the condition specified inside if block is True. Hence print() statement inside if block is executed and finally next print() statement is executed outside if block.

Example 2:

Check if a is less than b or a is equal to b.

# Create two integer variables and assign values to it
a=34
b=21

# Check if a is less or b and  a is  equal to b
if(a<b or a==b):
    print("a is greater or  equal to b")

#other statements
print("Bye")

Output:

Bye

We can see that the condition specified inside if block is False. Hence print() statement inside if block is not executed and only the next print() statement is executed outside if block.

Example 3:

We can also use the else block if the condition fails in the if block.

# Create two integer variables and assign values to it
a=34
b=21

# Check if a is less than b or  a is  equal to b
if(a<b or a==b):
    print("a is lesser or not equal to b")
else:
    print("a is greater or not equal to b")

Output:

a is greater or not equal to b

Using logical “and” logical “or” with if statement

If we want to specify the multiple conditions, then we can apply both the operators in a condition.

Example:

In this example, we will check whether a greater than 34 and b less than 45 or b is equal to 21.

# Create two integer variables and assign values to it
a=34
b=21

# Check whether a greater than 34 and b less than 45
# or b is equal to 21
if((a>34 and b < 45) or (b==21)):
    print("Welcome to thispointer")

print("other code")

Output:

Welcome to thispointer
other code

To check whether a greater than 34 and b is less than 45, we used “and” logical operator. Then we used the logical “or” operator to check if b is equal to 21. So, if either of (a>34 and b < 45) or (b==21) is True, then if-block will get executed.

Summary

In this logical operators tutorial, we have seen how to place conditions using logical “and” & “or” operators. Based on the need for your application, you can include these operators in your code/project. Happy Coding.

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

Leave a Comment

Your email address will not be published.

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

Scroll to Top