Developed By
Gautam Kumar - Full stack developer
DEEP DIVE INTO
Operators in Python are special symbols or keywords used to perform operations on variables and values. Python provides a variety of operators for tasks such as arithmetic, comparison, logical operations, assignment, and more. Here are some common categories of operators in Python:
Arithmetic Operators: These operators perform mathematical operations on numeric values.
Addition: +
Subtraction: -
Multiplication: *
Division: /
Floor Division (integer division): //
Modulus (remainder): %
Exponentiation:**
Example:
pythona = 10
b = 3
result_add = a + b # 13
result_div = a / b # 3.3333...
result_floor_div = a // b # 3
result_mod = a % b # 1
result_exp = a ** b # 1000
Comparison Operators: These operators are used to compare values and return Boolean results.
Equal to:
Not equal to:
Greater than:
Less than:
Greater than or equal to:
Less than or equal to:
Example:
pythonx = 5
y = 7
result_equal = x == y # False
result_greater = x > y # False
Logical Operators: Logical operators are used to perform logical operations on Boolean values.
Logical AND:
Logical OR:
Logical NOT:
Example:
pythonp = True
q = False
result_and = p and q # False
result_or = p or q # True
result_not = not p # False
Assignment Operators: These operators are used to assign values to variables.
Assignment:
Add and assign:
Subtract and assign:
Multiply and assign:
Divide and assign:
Modulus and assign:
Floor divide and assign:
Exponentiate and assign:
Example:
pythonnum = 10
num += 5 # num is now 15
Bitwise Operators: These operators perform operations at the bit level.
Bitwise AND:
Bitwise OR:
Bitwise XOR:
Bitwise NOT:
Left shift:
Right shift:
Example:
pythona = 5 # 0101 in binary
b = 3 # 0011 in binary
result_and = a & b # 0001 (1 in decimal)
Membership Operators: These operators check for the presence of a value in a sequence.
in
not in
Example:
pythonmyList = [1, 2, 3, 4, 5]
result = 3 in myList # True
Identity Operators: These operators compare the memory locations of two objects.
is
is not
Example:
pythonx = [1, 2, 3]
y = x
result = x is y # True
These are some of the most commonly used operators in Python. Understanding how to use these operators is essential for writing Python programs to perform various operations and calculations.