Skip to content

Python Operators

In Python, operators are special symbols or keywords used to perform operations on variables and values. They are essential for carrying out various computations and comparisons.

Arithmetic Operators:

        
# Addition
result_addition = 5 + 3  # 8

# Subtraction
result_subtraction = 5 - 3  # 2

# Multiplication
result_multiplication = 5 * 3  # 15

# Division
result_division = 5 / 3  # 1.666...
        
    

Comparison Operators:

        
# Equal to
is_equal = (5 == 3)  # False

# Not equal to
is_not_equal = (5 != 3)  # True

# Greater than
is_greater_than = (5 > 3)  # True

# Less than
is_less_than = (5 < 3)  # False
        
    

Logical Operators:

        
# Logical AND
result_and = (True and False)  # False

# Logical OR
result_or = (True or False)  # True

# Logical NOT
result_not = not True  # False
        
    

Assignment Operators:

        
x = 5
y = 3

# Addition assignment
x += y  # x = x + y

# Subtraction assignment
x -= y  # x = x - y

# Multiplication assignment
x *= y  # x = x * y
        
    

Membership Operators:

        
fruits = ["apple", "orange", "banana"]

# In
is_in_list = "apple" in fruits  # True

# Not in
is_not_in_list = "grape" not in fruits  # True
        
    

Identity Operators:

        
a = [1, 2, 3]
b = a

# Is
is_same_object = (a is b)  # True

# Is not
is_not_same_object = (a is not b)  # False