LEARN COMPLETE PYTHON IN 24 HOURS

3. Operators in Python

Operators are special symbols that perform operations on values (called operands). Python has many built-in operators that make calculations, comparisons, and logic super easy.

Let’s learn all types one by one with real examples.

3.1 Arithmetic Operators (+, -, , /, //, %, *)

These are used for mathematical calculations.

OperatorNameExampleResultDescription+Addition10 + 515Adds two numbers-Subtraction10 - 55Subtracts*Multiplication10 550Multiplies/Division10 / 33.333...Always returns float//Floor Division10 // 33Returns integer (removes decimal)%Modulo (Remainder)10 % 31Returns remainder*Exponent (Power)2 ** 382 to the power of 3

Practical Examples:

Python

a = 15 b = 4 print(a + b) # 19 print(a - b) # 11 print(a b) # 60 print(a / b) # 3.75 print(a // b) # 3 print(a % b) # 3 print(a * b) # 50625

Real-life Tip: Use % to check even/odd:

Python

num = 25 print("Even" if num % 2 == 0 else "Odd") # Odd

3.2 Comparison Operators (==, !=, >, <, >=, <=)

These compare two values and return True or False.

OperatorNameExampleResult==Equal to5 == 5True!=Not equal to5 != 3True>Greater than10 > 5True<Less than10 < 5False>=Greater than or equal10 >= 10True<=Less than or equal5 <= 10True

Examples:

Python

age = 20 print(age == 18) # False print(age != 18) # True print(age > 18) # True print(age >= 20) # True print(age < 18) # False

Tip: These are used in if conditions (we will learn in Control Flow).

3.3 Logical Operators (and, or, not)

These combine multiple conditions.

OperatorDescriptionExampleandTrue only if both conditions are True(5>3 and 10>8) → TrueorTrue if at least one is True(5>10 or 10>8) → TruenotReverses the result (True becomes False)not (5>10) → True

Examples:

Python

age = 25 has_license = True print(age > 18 and has_license) # True → can drive print(age > 18 or has_license) # True print(not has_license) # False

Real-life Example:

Python

is_raining = True have_umbrella = False print("Go outside?" if not is_raining or have_umbrella else "Stay home") # Output: Stay home

3.4 Assignment Operators (=, +=, -=, *=, etc.)

Used to assign and update values in one step.

OperatorExampleSame as=x = 10x = 10+=x += 5x = x + 5-=x -= 3x = x - 3*=x = 2x = x 2/=x /= 4x = x / 4//=x //= 2x = x // 2%=x %= 3x = x % 3**=x = 2x = x 2

Live Example:

Python

score = 100 score += 50 # score = 150 score -= 20 # score = 130 score *= 2 # score = 260 score /= 2 # score = 130.0 print("Final score:", score)

3.5 Membership Operators (in, not in)

Check if a value exists in a sequence (string, list, tuple, etc.).

Python

fruits = ["apple", "banana", "mango"] name = "Anshuman" print("apple" in fruits) # True print("grape" in fruits) # False print("grape" not in fruits) # True print("a" in name) # True print("z" not in name) # True

3.6 Identity Operators (is, is not)

Check if two variables point to the same object in memory (not just equal value).

Python

x = 1000 y = 1000 a = [1, 2, 3] b = [1, 2, 3] print(x == y) # True (same value) print(x is y) # False (different objects) print(a == b) # True print(a is b) # False print(x is not y) # True

Important Note: Use == for value comparison and is only for identity (mostly with None, True, False).

Example with None:

Python

result = None print(result is None) # True (correct way)

This completes the full Operators in Python section — super important for all future topics!