Python And Keyword (original) (raw)
Last Updated : 06 Mar, 2025
The **and keyword in Python is a logical operator used to combine two conditions. It returns True if both conditions are true, otherwise, it returns False. It is commonly used in if statements, loops and Boolean expressions.
Let's understand with a simple example.
Python `
x = 10 y = 5
if x > 0 and y > 0: print("Both conditions are true")
if x < 0 and y > 0: print("One condition is false")
if x < 0 and y < 0: print("Both conditions are false")
`
Output
Both conditions are true
**Explanation: Only the first if statement is evaluated as True because both the conditions in it are true.
Now, let's explore some common use cases of "**and" keyword with examples.
Using and in Conditional Statements
**and keyword is mostly used in if statements to check multiple conditions at once.
Python `
age = 20 inc = 5000
if age > 18 and inc > 3000: print("Eligible for a credit card")
`
Output
Eligible for a credit card
Using and in Loops
**and keyword can be used inside loops to control iteration based on multiple conditions.
Python `
a = 1
while a < 10 and a % 2 != 0: print(a) a += 2
`
**Explanation: in the code we are printing all odd numbers less than 10 by simultaneously checking for two conditions (**a < 10 and **a % 2 != 0) using and.
Using and with Boolean Values
Python `
a = True b = False
print(a and b)
print(a and True)
print(False and False)
`
**Explanation: Only those conditions are being evaluated as True where both left and right part of the **and keyword is True.