Site icon Rashed Hossain

Understanding Python Operators: Arithmetic, Comparison, and Logical

In Python, operators are special symbols or keywords used to perform operations on variables and values. Think of them as tools that let your code calculate, compare, or make decisions.

This guide will walk you through the three core types of basic operators in Python:

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators

Let’s explore each one with real-world scenarios and easy-to-understand examples.


🧮 1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations, just like you do on a calculator.

OperatorNameDescriptionExampleResult
+AdditionAdds two values5 + 38
-SubtractionSubtracts right value from left10 - 28
*MultiplicationMultiplies two values4 * 624
/DivisionDivides left by right (returns float)9 / 24.5
//Floor DivisionDivides and returns the whole number only9 // 24
%ModulusReturns the remainder10 % 31
**ExponentRaises to power2 ** 38

📌 Example Code:

a = 12
b = 5

print("Addition:", a + b)        # 17
print("Subtraction:", a - b)     # 7
print("Multiplication:", a * b)  # 60
print("Division:", a / b)        # 2.4
print("Floor Division:", a // b) # 2
print("Modulus:", a % b)         # 2
print("Exponent:", a ** b)       # 248832

💡 Real-life Analogy:

If you have 10 candies and give 3 to each of your 3 friends:


🔍 2. Comparison Operators

These operators are used to compare two values. The result is always either True or False. They are mostly used in if statements, loops, or condition checks.

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than8 > 6True
<Less than4 < 2False
>=Greater than or equal to7 >= 7True
<=Less than or equal to5 <= 8True

📌 Example Code:

age = 18

print(age == 18)   # True
print(age != 21)   # True
print(age > 16)    # True
print(age < 16)    # False
print(age >= 18)   # True
print(age <= 17)   # False

💡 Real-life Analogy:

If you’re checking if someone is old enough to vote:

age = 17
if age >= 18:
    print("You can vote")
else:
    print("You cannot vote yet")

🔗 3. Logical Operators

Logical operators are used to combine multiple conditions. They help in decision-making by evaluating the truthiness of multiple expressions.

OperatorMeaningUsageResult
andTrue if both conditions are trueTrue and TrueTrue
orTrue if at least one condition is trueTrue or FalseTrue
notReverses the resultnot TrueFalse

📌 Example Code:

age = 20
has_id = True

# Using 'and'
if age >= 18 and has_id:
    print("You can enter the club")  # True

# Using 'or'
if age >= 18 or has_id:
    print("You can still enter")

# Using 'not'
is_banned = False
if not is_banned:
    print("Access granted")

💡 Real-life Analogy:

Imagine a login system:


✅ Final Summary

TypeUsed ForExamples
ArithmeticMath operations+, -, *, /, %, **, //
ComparisonCheck values==, !=, <, >, <=, >=
LogicalCombine conditionsand, or, not

🧠 Practice Tips:

Let me know if you’d like me to turn this into a PDF guide or tutorial post!

Exit mobile version