Operator in Python
Operator in Python
Topperworld.in
Operators
• The operator is a symbol that performs a specific operation between two
operands, according to one definition.
• Operators serve as the foundation upon which logic is constructed in a
program in a particular programming language.
• In every programming language, some operators perform several tasks.
Same as other languages, Python also has some operators, and these are
given below -
➢ Arithmetic operators
➢ Comparison operators
➢ Assignment Operators
➢ Logical Operators
➢ Bitwise Operators
➢ Membership Operators
➢ Identity Operators
©Topperworld
Python Programming
Example:
a=9
b=4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Modulo of both number
mod = a % b
print(add)
print(sub)
print(mul)
print(mod)
©Topperworld
Python Programming
Output:
13
5
36
1
6561
< Less than: True if the left operand is less than the right x<y
©Topperworld
Python Programming
Example:
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
Output:
False
True
False
True
False
©Topperworld
Python Programming
©Topperworld
Python Programming
Example:
a = 10
# Assign value
b=a
print(b)
# Add and assign value
b += a
print(b)
# Subtract and assign value
b -= a
print(b)
# multiply and assign
b *= a
print(b)
©Topperworld
Python Programming
Output:
10
20
10
100
and Logical AND: True if both the operands are true x and y
Example:
# Examples of Logical Operator
a = True
b = False
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print not a is False
print(not a)
©Topperworld
Python Programming
Output:
False
True
False
| Bitwise OR x|y
Example:
# Examples of Bitwise operators
a = 10
b=4
# Print bitwise AND operation
print(a & b)
# Print bitwise OR operation
print(a | b)
©Topperworld
Python Programming
Output:
0
14
In Python, in and not in are the membership operators that are used to test
whether a value or variable is in a sequence.
Example:
if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if (y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
©Topperworld
Python Programming
Output:
Example:
a = 10
b = 20
c=a
print(a is not b)
print(a is c)
Output:
True
True
©Topperworld