Input, Output, Type Conversion और Errors
print() और f-strings से output
print() आप जानते हैं। Text और values मिलाने का आसान तरीका f-string है: quotes से पहले f लगाएँ और variables { } में लिखें।
name, marks = 'Riya', 91.456
print(f'{name} scored {marks:.1f} marks')
print(f'{name:<8}|{marks:>8.2f}|'):.1f मतलब decimal के बाद एक digit; <8 और >8 मतलब 8 spaces में left / right align — साफ़ tables print करने में काम का।
input() से input लेना
input(prompt) prompt दिखाता है, user के type करके Enter दबाने का इंतज़ार करता है, फिर type किया हुआ return करता है।
name = input('Enter your name: ')
city = input('Enter your city: ')
print('Hello', name, 'from', city + '!')input() हमेशा string देता है
Class XI programs की यह सबसे आम ग़लती है। User 25 type करे तब भी input string '25' देता है। ऐसी दो strings जोड़ने पर वे जुड़ जाती हैं।
a = input('First number: ')
b = input('Second number: ')
print('Without conversion:', a + b) # strings are joined
print('With int():', int(a) + int(b)) # numbers are addedprice = float(input('Price of one pen: '))
qty = int(input('Quantity: '))
bill = price * qty
print('Bill =', round(bill, 2))
print('Bill = Rs.', format(bill, '.2f'))Implicit type conversion
Expression में अलग types हों तो Python अपने आप "छोटे" type को "बड़े" में बदल देता है ताकि जानकारी न खोए: bool → int → float → complex। इसे implicit conversion या type coercion कहते हैं।
print(5 + 2.0, type(5 + 2.0)) # int is promoted to float print(True + 3.5) # bool -> float
Explicit type conversion (type casting)
Programmer जानबूझकर function से value बदलता है।
| Function | किसमें बदलता है | Example → result |
|---|---|---|
int(x) | Integer | int('45') → 45, int(9.99) → 9 |
float(x) | Float | float('2.5') → 2.5, float(3) → 3.0 |
str(x) | String | str(99) → '99' |
bool(x) | True / False | bool(0) → False |
list(x), tuple(x) | List / tuple | list('hi') → ['h', 'i'] |
chr(n), ord(c) | Character ↔ code | chr(65) → 'A' |
print(int('25') + 5, float('3.5') * 2, str(10) + '0')
print(int(7.9), int(-7.9)) # int() cuts off the decimal part
print(round(7.5), round(8.5), round(3.14159, 2))
print(list('abc'), tuple([1, 2]), bool(5))round() "banker's rounding" use करता है: ठीक आधी value सबसे पास के even number पर जाती है, इसलिए round(8.5) = 8। Text valid number न हो तो conversion fail होता है:
age = int('16 years')Errors के प्रकार
Syntax error
if 5 > 3
print('yes')Runtime errors
print('Total: ' + 50)print(10 / 0)
marks = 80 print(Marks)
(Python 3.10 और नए versions मददगार "Did you mean" hint भी देते हैं।)
Logical error
a, b, c = 80, 90, 70
average = a + b + c / 3 # LOGICAL error: wrong formula
print('Wrong average =', average)
average = (a + b + c) / 3 # fixed with brackets
print('Correct average =', average)Debugging tips
Debugging यानी errors (bugs) ढूँढकर हटाना।
- 1Error message की आख़िरी line पहले पढ़ें — उसमें error type और कारण होता है।
- 2दिखाया गया line number देखें; ग़लती उसी line पर या ठीक ऊपर होती है।
- 3हर step पर variables की values देखने के लिए
print()statements जोड़ें। - 4ऐसे simple inputs से test करें जिनका answer आपको पता है (जैसे 2 + 2)।
- 5एक बार में एक error ठीक करें और फिर चलाएँ।
सारांश और practice
input()string देता है —int()/float()से बदलें।- Implicit conversion: अपने आप (int + float → float)। Explicit: programmer int(), float(), str() use करता है।
- Errors: syntax (rules), runtime (crash), logical (ग़लत answer)।
Program लिखें जो rectangle की length और breadth (decimal भी) input करे और area और perimeter 2 decimal places तक print करे। फिर error type बताइए: (a) print('Hi' (b) x = int('ten') (c) percentage = marks / 500 * 10।
Answers: (a) syntax error (b) runtime – ValueError (c) logical error (× 100 होना चाहिए)।
