Learning outcomes
By the end of this chapter, you should be able to:- use
input()for user input - convert input into required data type
- take multiple values in one line
- print clean output using formatting
30-minute recording plan
0-6 min:input()fundamentals6-13 min: type conversion (int,float)13-20 min: multiple inputs withsplit()andmap()20-26 min: formatted output using f-strings26-30 min: common input mistakes and MCQ recap
Input workflow (important)
Most beginner input bugs happen because steps are skipped.- read value with
input() - convert to required type (
int,float, etc.) - process using formula/logic
- print final result clearly
Basic input
input() always returns a string.
Numeric input with conversion
int()for integersfloat()for decimals
Multiple inputs in one line
10 20.
This version expects whole numbers; if you enter decimals like 12.5, int() will raise ValueError.
Short form:
Sequence unpacking from a single string (MCQ-critical)
Python can unpack characters of a string directly:"56" has exactly 2 characters.
Same pattern with user input:
- number of variables must match number of characters
- otherwise
ValueErroroccurs
a, b = input().split()splits by spaces into words/tokensa, b = input()unpacks raw characters directly
Digit extraction trap from input (sandwich-number type)
Question style:- “Take a 3-digit number.”
- “Compare first + last with middle digit.”
intvalues are not subscriptable.- indexing like
num[0]works for strings/lists, not ints.
first,middle,lastare strings.first + lastconcatenates (for143,"1" + "3" -> "13"), not numeric sum.
Beginner alert: what map() means
This one-liner is common in exams, but it hides 3 steps:
input(...).split()gives a list of stringsmap(int, ...)converts each string to integera, b = ...unpacks the two integers into two variables
- If user enters fewer or more than 2 values: unpacking error
- If user enters non-numeric text (like
ten):ValueError - If user enters decimals while using
int(like12.5):ValueError(usefloatinstead)
Output formatting
f"..." formatting is clean and exam-safe.
The f means “formatted string”, so variables inside {} are replaced by values.
Mini showcase: simple marks report
{avg:.2f} means: show avg with exactly 2 digits after decimal.
Sample run:
- Input:
Aaravand78 84 91 - Output shows total and average with 2 decimal places.
Common mistakes
- forgetting type conversion after
input() - using
+between string and integer - expecting
input()to returnintautomatically
Exam-focused points
- default return type of
input()isstr - type conversion functions:
int(),float(),str() split()returns a list of strings- unpacking can be done from token list (
split) or directly from string characters
Practice questions
- Take length and breadth as input and print area.
- Take 3 marks and print total and average.
- Write program to input name, class, and roll number and print in one formatted line.
- Predict output:
