Operators in Python
Operators are an important part of Python because they allow us to perform different operations on values.
Python provides operators for performing calculations, assigning values, comparing values, combining conditions, checking membership, comparing object identity, and working with binary representations.
An operator is a symbol or keyword that tells Python to perform a particular operation on one or more values.
For example:
a = 10
b = 5
print(a + b)Output:
15Here, + is an operator that performs addition.
Python provides several types of operators, including arithmetic, assignment, comparison, logical, membership, identity, and bitwise operators.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
The main arithmetic operators in Python are:
+— Addition-— Subtraction*— Multiplication/— Division%— Modulus//— Floor Division**— Exponentiation
Addition
The + operator adds two values.
a = 8
b = 6
print(a + b)Output:
14Subtraction
The - operator subtracts one value from another.
a = 15
b = 7
print(a - b)Output:
8Multiplication
The * operator multiplies two values.
a = 4
b = 7
print(a * b)Output:
28Division
The / operator performs division.
a = 21
b = 4
print(a / b)Output:
5.25Division using / produces a floating-point result.
Modulus
The % operator returns the remainder after division.
a = 21
b = 4
print(a % b)Output:
1Because 21 divided by 4 leaves a remainder of 1.
Floor Division
The // operator performs floor division.
a = 21
b = 4
print(a // b)Output:
5Floor division returns the floor of the division result.
Exponentiation
The ** operator is used to raise a number to a power.
a = 2
b = 5
print(a ** b)Output:
32This is equivalent to:
2 × 2 × 2 × 2 × 2 = 32Assignment Operators
Assignment operators are used to assign values to variables.
The most common assignment operator is:
=For example:
number = 10Here, the value 10 is assigned to number.
Python also provides operators that combine an arithmetic operation with assignment.
+= : The += operator adds a value to the existing value of a variable and assigns the result back to that variable.
number = 10
number += 5
print(number)Output:
15This is equivalent to:
number = number + 5-= : The -= operator subtracts a value from the existing value and assigns the result back to the variable.
number = 10
number -= 3
print(number)Output:
7This is equivalent to:
number = number - 3These operators allow a value to be updated without writing the variable name again in the longer form.
Comparison Operators
Comparison operators are used to compare two values.
The result of a comparison is a Boolean value: True or False.
Python provides the following comparison operators:
==— Equal to!=— Not equal to>— Greater than<— Less than>=— Greater than or equal to<=— Less than or equal to
For example:
a = 10
b = 5
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)Output:
False
True
True
False
True
FalseComparison operators are useful when a program needs to determine whether a particular condition is true or false.
Logical Operators
Logical operators are used to combine or modify conditions.
Python provides three logical operators:
andornot
and
The and operator returns True when both conditions are true.
age = 25
has_id = True
print(age > 18 and has_id)Output:
TrueThe basic behaviour of and is:
True and True → True
True and False → False
False and True → False
False and False → Falseor
The or operator returns True when at least one condition is true.
age = 16
has_permission = True
print(age >= 18 or has_permission)Output:
TrueIts basic behaviour is:
True or True → True
True or False → True
False or True → True
False or False → Falsenot
The not operator reverses a Boolean value.
print(not True)
print(not False)Output:
False
TrueThese operators are useful when working with multiple conditions in a program.
Membership Operators
Membership operators are used to check whether a value exists within a sequence or collection.
Python provides two membership operators:
innot in
For example, membership can be checked within a string:
text = "python"
print("p" in text)
print("z" in text)Output:
True
FalseThe in operator checks whether the specified value is present.
The not in operator checks whether the specified value is not present.
text = "python"
print("z" not in text)Output:
TrueMembership operators can also be used with collections such as lists.
For example:
numbers = [10, 20, 30, 40]
print(20 in numbers)
print(50 not in numbers)Output:
True
TrueIdentity Operators
Identity operators are used to check whether two variables refer to the same object.
Python provides:
isis not
For example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b)
print(a is c)Output:
True
FalseHere, b refers to the same object as a, so:
a is breturns True.
Get Mrunal Shrikumar’s stories in your inbox
Join Medium for free to get updates from this writer.
Although c contains the same values as a, it is a different object. Therefore:
a is creturns False.
Identity vs Equality
Identity is different from equality.
The == operator checks whether two values are equal:
a == cThis returns:
Truebecause both contain the same values.
The is operator, on the other hand, checks whether two variables refer to the same object:
a is cThis returns:
Falsebecause a and c refer to different objects.
Bitwise Operators
Bitwise operators work with the binary representation of numbers.
Python provides several bitwise operators:
&— Bitwise AND|— Bitwise OR^— Bitwise XOR~— Bitwise NOT<<— Left shift>>— Right shift
To understand bitwise operators, it is useful to first understand how numbers can be represented in binary.
Binary Representation
Binary is a number system that uses only two digits:
0 and 1For example, the decimal number 5 can be represented in binary as:
101The decimal number 3 can be represented as:
11Python provides the bin() function to display the binary representation of an integer.
number = 10
print(bin(number))Output:
0b1010The prefix 0b indicates that the value is represented in binary.
Binary representation is particularly useful when understanding bitwise operations because these operators work on individual bits.
Bitwise AND (&)
The & operator performs AND between corresponding binary bits.
The basic behaviour of AND is:
0 & 0 → 0
0 & 1 → 0
1 & 0 → 0
1 & 1 → 1For example:
a = 18
b = 3
print(a & b)Output:
2The operation is performed on the binary representation of the two numbers.
Bitwise OR ( | )
The | operator performs a bitwise OR operation between corresponding bits.
The basic behaviour of OR is:
0 | 0 → 0
0 | 1 → 1
1 | 0 → 1
1 | 1 → 1For example:
a = 5
b = 3
print(a | b)Output:
7Bitwise XOR (^)
The ^ operator performs a bitwise XOR operation.
XOR produces 1 when the two corresponding bits are different and 0 when they are the same.
Its basic behaviour is:
0 ^ 0 → 0
0 ^ 1 → 1
1 ^ 0 → 1
1 ^ 1 → 0For example:
a = 5
b = 3
print(a ^ b)Output:
6Bitwise NOT (~)
The ~ operator performs a bitwise NOT operation.
For example:
print(~3)Output:
-4Similarly:
print(~6)Output:
-7The result may initially seem surprising because bitwise NOT operation works on the binary representation of an integer.
Left Shift (<<)
The left-shift operator moves the bits of a number to the left.
For example:
a = 5
print(a << 1)Output:
10Another example:
a = 35
print(a << 3)Output:
280Right Shift (>>)
The right-shift operator moves the bits of a number to the right.
For example:
a = 10
print(a >> 1)Output:
5Another example:
a = 280
print(a >> 3)Output:
35Left and right shift operations are performed on the binary representation of integers.
Operator Precedence
When an expression contains more than one operator, Python needs to determine which operation should be performed first.
This is known as operator precedence.
For example:
result = 2 + 3 * 4
print(result)Output:
14The multiplication is performed before the addition.
Parentheses can be used when a specific operation needs to be performed first:
result = (2 + 3) * 4
print(result)Output:
20Here, the expression inside the parentheses is evaluated first.
This is similar to the mathematical order of operations often remembered as BODMAS.
Using parentheses also makes expressions easier to understand and reduces ambiguity.
For example:
result = (10 + 5) + (8 / 2)
print(result)The expressions inside the parentheses are evaluated first.
The + Operator with Strings
The behaviour of an operator can depend on the data type of the values being used.
The + operator can also be used to combine strings.
first_name = "Riya"
last_name = "Sharma"
print(first_name + last_name)Output:
RiyaSharmaThis operation is called string concatenation.
However, a string and an integer cannot be directly added together.
name = "Riya"
age = 25
print(name + age)This produces a TypeError because Python does not automatically convert the integer into a string for this operation.
The value can instead be converted explicitly:
print(name + str(age))Output:
Riya25This is one example of why understanding data types and type conversion is important when working with operators.
A Practical Example
Several operators can be combined in a single program.
marks = 78
passing_marks = 40
print(marks >= passing_marks)
print(marks + 2)
print(marks % 10)
print(marks > 50 and marks < 100)Output:
True
80
8
TrueHere:
>=compares the marks with the passing marks.+performs addition.%finds the remainder.andcombines two conditions.
This shows how different operators can be used together to work with values and conditions.
Key Takeaways
- Arithmetic operators perform mathematical calculations.
- Assignment operators assign and update values.
- Comparison operators compare values and return Boolean results.
- Logical operators combine or modify conditions.
- Membership operators check whether a value exists in a sequence or collection.
- Identity operators check whether two variables refer to the same object.
- Bitwise operators work with the binary representation of integers.
bin()can be used to view the binary representation of an integer.- Shift operators move bits to the left or right.
- Operator precedence determines the order in which operations are evaluated.
- The behaviour of an operator can depend on the data types involved.
- Strings can be combined using the
+operator, while values of incompatible types need to be converted before they can be combined.







