Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🐍 Unit 2 – Python Basics  ·  Lesson 14

Input, Output, Type Conversion & Errors

Output with print() and f-strings

You already know print(). A neat way to mix text and values is an f-string: put f before the quotes and write variables inside { }.

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 means one digit after the decimal; <8 and >8 mean left / right align in 8 spaces — useful for printing neat tables.

Taking input with input()

input(prompt) shows the prompt, waits for the user to type something and press Enter, then returns what was typed.

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 shown'Enter your name: 'User typesKabir + EnterReturned'Kabir' (a string)Storedname
What input() does

input() always returns a string

This is the most common mistake in Class XI programs. Even if the user types 25, input gives the string '25'. Adding two such strings joins them.

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

When an expression mixes types, Python automatically converts the "smaller" type to the "bigger" one so no information is lost: bool → int → float → complex. This is called implicit conversion or 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)

The programmer converts a value on purpose using a function.

FunctionConverts toExample → 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() uses "banker's rounding": a value exactly halfway goes to the nearest even number, so round(8.5) is 8. Conversion fails if the text is not a valid number:

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

Types of errors

✏️
Syntax errorRules of Python broken – missing colon, bracket or quote. Program does not start.
💥
Runtime error (exception)Program starts but crashes on a bad operation – divide by zero, wrong type, missing name.
🤔
Logical errorProgram runs fine but the answer is wrong because the logic or formula is wrong.
Three kinds of 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 and newer add the helpful "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 is finding and removing errors (bugs).

  1. 1Read the last line of the error message first — it names the error type and reason.
  2. 2Look at the line number shown; the mistake is on that line or just above it.
  3. 3Add print() statements to see the values of variables at each step.
  4. 4Test with simple inputs whose answer you already know (e.g. 2 + 2).
  5. 5Fix one error at a time and run again.
How to debug a program

Summary & practice

  • input() returns a string — convert with int() / float().
  • Implicit conversion: automatic (int + float → float). Explicit: programmer uses int(), float(), str().
  • Errors: syntax (rules), runtime (crash), logical (wrong answer).
✅ Practice

Write a program that inputs the length and breadth of a rectangle (decimals allowed) and prints its area and perimeter to 2 decimal places. Then find the error type: (a) print('Hi' (b) x = int('ten') (c) percentage = marks / 500 * 10.

Answers: (a) syntax error (b) runtime – ValueError (c) logical error (should be × 100).

Frequently Asked Questions

Why does input() return a string?
Because whatever is typed on the keyboard is text. Python cannot guess whether 25 is a number or part of a name, so we convert it with int() or float() when we need a number.
What is the difference between implicit and explicit conversion?
Implicit conversion is done automatically by Python, like int + float giving float. Explicit conversion (type casting) is done by the programmer using functions such as int(), float() and str().
Which error is hardest to find?
A logical error, because the program runs without any message but gives a wrong answer. You find it by testing with known inputs and checking the output.
← Class 11 Computer Science
🔗

Share this topic with a friend

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

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

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

Next topicif, elif, else (Selection) Your progress: 14 / 29 (48%) · Lesson 15 / 29 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free