Operators in Python
Operators in Python are essential tools that allow you to manipulate data, perform calculations, and make decisions in your programs. They allow you to perform operations on variables and values, enabling Python to perform everything from basic math to complex logic.
Operators in Python can be classified into several categories based on the type of operation they perform:
- Arithmetic Operators: Used for mathematical operations like addition, subtraction, multiplication, and more.
- Comparison Operators: Used to compare values and return boolean results, such as True or False.
- Logical Operators: Used to combine multiple conditions and evaluate their truth values.
- Assignment Operators: Used to assign values to variables and modify their existing values.
- Identity Operators: Used to compare the memory locations of two objects.
- Membership Operators: Used to check whether a value is part of a sequence (like a list or string).
- Bitwise Operators: Used to perform bit-level operations on integers, manipulating the individual bits of the data.
What You'll Learn
In this tutorial, you'll learn how to use Python operators for performing arithmetic, comparison, logical, and assignment operations, helping you build more dynamic and functional programs.
1. Arithmetic Operators
Arithmetic operators in Python are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. These operators help you manipulate numbers and perform calculations within your programs.
The basic arithmetic operators are:
Operator | Example | Operator Name |
---|---|---|
+ | 5 + 3 = 8 | Addition (adds two values together) |
- | 5 - 3 = 2 | Subtraction (subtracts the second value from the first) |
* | 5 * 3 = 15 | Multiplication (multiplies two values) |
/ | 5 / 3 = 1.666 | Division (divides the first value by the second and returns a floating-point number) |
% | 5 % 3 = 2 | Modulus (returns the remainder of division) |
** | 5 ** 3 = 125 | Exponentiation (raises the first value to the power of the second value.) |
// | 5 // 3 = 1 | Floor Division (divides the first value by the second and returns the largest integer less than or equal to the result) |
Example:
# Arithmetic Operators in Python
a = 5
b = 3
# 1. Addition (+)
print('Addition(5 + 3) = ',a+b)
# 2. Subtraction (-)
print('Subtraction(5 - 3) = ',a-b)
# 3. Multiplication (*)
print('Multiplication(5 * 3) = ',a*b)
# 4. Division (/)
print('Division(5 / 3) = ',a/b)
# 5. Modulus (%)
print('Modulus(5 % 3) = ',a%b)
# 6. Exponentiation (**)
print('Exponentiation(5 ** 3) = ',a**b)
# 7. Floor Division (//)
print('Floor Division(5 // 3) = ',a//b)
# Arithmetic Operators in Python
a = 5
b = 3
# 1. Addition (+)
print('Addition(5 + 3) = ',a+b)
# 2. Subtraction (-)
print('Subtraction(5 - 3) = ',a-b)
# 3. Multiplication (*)
print('Multiplication(5 * 3) = ',a*b)
# 4. Division (/)
print('Division(5 / 3) = ',a/b)
# 5. Modulus (%)
print('Modulus(5 % 3) = ',a%b)
# 6. Exponentiation (**)
print('Exponentiation(5 ** 3) = ',a**b)
# 7. Floor Division (//)
print('Floor Division(5 // 3) = ',a//b)
Output
Addition(5 + 3) = 8
Subtraction(5 - 3) = 2
Multiplication(5 * 3) = 15
Division(5 / 3) = 1.6666666666666667
Modulus(5 % 3) = 2
Exponentiation(5 ** 3) = 125
Floor Division(5 // 3) = 1
Addition(5 + 3) = 8
Subtraction(5 - 3) = 2
Multiplication(5 * 3) = 15
Division(5 / 3) = 1.6666666666666667
Modulus(5 % 3) = 2
Exponentiation(5 ** 3) = 125
Floor Division(5 // 3) = 1
2. Comparison Operators
Comparison operators in Python are used to compare two values. These operators evaluate the relationship between the values and return a boolean result, either True or False.
The basic comparison operators are:
Operator | Example | Operator Name |
---|---|---|
== | 5 == 3 = False | Equal to (checks if two values are equal) |
!= | 5 != 3 = True | Not equal to (checks if two values are not equal) |
> | 5 > 3 = True | Greater than (checks if the first value is greater than the second) |
< | 5 < 3 = False | Less than (checks if the first value is less than the second) |
>= | 5 >= 3 = True | Greater than or equal to (checks if the first value is greater than or equal to the second) |
<= | 5 <= 3 = False | Less than or equal to (checks if the first value is less than or equal to the second) |
Example:
# Comparison Operators in Python
a = 5
b = 3
# 1. Equal to (==)
print('Equal to: ', a == b)
# 2. Not equal to (!=)
print('Not equal to: ', a != b)
# 3. Greater than (>)
print('Greater than: ', a > b)
# 4. Less than (<)
print('Less than: ', a < b)
# 5. Greater than or equal to (>=)
print('Greater than or equal to: ', a >= b)
# 6. Less than or equal to (<=)
print('Less than or equal to: ', a <= b)
# Comparison Operators in Python
a = 5
b = 3
# 1. Equal to (==)
print('Equal to: ', a == b)
# 2. Not equal to (!=)
print('Not equal to: ', a != b)
# 3. Greater than (>)
print('Greater than: ', a > b)
# 4. Less than (<)
print('Less than: ', a < b)
# 5. Greater than or equal to (>=)
print('Greater than or equal to: ', a >= b)
# 6. Less than or equal to (<=)
print('Less than or equal to: ', a <= b)
Output
Equal to: False
Not equal to: True
Greater than: True
Less than: False
Greater than or equal to: True
Less than or equal to: False
Equal to: False
Not equal to: True
Greater than: True
Less than: False
Greater than or equal to: True
Less than or equal to: False
3. Logical Operators
Logical operators in Python are used to combine conditional statements and evaluate multiple conditions. These operators return a boolean result, either True or False, based on the logic applied.
The basic logical operators are:
Operator | Example | Operator Name |
---|---|---|
and | (True and False) = False | Logical AND (returns True if both conditions are True) |
or | (True or False) = True | Logical OR (returns True if at least one condition is True) |
not | not True = False | Logical NOT (reverses the boolean value) |
Example:
# Logical Operators in Python
a = True
b = False
# 1. AND (and)
print('AND: ', a and b)
# 2. OR (or)
print('OR: ', a or b)
# 3. NOT (not)
print('NOT: ', not a)
# Logical Operators in Python
a = True
b = False
# 1. AND (and)
print('AND: ', a and b)
# 2. OR (or)
print('OR: ', a or b)
# 3. NOT (not)
print('NOT: ', not a)
Output
AND: False
OR: True
NOT: False
AND: False
OR: True
NOT: False
4. Assignment Operators
Assignment operators in Python are used to assign values to variables. These operators allow you to modify the value of a variable in various ways, including addition, subtraction, multiplication, etc.
The basic assignment operators are:
Operator | Description |
---|---|
= | Assignment (assigns the right-hand value to the left-hand variable). Ex : x = 5 (assigns 5 to x) |
+= | Addition Assignment (adds the right-hand value to the left-hand variable). Ex : x += 3 (equivalent to x = x + 3) |
-= | Subtraction Assignment (subtracts the right-hand value from the left-hand variable). Ex : x -= 2 (equivalent to x = x - 2) |
*= | Multiplication Assignment (multiplies the left-hand variable by the right-hand value). Ex : x *= 4 (equivalent to x = x * 4) |
/= | Division Assignment (divides the left-hand variable by the right-hand value). Ex : x /= 2 (equivalent to x = x / 2) |
%= | Modulus Assignment (takes the modulus of the left-hand variable with the right-hand value). Ex : x %= 3 (equivalent to x = x % 3) |
**= | Exponentiation Assignment (raises the left-hand variable to the power of the right-hand value). Ex : x **= 2 (equivalent to x = x ** 2) |
//= | Floor Division Assignment (performs floor division on the left-hand variable by the right-hand value). Ex : x //= 2 (equivalent to x = x // 2) |
Example:
# Assignment Operators in Python
x = 5
# 1. Assignment (=)
x = 5
print('Assignment: ', x)
# 2. Addition Assignment (+=)
x += 3
print('Addition Assignment: ', x)
# 3. Subtraction Assignment (-=)
x -= 2
print('Subtraction Assignment: ', x)
# 4. Multiplication Assignment (*=)
x *= 4
print('Multiplication Assignment: ', x)
# 5. Division Assignment (/=)
x /= 2
print('Division Assignment: ', x)
# 6. Modulus Assignment (%=)
x %= 3
print('Modulus Assignment: ', x)
# 7. Exponentiation Assignment (**=)
x **= 2
print('Exponentiation Assignment: ', x)
# 8. Floor Division Assignment (//=)
x //= 2
print('Floor Division Assignment: ', x)
# Assignment Operators in Python
x = 5
# 1. Assignment (=)
x = 5
print('Assignment: ', x)
# 2. Addition Assignment (+=)
x += 3
print('Addition Assignment: ', x)
# 3. Subtraction Assignment (-=)
x -= 2
print('Subtraction Assignment: ', x)
# 4. Multiplication Assignment (*=)
x *= 4
print('Multiplication Assignment: ', x)
# 5. Division Assignment (/=)
x /= 2
print('Division Assignment: ', x)
# 6. Modulus Assignment (%=)
x %= 3
print('Modulus Assignment: ', x)
# 7. Exponentiation Assignment (**=)
x **= 2
print('Exponentiation Assignment: ', x)
# 8. Floor Division Assignment (//=)
x //= 2
print('Floor Division Assignment: ', x)
Output
Assignment: 5
Addition Assignment: 8
Subtraction Assignment: 6
Multiplication Assignment: 24
Division Assignment: 12.0
Modulus Assignment: 0.0
Exponentiation Assignment: 0.0
Floor Division Assignment: 0.0
Assignment: 5
Addition Assignment: 8
Subtraction Assignment: 6
Multiplication Assignment: 24
Division Assignment: 12.0
Modulus Assignment: 0.0
Exponentiation Assignment: 0.0
Floor Division Assignment: 0.0
5. Identity Operators
Identity operators in Python are used to compare the memory locations of two objects. These operators allow you to check if two variables point to the same object in memory.
The identity operators are:
Operator | Example | Operator Name |
---|---|---|
is | a is b | Identity comparison (True if both variables point to the same object) |
is not | a is not b | Identity negation (True if both variables do not point to the same object) |
Example:
# Identity Operators in Python
a = 10
b = 10
c = a
# Check if a and b refer to the same object
print('a is b: ', a is b) # True, because both a and b refer to the same object in memory
# Check if a and c refer to the same object
print('a is c: ', a is c) # True, because a and c refer to the same object in memory
# Check if a and b do not refer to the same object
print('a is not b: ', a is not b) # False, because a and b refer to the same object
# Check if a and c do not refer to the same object
print('a is not c: ', a is not c) # False, because a and c refer to the same object
# Identity Operators in Python
a = 10
b = 10
c = a
# Check if a and b refer to the same object
print('a is b: ', a is b) # True, because both a and b refer to the same object in memory
# Check if a and c refer to the same object
print('a is c: ', a is c) # True, because a and c refer to the same object in memory
# Check if a and b do not refer to the same object
print('a is not b: ', a is not b) # False, because a and b refer to the same object
# Check if a and c do not refer to the same object
print('a is not c: ', a is not c) # False, because a and c refer to the same object
Output
a is b: True
a is c: True
a is not b: False
a is not c: False
a is b: True
a is c: True
a is not b: False
a is not c: False
6. Membership Operators
Membership operators in Python are used to check if a value is present in a sequence such as a string, tuple, or range. These operators allow you to verify if a value exists in a collection.
The membership operators are:
Operator | Example | Operator Name |
---|---|---|
in | 'a' in 'apple' | Membership check (True if the value exists in the sequence) |
not in | 'b' not in 'apple' | Membership negation (True if the value does not exist in the sequence) |
Example:
# Membership Operators in Python
# String example
fruit = "apple"
print("'a' in 'apple':", 'a' in fruit) # True, because 'a' is in "apple"
print("'b' in 'apple':", 'b' in fruit) # False, because 'b' is not in "apple"
print("'p' not in 'apple':", 'p' not in fruit) # False, because 'p' is in "apple"
# Membership Operators in Python
# String example
fruit = "apple"
print("'a' in 'apple':", 'a' in fruit) # True, because 'a' is in "apple"
print("'b' in 'apple':", 'b' in fruit) # False, because 'b' is not in "apple"
print("'p' not in 'apple':", 'p' not in fruit) # False, because 'p' is in "apple"
Output
'a' in 'apple': True
'b' in 'apple': False
'p' not in 'apple': False
'a' in 'apple': True
'b' in 'apple': False
'p' not in 'apple': False
7. Bitwise Operators
Bitwise operators in Python work on the binary representation of numbers. These operators allow you to manipulate the individual bits of an integer value.
The bitwise operators are:
Operator | Description |
---|---|
& | Bitwise AND (sets each bit to 1 if both bits are 1). Ex : 5 & 3 → 1 (binary 101 & 011 = 001) |
| | Bitwise OR (sets each bit to 1 if one of the bits is 1). Ex : 5 | 3 → 7 (binary 101 | 011 = 111) |
^ | Bitwise XOR (sets each bit to 1 if only one of the bits is 1). Ex : 5 ^ 3 → 6 (binary 101 ^ 011 = 110) |
~ | Bitwise NOT (inverts all the bits). Ex : ~5 → -6 (binary ~101 = 010 in two's complement) |
<< | Left Shift (shifts the bits to the left). Ex : 5 << 1 → 10 (binary 101 << 1 = 1010) |
>> | Right Shift (shifts the bits to the right). Ex : 5 >> 1 → 2 (binary 101 >> 1 = 10) |
Example:
# Bitwise Operators in Python
a = 5 # binary: 101
b = 3 # binary: 011
# Bitwise AND
print("5 & 3:", a & b) # 1 (binary 101 & 011 = 001)
# Bitwise OR
print("5 | 3:", a | b) # 7 (binary 101 | 011 = 111)
# Bitwise XOR
print("5 ^ 3:", a ^ b) # 6 (binary 101 ^ 011 = 110)
# Bitwise NOT
print("~5:", ~a) # -6 (binary ~101 = 010 (two's complement))
# Left Shift
print("5 << 1:", a << 1) # 10 (binary 101 << 1 = 1010)
# Right Shift
print("5 >> 1:", a >> 1) # 2 (binary 101 >> 1 = 10)
# Bitwise Operators in Python
a = 5 # binary: 101
b = 3 # binary: 011
# Bitwise AND
print("5 & 3:", a & b) # 1 (binary 101 & 011 = 001)
# Bitwise OR
print("5 | 3:", a | b) # 7 (binary 101 | 011 = 111)
# Bitwise XOR
print("5 ^ 3:", a ^ b) # 6 (binary 101 ^ 011 = 110)
# Bitwise NOT
print("~5:", ~a) # -6 (binary ~101 = 010 (two's complement))
# Left Shift
print("5 << 1:", a << 1) # 10 (binary 101 << 1 = 1010)
# Right Shift
print("5 >> 1:", a >> 1) # 2 (binary 101 >> 1 = 10)
Output
5 & 3: 1
5 | 3: 7
5 ^ 3: 6
~5: -6
5 << 1: 10
5 >> 1: 2
5 & 3: 1
5 | 3: 7
5 ^ 3: 6
~5: -6
5 << 1: 10
5 >> 1: 2
Frequently Asked Questions
What are operators in Python?
What are operators in Python?
Operators in Python are special symbols or keywords that perform operations on values or variables. For example, + is an arithmetic operator used to add numbers.
How many types of operators are there in Python?
How many types of operators are there in Python?
Python supports seven types of operators: Arithmetic, Assignment, Comparison, Logical, Identity, Membership, and Bitwise operators.
What is the difference between = and == in Python?
What is the difference between = and == in Python?
= is used to assign a value to a variable, while == is used to compare two values to check if they are equal.
What does the is operator check in Python?
What does the is operator check in Python?
The is operator checks whether two variables point to the same object in memory, not just if they have the same value.
Can I combine arithmetic and assignment in Python?
Can I combine arithmetic and assignment in Python?
Yes! Python allows compound assignment like +=, *=, etc. For example, x += 2 is equivalent to x = x + 2.
What does the in operator do in Python?
What does the in operator do in Python?
The in operator checks if a value exists within a sequence (like a string, list, or tuple). For example, 'a' in 'apple' returns True.
What's Next?
Next, you'll explore type conversion in Python, a key concept for working with different data types. You'll learn how to convert values between types like strings, integers, and floats, and understand when and why type conversion is necessary to avoid errors and ensure your programs run smoothly.