Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🐍 Unit 2 – Python Basics  ·  Lesson 14

Input, Output, Type Conversion और Errors

print() और f-strings से output

print() आप जानते हैं। Text और values मिलाने का आसान तरीका f-string है: quotes से पहले f लगाएँ और variables { } में लिखें।

Python
name, marks = 'Riya', 91.456
print(f'{name} scored {marks:.1f} marks')
print(f'{name:<8}|{marks:>8.2f}|')
Riya scored 91.5 marks Riya | 91.46|

:.1f मतलब decimal के बाद एक digit; <8 और >8 मतलब 8 spaces में left / right align — साफ़ tables print करने में काम का।

input() से input लेना

input(prompt) prompt दिखाता है, user के type करके Enter दबाने का इंतज़ार करता है, फिर type किया हुआ return करता है।

Python
name = input('Enter your name: ')
city = input('Enter your city: ')
print('Hello', name, 'from', city + '!')
Enter your name: Kabir Enter your city: Jaipur Hello Kabir from Jaipur!
Prompt दिखा'Enter your name: 'User type करता हैKabir + EnterReturn हुआ'Kabir' (string)Storename
input() क्या करता है

input() हमेशा string देता है

Class XI programs की यह सबसे आम ग़लती है। User 25 type करे तब भी input string '25' देता है। ऐसी दो strings जोड़ने पर वे जुड़ जाती हैं।

Python
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 added
First number: 25 Second number: 17 Without conversion: 2517 With int(): 42
Python
price = float(input('Price of one pen: '))
qty = int(input('Quantity: '))
bill = price * qty
print('Bill =', round(bill, 2))
print('Bill = Rs.', format(bill, '.2f'))
Price of one pen: 12.5 Quantity: 7 Bill = 87.5 Bill = Rs. 87.50

Implicit type conversion

Expression में अलग types हों तो Python अपने आप "छोटे" type को "बड़े" में बदल देता है ताकि जानकारी न खोए: bool → int → float → complex। इसे implicit conversion या type coercion कहते हैं।

Python
print(5 + 2.0, type(5 + 2.0))   # int is promoted to float
print(True + 3.5)               # bool -> float
7.0 <class 'float'> 4.5

Explicit type conversion (type casting)

Programmer जानबूझकर function से value बदलता है।

Functionकिसमें बदलता हैExample → result
int(x)Integerint('45') → 45, int(9.99) → 9
float(x)Floatfloat('2.5') → 2.5, float(3) → 3.0
str(x)Stringstr(99) → '99'
bool(x)True / Falsebool(0) → False
list(x), tuple(x)List / tuplelist('hi') → ['h', 'i']
chr(n), ord(c)Character ↔ codechr(65) → 'A'
Python
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))
30 7.0 100 7 -7 8 8 3.14 ['a', 'b', 'c'] (1, 2) True

round() "banker's rounding" use करता है: ठीक आधी value सबसे पास के even number पर जाती है, इसलिए round(8.5) = 8। Text valid number न हो तो conversion fail होता है:

Python
age = int('16 years')
ValueError: invalid literal for int() with base 10: '16 years'

Errors के प्रकार

✏️
Syntax errorPython के rules टूटे – colon, bracket या quote छूटा। Program शुरू ही नहीं होता।
💥
Runtime error (exception)Program शुरू होता है पर ग़लत operation पर crash – zero से भाग, ग़लत type, नाम नहीं मिला।
🤔
Logical errorProgram ठीक चलता है पर logic या formula ग़लत होने से answer ग़लत।
तीन तरह की errors

Syntax error

Python
if 5 > 3
    print('yes')
SyntaxError: expected ':'

Runtime errors

Python
print('Total: ' + 50)
TypeError: can only concatenate str (not "int") to str
Python
print(10 / 0)
ZeroDivisionError: division by zero
Python
marks = 80
print(Marks)
NameError: name 'Marks' is not defined. Did you mean: 'marks'?

(Python 3.10 और नए versions मददगार "Did you mean" hint भी देते हैं।)

Logical error

Python
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)
Wrong average = 193.33333333333334 Correct average = 80.0

Debugging tips

Debugging यानी errors (bugs) ढूँढकर हटाना।

  1. 1Error message की आख़िरी line पहले पढ़ें — उसमें error type और कारण होता है।
  2. 2दिखाया गया line number देखें; ग़लती उसी line पर या ठीक ऊपर होती है।
  3. 3हर step पर variables की values देखने के लिए print() statements जोड़ें।
  4. 4ऐसे simple inputs से test करें जिनका answer आपको पता है (जैसे 2 + 2)।
  5. 5एक बार में एक error ठीक करें और फिर चलाएँ।
Program debug कैसे करें

सारांश और practice

  • input() string देता है — int() / float() से बदलें।
  • Implicit conversion: अपने आप (int + float → float)। Explicit: programmer int(), float(), str() use करता है।
  • Errors: syntax (rules), runtime (crash), logical (ग़लत answer)।
✅ Practice

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 होना चाहिए)।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

input() string क्यों return करता है?
क्योंकि keyboard पर जो भी type होता है वह text है। Python अंदाज़ा नहीं लगा सकता कि 25 number है या किसी नाम का हिस्सा, इसलिए number चाहिए तो int() या float() से बदलते हैं।
Implicit और explicit conversion में क्या फ़र्क है?
Implicit conversion Python अपने आप करता है, जैसे int + float से float। Explicit conversion (type casting) programmer int(), float() और str() जैसे functions से करता है।
कौन सी error ढूँढना सबसे मुश्किल है?
Logical error, क्योंकि program बिना किसी message के चलता है पर answer ग़लत देता है। इसे known inputs से test करके और output check करके ढूँढते हैं।
← Class 11 Computer Science
🔗

Share this topic with a friend

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

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

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

अगला topicif, elif, else (Selection) आपकी प्रगति: 14 / 29 (48%) · Lesson 15 / 29 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें