🟢 Beginner · Lesson 07
Python में Operators
Operators क्या हैं?
Operators वे चिह्न हैं जो values (operands) पर operation करते हैं। 5 + 3 में + operator, operands 5 और 3 पर काम करता है।
Arithmetic Operators
| Op | मतलब | Example | Result |
|---|---|---|---|
| + | जोड़ | 5 + 2 | 7 |
| - | घटाव | 5 - 2 | 3 |
| * | गुणा | 5 * 2 | 10 |
| / | भाग (float) | 5 / 2 | 2.5 |
| // | Floor भाग | 5 // 2 | 2 |
| % | शेषफल | 5 % 2 | 1 |
| ** | घात (power) | 5 ** 2 | 25 |
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 चाहिए।ortrue है अगर कोई एक तरफ true हो।notTrue को 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 == 0classic 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
- दो numbers लेकर सातों arithmetic results print करें।
andसे check करें कि number 3 और 5 दोनों से divisible है या नहीं।- Age पूछकर vote की eligibility check करें (>= 18)।
+=style operators से दो numbers swap करें।
सारांश
- Arithmetic:
+ - * / // % **। - Comparison bool लौटाता है; logical:
and or not। - even/odd के लिए
%, पूर्ण भाग के लिए//। - Precedence control के लिए brackets use करें।
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.