Python Operators
Python is a versatile language, and one of the fundamental concepts for any Python programmer to understand is operators. Operators in Python are special symbols that perform operations on variables or values. Python provides a wide range of operators that you can use to perform mathematical, logical, and comparison operations.
In this blog post, we will dive into the different types of Python operators, how they work, and practical examples to illustrate their use.
In Python, an operator is a symbol that performs operations on variables or values. Python has a variety of operators, including:
Each type of operator serves a specific purpose and is used in various scenarios in your Python code.
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.
+
: Addition-
: Subtraction*
: Multiplication/
: Division (returns float)//
: Floor Division (returns integer result)%
: Modulus (returns the remainder of division)**
: Exponentiation (raises the power)
# Arithmetic Operators
a = 10
b = 3
print("Addition: ", a + b) # Output: 13
print("Subtraction: ", a - b) # Output: 7
print("Multiplication: ", a * b) # Output: 30
print("Division: ", a / b) # Output: 3.333...
print("Floor Division: ", a // b) # Output: 3
print("Modulus: ", a % b) # Output: 1
print("Exponentiation: ", a ** b) # Output: 1000
Comparison operators are used to compare two values and return a Boolean value (True
or False
).
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
# Comparison Operators
x = 5
y = 10
print(x == y) # Output: False (5 is not equal to 10)
print(x != y) # Output: True (5 is not equal to 10)
print(x > y) # Output: False (5 is not greater than 10)
print(x < y) # Output: True (5 is less than 10)
print(x >= y) # Output: False (5 is not greater than or equal to 10)
print(x <= y) # Output: True (5 is less than or equal to 10)
Logical operators are used to combine conditional statements. They allow us to check multiple conditions at once.
and
: Returns True
if both conditions are trueor
: Returns True
if at least one condition is truenot
: Reverses the Boolean value
# Logical Operators
a = True
b = False
print(a and b) # Output: False (both conditions are not true)
print(a or b) # Output: True (at least one condition is true)
print(not a) # Output: False (reverses the Boolean value of a)
Assignment operators are used to assign values to variables. These operators are used to store data in variables in various ways.
=
: Assigns value+=
: Adds and assigns the value-=
: Subtracts and assigns the value*=
: Multiplies and assigns the value/=
: Divides and assigns the value//=
: Floor divides and assigns the value%=
: Modulus and assigns the value**=
: Exponentiation and assigns the value
# Assignment Operators
x = 5
x += 3 # x = x + 3
print(x) # Output: 8
x -= 2 # x = x - 2
print(x) # Output: 6
x *= 4 # x = x * 4
print(x) # Output: 24
x /= 6 # x = x / 6
print(x) # Output: 4.0
Bitwise operators are used to perform operations on binary numbers (bits). These operators work on integers and perform bit-level operations.
&
: AND|
: OR^
: XOR~
: NOT (inverts bits)<<
: Left shift>>
: Right shift
# Bitwise Operators
a = 10 # Binary: 1010
b = 4 # Binary: 0100
print(a & b) # Output: 0 (AND operation)
print(a | b) # Output: 14 (OR operation)
print(a ^ b) # Output: 14 (XOR operation)
print(~a) # Output: -11 (NOT operation)
print(a << 1) # Output: 20 (Left shift)
print(a >> 1) # Output: 5 (Right shift)
Membership operators are used to test if a value or variable is found in a sequence (like a list, tuple, or string).
in
: Returns True
if a value is found in the sequencenot in
: Returns True
if a value is not found in the sequence
# Membership Operators
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # Output: True
print("orange" not in fruits) # Output: True
Identity operators are used to compare the memory locations of two objects.
is
: Returns True
if both variables point to the same objectis not
: Returns True
if both variables do not point to the same object
# Identity Operators
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a is b) # Output: False (a and b are not the same object)
print(a is c) # Output: True (a and c are the same object)
print(a is not b) # Output: True (a and b are not the same object)