Paramclasses Academy - Blog

Tutorials Details

←
Post 3 of 20
→

Python Operators Explained: Arithmetic, Comparison, Logical, Assignment & More

Python Operators Explained: Arithmetic, Comparison, Logical, Assignment & More

October 16, 2026

Python Operators Explained: Arithmetic, Comparison, Logical, Assignment & More

Introduction to Python Operators

Python operators are special symbols and keywords used to perform operations on variables and values. They help developers perform mathematical calculations, compare values, combine conditions, assign data, and make decisions in Python programs.

In this tutorial, you will learn the different types of Python operators with simple explanations, practical examples, and code snippets suitable for beginners.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Description Example Result
+ Addition 10 + 3 13
- Subtraction 10 - 3 7
* Multiplication 10 * 3 30
/ Division 10 / 3 3.333...
// Floor division 10 // 3 3
% Modulus (remainder) 10 % 3 1
** Exponentiation 2 ** 3 8

Example:

a = 10
b = 3

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Remainder:", a % b)
print("Power:", 2 ** 3)

2. Comparison Operators

Comparison operators compare two values and return either True or False.

Operator Description Example
== Equal to 10 == 10
!= Not equal to 10 != 5
> Greater than 10 > 5
< Less than 5 < 10
>= Greater than or equal to 10 >= 10
<= Less than or equal to 5 <= 10

Example:

a = 10
b = 5

print(a == b)  # False
print(a != b)  # True
print(a > b)   # True
print(a < b)   # False
print(a >= 10) # True
print(b <= 10) # True

Comparison operators are useful for checking conditions, validating data, and making decisions in applications.

3. Logical Operators

Logical operators combine conditions or reverse a Boolean result.

Python has three main logical operators:

  • and: Returns True when both conditions are true.

  • or: Returns True when at least one condition is true.

  • not: Reverses the Boolean result.

Example:

age = 22
has_id = True

print(age >= 18 and has_id)
print(age >= 18 or has_id)
print(not has_id)

Output:

True
True
False

Logical operators are commonly used in login validation, eligibility checks, access control, and other decision-making tasks.

4. Assignment Operators

Assignment operators assign values to variables or update their existing values.

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

Example:

score = 10

score += 5
print(score)  # 15

score *= 2
print(score)  # 30

score -= 10
print(score)  # 20

These operators make it easier to update counters, scores, totals, and other variable values.

5. Bitwise Operators

Bitwise operators work with the binary representation of integers.

Operator Name
& AND
| OR
^ XOR
~ NOT
<< Left shift
>> Right shift

Example:

a = 5  # Binary: 0101
b = 3  # Binary: 0011

print(a & b)  # 1
print(a | b)  # 7
print(a ^ b)  # 6
print(a << 1) # 10
print(a >> 1) # 2

Bitwise operators are useful in low-level programming, binary data processing, and certain performance-sensitive operations.

6. Membership Operators

Membership operators check whether a value exists in a sequence or collection.

Python provides two membership operators:

  • in: Returns True if the value is present.

  • not in: Returns True if the value is absent.

Example:

languages = ["Python", "PHP", "JavaScript"]

print("Python" in languages)      # True
print("Java" not in languages)    # True

Membership operators are useful for checking whether a list contains an item or whether a string contains specific text.

7. Identity Operators

Identity operators check whether two references point to the same object in memory.

  • is: Checks object identity.

  • is not: Checks that two references do not point to the same object.

Example:

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

print(a is b)  # True
print(a is c)  # False
print(a == c)  # True

Important: The == operator compares values, while is checks object identity. Use is None when checking whether a variable is None.

8. Operator Precedence in Python

Operator precedence determines the order in which Python evaluates an expression. Parentheses can be used to make the intended order clear.

Example:

result = 10 + 5 * 2
print(result)  # 20

result = (10 + 5) * 2
print(result)  # 30

In the first expression, multiplication happens before addition. In the second expression, parentheses cause addition to happen first.

9. Practical Example: Calculate a Total and Check Eligibility

The following program combines arithmetic, comparison, logical, and assignment operators.

price = 500
quantity = 3
discount = 100

total = price * quantity
total -= discount

print("Final Amount:", total)

is_eligible = total >= 1000 and quantity >= 2
print("Eligible for Offer:", is_eligible)

Output:

Final Amount: 1400
Eligible for Offer: True

This example demonstrates how operators can calculate a total amount and evaluate whether a customer qualifies for an offer.

10. Practice Questions

Try answering these questions to test your understanding of Python operators.

  1. What is the difference between / and //?

  2. What is the difference between = and ==?

  3. What does the % operator return?

  4. How do and and or differ?

  5. What is the difference between is and ==?

  6. Which operator checks whether a value exists in a list?

  7. What is operator precedence?

  8. Write a program that checks whether a number is greater than 10 and less than 100.

Conclusion

Python operators are essential for performing calculations, comparing values, combining conditions, updating variables, and checking data. Understanding arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators will help you write clearer and more effective Python programs.

Continue practising these examples and experiment with different values to strengthen your Python programming skills.

 

←
Post 3 of 20
→