Home »
Python
and keyword with example in Python
Python and keyword: Here, we are going to learn about the and keyword with example.
Submitted by IncludeHelp, on April 12, 2019
Python and keyword
and is a keyword (case-sensitive) in python, it is a logical operator actually, it is used to validate more than one conditions. It is similar to Logical AND (&&) operator in C, C++ programming. It requires a minimum of two conditions and returns True – if both conditions are True.
Truth table for "and" keyword/operator
Condition1 Condition2 (Condition1 and Condition2)
True True True
True False False
False True False
False False False
Syntax of and keyword/operator:
condition1 and condition2
Example:
Input:
a = 10
b = 20
# conditions
print(a>=10 and b>=20)
print(a>10 and b>20)
Output:
True
False
Python examples of and operator
Example1: Take two numbers and test the conditions using and operator
# python code to demonstrate example of
# and keyword/operator
a = 10
b = 20
# printing return values
print(a>=10 and b>=20)
print(a>10 and b>20)
print(a==10 and b==20)
print(a==10 and b!=20)
Output
True
False
True
False
Example 2: Input age and check whether it is teenage or not
# Input age and check teenage or not
# input
age = int(input("Enter age: "))
# condition
if age>=13 and age<=19:
print("Yes ", age, " is a teenage")
else:
print("No ", age, " is not a teenage")
Output
First run:
Enter age: 17
Yes 17 is a teenage
Second run:
Enter age: 21
No 21 is not a teenage
Example 3: Input a character and check whether it is an uppercase alphabet or not
# input a character and check whether
# it is an uppercase alphabet or not
# input
ch = input("Enter a character: ")
# condition
if ch>='A' and ch<='Z':
print("\'", ch, "\' is an uppercase alphabet")
else:
print("\'", ch, "\' is not an uppercase alphabet")
Output
First run:
Enter a character: I
' I ' is an uppercase alphabet
Second run:
Enter a character: x
' x ' is not an uppercase alphabet