How to Use Python AND With IF?


Conditional statements in Python allow us to execute different blocks of code based on particular conditions. It executes the code blocks only if the specified condition is full-filled. While “AND” is a logical operator used to combine various conditions. The “IF AND” in Python allows us to combine various conditions in the if statement. The “IF AND” retrieves true when all the expressions specified within the if block retrieves true.

This article will show you how to use the AND operator in an if-statement in Python.

Conditional Statements in Python

Python has three types of conditional statements: If, if-elif-else, and if-else.

How to Use If Statement in Python?

It executes a code block only if a condition is true, as described in the below snippet:

if condition: 
     # The associated code block will execute only when the condition is true.

How to Use If-else Statement in Python?

The if-else statement handles both true and false conditions: the if statement executes the set of statements if a condition is true, and the else statement executes the associated statements if the condition is not true. The following syntax is exercised to work with the if-else statement:

if condition: 
     # Statements written here will execute if the condition is true
else: 
    # Statements written here will execute if the condition is not true

How to Use if-elif-else Statement in Python?

The if-elif-else statement executes one block of code among multiple alternatives. Use the below provided basic syntax for if-elif-else statement:

if first_condition:
    # This code block will execute if first_condition is true
elif second_condition:
    # Statements written here will execute if first_condition is false and second_condition is true
else:
    # This code block will execute if all conditions are false

AND Operator in Python

In Python, the logical “AND” retrieves True only if both operands retrieve True and False otherwise. Here’s the basic syntax for using the AND operator in Python:

Operand_1 AND Operand_2

The AND operator will return one of the following results:

Operand_1Operand_2Operand_1 AND Operand_2
TRUETRUETRUE
TRUEFALSEFALSE
FALSETRUEFALSE
FALSEFALSEFALSE
AND Operator Working

How to Use Python AND With IF?

The logical AND can be used with conditional statements to combine/join several conditions.

if (condition_1) AND (condition_2):
    # This code will execute if both conditions are true.

Example 1: AND Operator within If Statement

In this example, you will learn how to use the AND operator with an if-statement in python.

Code:

firstNum = int(input("Enter First Number:"))
secondNum = 5
if firstNum > secondNum and firstNum < 10:
    print('firstNum is greater than 5 but less than 10')

Two variables are created in the program. The input() function takes a user input as a string. The int() function is used with the input() function to convert the user’s entered value to INTEGER data type. Finally, the “and” operator is used within the “if” statement to join multiple conditions. The statements written within the if-block will execute only if both conditions are fulfilled.

Output:

Example 2: AND Operator with If-else Statement

The following code example will show you how to use the AND operator with an if-else statement in python. The if statement will use the AND operator to join two conditions.

Code:

firstNum = int(input("Enter First Number:"))
secondNum = 12
if firstNum > secondNum and firstNum < 30:
    print('firstNum is greater than 12 but less than 30')
else:
    print('Either firstNum is less than 12 or greater than 30')

This example will handle both true and false scenarios, i.e., the if statement will execute when the specified conditions are true. In contrast, the else block will execute when the specified conditions are false.

Output:

Example 3: AND Operator with If-elif-else Statement

This example states the usage of the “if-elif-else” statement:

firstNum = int(input("Enter First Number:"))
secondNum = 6
if firstNum > secondNum and firstNum < 10:
    print('firstNum is greater than secondNum but less than 10')
elif firstNum > secondNum and firstNum <30:
    print('firstNum is greater than secondNum but less than 30')
else:
    print('Either firstNum is less than equal to secondNum or greater than 30')

In this example, the if statement will execute only when the first number is greater than a second number but less than 10. The elif statement will execute if the first number is greater than the second one but less than 30. And other than that, the else statement will execute.

Output:

Conclusion

In Python, “if” is one of the conditional statements that execute a code block only if the given condition is true. While “AND” is a logical operator used to combine various conditions. The “IF AND” in Python allows us to combine various conditions in the if statement. The “IF AND” retrieves true when all the expressions specified within the if block retrieves true. 

Print Friendly, PDF & Email