
Operators in Python – Part 2
In the first part, we looked at arithmetic, comparison, and assignment operators. Now let's look at logical, membership, and identity operators.
Logical Operators
Logical operators are used to combine or reverse conditions.
Python has three logical operators:
andornot
Example:
age = 20
print(age >= 18 and age <= 60)Output:
TrueMembership Operators
Membership operators check whether a value is present in a sequence, such as a list or string.
The two main operators are in and not in.
fruits = ["apple", "banana", "mango"]
print("apple" in fruits)Output:
TrueIdentity Operators
Identity operators check whether two variables refer to the same object.
The two identity operators are is and is not.
a = [1, 2]
b = a
print(a is b)Output:
TrueJoin Techsnap Creators
Share your knowledge and earn ??
Want to showcase your tech expertise and get rewarded for your insights? Join the Techsnap creator network!
Write insightful blogs, stay ahead of industry trends, and grow your professional brand while helping others in the community.
Ready to make an impact?

Comments