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 { }.
name, marks = 'Riya', 91.456
print(f'{name} scored {marks:.1f} marks')
print(f'{name:<8}|{marks:>8.2f}|'):.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.
name = input('Enter your name: ')
city = input('Enter your city: ')
print('Hello', name, 'from', city + '!')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.
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
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.
print(5 + 2.0, type(5 + 2.0)) # int is promoted to float print(True + 3.5) # bool -> float
Explicit type conversion (type casting)
The programmer converts a value on purpose using a function.
| Function | Converts to | 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() 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:
age = int('16 years')Types of errors
Syntax error
if 5 > 3
print('yes')Runtime errors
print('Total: ' + 50)print(10 / 0)
marks = 80 print(Marks)
(Python 3.10 and newer add the helpful "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 is finding and removing errors (bugs).
- 1Read the last line of the error message first — it names the error type and reason.
- 2Look at the line number shown; the mistake is on that line or just above it.
- 3Add
print()statements to see the values of variables at each step. - 4Test with simple inputs whose answer you already know (e.g. 2 + 2).
- 5Fix one error at a time and run again.
Summary & practice
input()returns a string — convert withint()/float().- Implicit conversion: automatic (int + float → float). Explicit: programmer uses int(), float(), str().
- Errors: syntax (rules), runtime (crash), logical (wrong answer).
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).
