Operators, Input-Output और Debugging
Python में operators
Operator एक symbol है जो Python को operands नाम की values पर calculation या comparison करने को कहता है। Shops, banks और schools रोज़ इन्हें use करते हैं — GST जोड़ना, marks compare करना, eligibility जाँचना।
Arithmetic operators
price, gst = 1250, 18
tax = price * gst / 100
print('GST =', tax, '| Total =', price + tax)
print(29 // 4, 29 % 4, 2 ** 10, 9 / 2)
minutes = 135
print(minutes // 60, 'h', minutes % 60, 'min')| Operator | नाम | Example | Result |
|---|---|---|---|
| + | जोड़ | 1250 + 225 | 1475 |
| - | घटाव | 50 - 12 | 38 |
| * | गुणा | 45.5 * 3 | 136.5 |
| / | भाग | 9 / 2 | 4.5 |
| // | Floor division | 29 // 4 | 7 |
| % | Modulus (शेषफल) | 29 % 4 | 1 |
| ** | घात | 2 ** 10 | 1024 |
Relational, logical और membership operators
Relational operators values compare करके True या False देते हैं। Logical operators conditions जोड़ते हैं। Membership operators जाँचते हैं कि value string, list या दूसरे collection में है या नहीं।
marks, attendance = 72, 68
print(marks >= 33, attendance >= 75)
eligible = marks >= 33 and attendance >= 75
print('Eligible for exam:', eligible)
print(10 > 3 or 5 < 2, not 10 > 3)
print('IP' in 'Informatics Practices', 'ip' in 'Informatics Practices')ध्यान दें 'IP' in 'Informatics Practices' False है — capital "IP" साथ में नहीं आते; membership case-sensitive है।
Assignment operators
stock = 50
stock -= 12 # 12 sold
stock += 30 # 30 arrived
stock *= 2 # second branch has the same
print('Stock =', stock)Expressions और precedence
Expression operands और operators का मेल है जो एक value देता है। कई operators हों तो Python precedence मानता है: पहले brackets, फिर **, फिर * / // %, फिर + -, फिर comparisons, फिर not, and, or। एक ही level के operators बाएँ से दाएँ चलते हैं, सिवाय ** के जो दाएँ से बाएँ।
print(5 + 3 * 2 - 8 / 4) print((5 + 3) * 2) print(2 ** 2 ** 3) print(15 - 6 // 4)
Input और output
input(prompt) user का type किया text पढ़ता है। print() values दिखाता है; spacing के लिए sep और end use करें।
name = input('Customer name: ')
amount = float(input('Purchase amount: '))
if amount > 2000:
discount = amount * 10 / 100
else:
discount = amount * 5 / 100
print('Dear', name + ', you pay Rs.', amount - discount)Type conversion
Implicit conversion अपने आप होता है (int + float → float)। Explicit conversion (type casting) में int(), float(), str(), bool() use होते हैं।
a = '5'
print(a * 3, int(a) * 3) # string repeat vs number multiply
print(int(9.99), float('12'), str(3.0), int('0012'))
print(round(1234.5678, 2), round(1234.5678, -2))c = float(input('Temperature in Celsius: '))
f = c * 9 / 5 + 32
print('Fahrenheit =', round(f, 2))Errors और debugging
Runtime error – program रुक जाता है
- Zero से भाग
- ऐसा variable use करना जो बना ही नहीं
- "abc" को int() से बदलना
- Python error का नाम और line दिखाता है
Logical error – ग़लत answer
- Program बिना message के ख़त्म
- ग़लत formula या भूला हुआ conversion
- Output जाँचकर ही पता चलता है
- ढूँढना सबसे मुश्किल
total = 450
items = 0
print('Average price:', total / items)Classic logical error — input convert करना भूल जाना। User 3 type करता है, "total" 120 होना चाहिए, पर string "3" 40 बार दोहराई जाती है:
qty = input('Quantity: ')
print('Total:', qty * 40) # bug: qty is a string!Fix: qty = int(input('Quantity: '))। Variables और उनके types print करके debug करें: print(qty, type(qty))।
सारांश और practice
- Arithmetic: + − * / // % **; relational True/False देते हैं; logical: and, or, not।
- Precedence: () → ** → * / // % → + − → comparisons → not → and → or।
input()str देता है; int()/float() से बदलें।- Errors: syntax, runtime, logical।
1) 20 - 3 ** 2 // 4 * 2 evaluate करें। 2) Cost price और selling price input करके profit या loss की रकम और percentage print करने वाला program लिखें। 3) Seconds को hours, minutes और seconds में बदलने वाला program लिखें।
Answer 1: 3 ** 2 = 9 → 9 // 4 = 2 → 2 * 2 = 4 → 20 − 4 = 16।
