🟢 Beginner  ·  Lesson 07

Operators in Python

Python में Operators

What are Operators?

Operators are symbols that perform operations on values (called operands). 5 + 3 uses the + operator on operands 5 and 3.

Arithmetic Operators

OpMeaningExampleResult
+Add5 + 27
-Subtract5 - 23
*Multiply5 * 210
/Divide (float)5 / 22.5
//Floor divide5 // 22
%Modulus (remainder)5 % 21
**Power5 ** 225

Comparison Operators

Return True or False: == (equal), != (not equal), >, <, >=, <=.

Logical Operators

and (both true), or (at least one true), not (reverse).

Assignment Operators

= assigns. Shortcuts: +=, -=, *=, /=. So x += 5 means x = x + 5.

Program 1: Arithmetic

Python – arithmetic.py
a = 17
b = 5
print("Sum      :", a + b)
print("Quotient :", a // b)
print("Remainder:", a % b)
print("Power    :", a ** 2)
Sum : 22 Quotient : 3 Remainder: 2 Power : 289
  • // gives the whole-number part of division (3, not 3.4).
  • % gives the remainder (17 = 5*3 + 2).
  • ** is power: 17 squared = 289.

Program 2: Comparison & Logical

Python – logic.py
age = 20
has_id = True
print(age >= 18)               # comparison
print(age >= 18 and has_id)    # both must be true
print(age < 13 or has_id)      # one true is enough
print(not has_id)              # reverse
True True True False
  • and needs both sides true.
  • or is true if any side is true.
  • not flips True to False.

Program 3: Even/Odd & Leap Year

Python – checks.py
num = int(input("Enter a number: "))

# Even/Odd using modulus
if num % 2 == 0:
    print(num, "is Even")
else:
    print(num, "is Odd")

# Leap year check using logical operators
year = int(input("Enter a year: "))
is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
print(year, "leap year?", is_leap)
Enter a number: 7 7 is Odd Enter a year: 2024 2024 leap year? True
  • num % 2 == 0 is the classic even-number test.
  • The leap-year rule combines and, or and != in one expression.

Operator Precedence

Python follows BODMAS-like rules: ** first, then * / // %, then + -, then comparisons, then not, and, or. Use brackets to be safe: (a + b) * c.

Common Mistakes

  • Using = (assign) instead of == (compare) in conditions.
  • Expecting / to give a whole number — use // for that.
  • Writing &&/|| (C style) instead of and/or.

Practice Tasks

  1. Take two numbers and print all 7 arithmetic results.
  2. Check if a number is divisible by both 3 and 5 using and.
  3. Ask age and check eligibility to vote (>= 18).
  4. Swap two numbers using += style operators.

Summary

  • Arithmetic: + - * / // % **.
  • Comparison returns bool; logical: and or not.
  • % for even/odd, // for whole-number division.
  • Use brackets to control precedence.

Operators क्या हैं?

Operators वे चिह्न हैं जो values (operands) पर operation करते हैं। 5 + 3 में + operator, operands 5 और 3 पर काम करता है।

Arithmetic Operators

OpमतलबExampleResult
+जोड़5 + 27
-घटाव5 - 23
*गुणा5 * 210
/भाग (float)5 / 22.5
//Floor भाग5 // 22
%शेषफल5 % 21
**घात (power)5 ** 225

Comparison Operators

True या False लौटाते हैं: == (बराबर), != (बराबर नहीं), >, <, >=, <=

Logical Operators

and (दोनों true), or (कम से कम एक true), not (उल्टा)।

Assignment Operators

= assign करता है। Shortcuts: +=, -=, *=, /=। यानी x += 5 का मतलब x = x + 5

Program 1: Arithmetic

Python – arithmetic.py
a = 17
b = 5
print("Sum      :", a + b)
print("Quotient :", a // b)
print("Remainder:", a % b)
print("Power    :", a ** 2)
Sum : 22 Quotient : 3 Remainder: 2 Power : 289
  • // भाग का पूर्ण भाग देता है (3, न कि 3.4)।
  • % शेषफल देता है (17 = 5*3 + 2)।
  • ** power है: 17 का वर्ग = 289।

Program 2: Comparison & Logical

Python – logic.py
age = 20
has_id = True
print(age >= 18)               # comparison
print(age >= 18 and has_id)    # दोनों true हों
print(age < 13 or has_id)      # एक true काफी
print(not has_id)              # उल्टा
True True True False
  • and को दोनों तरफ true चाहिए।
  • or true है अगर कोई एक तरफ true हो।
  • not True को False में बदल देता है।

Program 3: Even/Odd और Leap Year

Python – checks.py
num = int(input("Enter a number: "))

# Modulus से Even/Odd
if num % 2 == 0:
    print(num, "is Even")
else:
    print(num, "is Odd")

# Logical operators से Leap year
year = int(input("Enter a year: "))
is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
print(year, "leap year?", is_leap)
Enter a number: 7 7 is Odd Enter a year: 2024 2024 leap year? True
  • num % 2 == 0 classic even-number test है।
  • Leap-year नियम एक ही expression में and, or और != जोड़ता है।

Operator Precedence

Python BODMAS जैसे नियम follow करता है: पहले **, फिर * / // %, फिर + -, फिर comparisons, फिर not, and, or। सुरक्षित रहने के लिए brackets use करें: (a + b) * c

सामान्य गलतियाँ

  • conditions में == की जगह = लगाना।
  • / से पूर्ण संख्या की उम्मीद करना — उसके लिए // use करें।
  • and/or की जगह &&/|| (C style) लिखना।

Practice Tasks

  1. दो numbers लेकर सातों arithmetic results print करें।
  2. and से check करें कि number 3 और 5 दोनों से divisible है या नहीं।
  3. Age पूछकर vote की eligibility check करें (>= 18)।
  4. += style operators से दो numbers swap करें।

सारांश

  • Arithmetic: + - * / // % **
  • Comparison bool लौटाता है; logical: and or not
  • even/odd के लिए %, पूर्ण भाग के लिए //
  • Precedence control के लिए brackets use करें।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

WhatsApp