Pythondex
Learn Python

Operators In Python

Operators in Python are symbols that perform operations on variables and values. This tutorial covers various types of operators and how to use them in Python.

Example


# Using + Operator for addition
sum = 1 + 2
print(sum)

Types Of Operators

Here is the list of different type of operators we will learn in this tutorial:

As you can see there are around 7 types of operators so let’s see each one of them one by one.

1. Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations such as addition, subtraction, multiplication, division etc.

OperatorNameExample
+Additioina + b
Subtractiona – b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b
**Exponentiationa ** b
//Floor divisiona // b

Example


x = 10
y = 3

# Addition
result_addition = x + y
print("Addition:", result_addition)

# Subtraction
result_subtraction = x - y
print("Subtraction:", result_subtraction)

# Multiplication
result_multiplication = x * y
print("Multiplication:", result_multiplication)

# Division
result_division = x / y
print("Division:", result_division)

# Modulus (remainder)
result_modulus = x % y
print("Modulus (remainder):", result_modulus)

# Exponentiation
result_exponentiation = x ** y
print("Exponentiation:", result_exponentiation)

# Floor Division
result_floor_division = x // y
print("Floor Divsion:", result_floor_division)

2. Comparison Operators

Comparison operators are used to compare values and return a Boolean result

OperatorNameExample
==Equal Toa == b
!=Not Equal Toa != b
>Greater Thana > b
<Less Thana < b
>=Greater Than or Equal Toa >= b
<=Less Than or Equal Toa <= b

Example


x = 10
y = 5

# Equal to
is_equal = x == y
print("Equal to:", is_equal)

# Not equal to
is_not_equal = x != y
print("Not equal to:", is_not_equal)

# Greater than
is_greater_than = x > y
print("Greater than:", is_greater_than)

# Less than
is_less_than = x < y
print("Less than:", is_less_than)

# Greater than or equal to
is_greater_equal = x >= y
print("Greater than or equal to:", is_greater_equal)

# Less than or equal to
is_less_equal = x <= y
print("Less than or equal to:", is_less_equal)

3. Logical Operators

A logical operators are used to connect two or more expressions.

OperatorNameExample
andReturns True if both statements are truea < 4 and  a < 9
orReturns True if one of the statements is truea < 4 or a < 3
notReverse the result, returns False if the result is truenot(a < 4 and a < 9)

Example


x = True
y = False

# AND
result_and = x and y
print("Logical AND:", result_and)

# OR
result_or = x or y
print("Logical OR:", result_or)

# NOT
result_not = not x
print("Logical NOT:", result_not)

4. Assignment Operators

Assignment operators are used to assign values.

OperatorNameExample
=Assigns the value on the right to the variable on the leftx = 4
+=Adds and assigns in one step.x += 2
-=Subtracts and assigns in one step.x -= 2
*=Multiplies and assigns in one step.x *= 2
/=Divides and assigns in one step.x /= 2

Example


x = 5

# += (Addition assignment)
x += 3
print("Addition assignment:", x)

# -= (Subtraction assignment)
x -= 2
print("Subtraction assignment:", x)

# *= (Multiplication assignment)
x *= 4
print("Multiplication assignment:", x)

# /= (Division assignment)
x /= 2
print("Division assignment:", x)

5. Identity Operators

Identity operators are used to compare the memory locations of two objects..

OperatorNameExample
isReturns True if both variables are the same objectx is y
is notReturns True if both variables are not the same objectx is not y

Example


x = [1, 2, 3]
y = [1, 2, 3]

# is (True if the variables reference the same object)
is_same_object = x is y
print("is (same object):", is_same_object)

# is not (True if the variables reference different objects)
is_different_object = x is not y
print("is not (different object):", is_different_object)

6. Membership Operators

Membership operators are used to check if a value is a member of a sequence (e.g., list, tuple).

OperatorNameExample
in Returns True if the specified value is present in the object sequencex in y
not inReturns True if the specified value is not present in the object sequencex not in y

Example


numbers = [1, 2, 3, 4, 5]

# in (True if the value is in the sequence)
is_in_sequence = 3 in numbers
print("in (in sequence):", is_in_sequence)

# not in (True if the value is not in the sequence)
is_not_in_sequence = 6 not in numbers
print("not in (not in sequence):", is_not_in_sequence)

7. Bitwise Operators

Bitwise operators in Python manipulate individual bits of binary numbers.

OperatorNameExample
&ANDx & y
|ORx | y
^XORx ^ y
~NOT~x
<<Zero fill left shiftx << 2
>>Signed right shiftx >> 2

Example


# Bitwise AND
x = 5  # 0b0101 in binary
y = 3  # 0b0011 in binary
result_and = x & y
print("Bitwise AND:", result_and)

# Bitwise OR
result_or = x | y
print("Bitwise OR:", result_or)

# Bitwise XOR
result_xor = x ^ y
print("Bitwise XOR:", result_xor)

# Bitwise NOT
result_not_x = ~x
print("Bitwise NOT (x):", result_not_x)

# Bitwise Left Shift
result_left_shift = x << 1
print("Bitwise Left Shift:", result_left_shift)

# Bitwise Right Shift
result_right_shift = x >> 1
print("Bitwise Right Shift:", result_right_shift)

So these were all the operators in python, don't worry you will not require to memorise all of them there are few which will be used most of the time.