Skip to main content

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() fundamentals
  • 6-13 min: type conversion (int, float)
  • 13-20 min: multiple inputs with split() and map()
  • 20-26 min: formatted output using f-strings
  • 26-30 min: common input mistakes and MCQ recap

Input workflow (important)

Most beginner input bugs happen because steps are skipped.
  1. read value with input()
  2. convert to required type (int, float, etc.)
  3. process using formula/logic
  4. print final result clearly

Basic input

name = input("Enter your name: ")
print("Hello", name)
Important: input() always returns a string.

Numeric input with conversion

age = int(input("Enter age: "))
height = float(input("Enter height in meters: "))
Use:
  • int() for integers
  • float() for decimals
If you skip conversion, numeric operations may fail or give wrong output.

Multiple inputs in one line

a, b = input("Enter two numbers: ").split()
a = int(a)
b = int(b)
print(a + b)
Enter values separated by a space, for example: 10 20. This version expects whole numbers; if you enter decimals like 12.5, int() will raise ValueError. Short form:
a, b = map(int, input("Enter two numbers: ").split())
print(a + b)

Sequence unpacking from a single string (MCQ-critical)

Python can unpack characters of a string directly:
a, b = "56"
print(a, b)  # 5 6
This works because string "56" has exactly 2 characters. Same pattern with user input:
a, b = input("Enter exactly 2 characters: ")
Rules:
  • number of variables must match number of characters
  • otherwise ValueError occurs
Examples:
# a, b = "5"      # ValueError (not enough values)
# a, b = "567"    # ValueError (too many values)
Important difference:
  • a, b = input().split() splits by spaces into words/tokens
  • a, 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.”
Common wrong implementation:
num = int(input())
first, middle, last = num[0], num[1], num[2]  # wrong
Why wrong:
  • int values are not subscriptable.
  • indexing like num[0] works for strings/lists, not ints.
Another wrong pattern:
num = input()
first, middle, last = num[0], num[1], num[2]
if first + last == middle:
    print("sandwich")
Why wrong:
  • first, middle, last are strings.
  • first + last concatenates (for 143, "1" + "3" -> "13"), not numeric sum.
Correct pattern:
num = input().strip()        # e.g., "143"
first, middle, last = map(int, num)

if first + last == middle:
    print("sandwich")
else:
    print("plain")

Beginner alert: what map() means

This one-liner is common in exams, but it hides 3 steps:
  1. input(...).split() gives a list of strings
  2. map(int, ...) converts each string to integer
  3. a, b = ... unpacks the two integers into two variables
Same logic in expanded form:
parts = input("Enter two numbers: ").split()  # ['10', '20']
a = int(parts[0])
b = int(parts[1])
print(a + b)
When can the one-liner fail?
  • 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 (like 12.5): ValueError (use float instead)

Output formatting

name = "Dhruv"
marks = 87
print(f"{name} scored {marks} marks")
f"..." formatting is clean and exam-safe. The f means “formatted string”, so variables inside {} are replaced by values.

Mini showcase: simple marks report

name = input("Name: ")
m1, m2, m3 = map(int, input("Enter 3 marks: ").split())

total = m1 + m2 + m3
avg = total / 3

print(f"Student: {name}")
print(f"Total: {total}")
print(f"Average: {avg:.2f}")
{avg:.2f} means: show avg with exactly 2 digits after decimal. Sample run:
  • Input: Aarav and 78 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 return int automatically

Exam-focused points

  • default return type of input() is str
  • 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

  1. Take length and breadth as input and print area.
  2. Take 3 marks and print total and average.
  3. Write program to input name, class, and roll number and print in one formatted line.
  4. Predict output:
a, b = "89"
print(int(a) + int(b))