🟢 Beginner  ·  Lesson 07

Python में Operators

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.

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.