×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python and Keyword

By IncludeHelp Last updated : December 07, 2024

Description and Usage

The 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

Truth table for "and" keyword/operator

Condition1	Condition2	(Condition1 and Condition2)
True		True		True 
True 		False		False
False		True		False
False 		False		False

Syntax

condition1 and condition2

Sample Input/Output

Input:
a = 10
b = 20

# conditions
print(a>=10 and b>=20)
print(a>10 and b>20)

Output:
True
False

Example 1

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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.