Operators in Python: Types and Examples for Beginners

← PrevNext →

Whether you are a beginner or an aspiring developer, understanding Python operators is an important step in learning programming. Operators allow you to perform calculations, compare values, combine conditions, and manipulate data. In this guide, we will explore the different types of operators in Python with simple explanations and practical examples.

What Are Operators in Python?

Operators are special symbols or keywords used to perform operations on values and variables.

For example, when you add two numbers using the + symbol, you are using an arithmetic operator.

a = 10
b = 20
result = a + b
print(result)

Output:

30

Python supports several types of operators. Let's understand them one by one.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, and division.

Operator Meaning Example
+Addition10 + 5 = 15
-Subtraction10 - 5 = 5
*Multiplication10 * 5 = 50
/Division10 / 5 = 2.0
%Modulus10 % 3 = 1
//Floor Division10 // 3 = 3
**Exponentiation2 ** 3 = 8

Example:

a = 10
b = 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)
print(a // b)
print(a ** b)

2. Comparison Operators

Comparison operators are used to compare two values. They return either True or False depending on the comparison result.

Operator Meaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example:

a = 10
b = 20

print(a == b)
print(a != b)
print(a < b)
print(a > b)

Output:

False
True
True
False

3. Logical Operators

Logical operators are used to combine multiple conditions and evaluate Boolean expressions.

Python has three logical operators:

Example:

age = 25

print(age > 18 and age < 60)
print(age < 18 or age > 60)
print(not(age > 18))

4. Assignment Operators

Assignment operators are used to assign values to variables and update their existing values.

Operator Example Equivalent
=x = 10x = 10
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 2x = x * 2
/=x /= 2x = x / 2
%=x %= 3x = x % 3

Example:

x = 10
x += 5

print(x)

Output:

15

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of integers. They are commonly used in low-level programming, bit manipulation, and certain optimization tasks.

Operator Meaning
&AND
|OR
^XOR
~NOT
<<Left Shift
>>Right Shift

Example:

a = 5
b = 3

print(a & b)
print(a | b)
print(a ^ b)

6. Membership Operators

Membership operators check whether a particular value exists in a sequence, such as a string, list, or tuple.

Python provides two membership operators:

Example:

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

print("apple" in fruits)
print("orange" not in fruits)

Output:

True
True

7. Identity Operators

Identity operators check whether two variables refer to the same object in memory. They are different from comparison operators, which check whether two values are equal.

Python provides two identity operators:

Example:

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

print(a is b)
print(a is c)

Output:

True
False

Although a and c contain the same values, they are different list objects in memory. However, b refers to the same object as a.

Conclusion

Operators are essential in Python programming because they allow developers to perform calculations, compare values, combine conditions, assign values, and manipulate data.

Python provides seven main categories of operators: arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators. Understanding these operators will help you write better programs, understand Python code more easily, and solve programming problems efficiently.

← PreviousNext →