Python Learning Series · @onlysamarrr · Day 3 / 30

Day 3: input() and Type Casting

Day 3 teaches input() and type casting. input() always returns text, even if the user types a number, so int() or float() must be used before doing maths with it.

Phase: Beginner · Reels: 11/450 – 15/450

Quick Answer

Day 3 teaches input() and type casting. input() always returns text, even if the user types a number, so int() or float() must be used before doing maths with it.

What You Will Learn

Code

age_text = "21"
print(age_text, type(age_text))
age = int(age_text)
print(age, type(age))
print(age + 1)
print(float("3.5"), str(10), bool(""))

Output

21 <class 'str'>
21 <class 'int'>
22
3.5 10 False

Code Explained, Line by Line

  1. input() asks the user for data and always returns a str, even for numbers.
  2. int("21") converts text to the whole number 21. float("3.5") converts to 3.5.
  3. str(10) converts a number back to text "10", and bool("") is False.
  4. Casting is the name for changing a value from one type to another.
  5. In a real program you write age = int(input("Age? ")) so the age can be used in maths.
Common Mistake: Never do maths directly on input() output. "21" + 1 raises a TypeError because the left side is still text.

Real-Life Use

Every form, calculator and signup page reads input as text first and converts it before calculating.

Practice Task: Build an age calculator: convert the age to int and print next year's age.

Test Yourself — MCQs

Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.

Q1. What does input() always return?

Q2. What is int("21") + 1?

Q3. What is float("3.5")?

Today's 5 Reels (from the 90-Day Plan Excel)

11/450Conceptinput() se user se data lo
12/450Live demoint(input()) live demo
13/450Common mistakeinput hamesha string deta hai
14/450QuizQuiz: age calculator
15/450Interview / real lifeInterview: type casting kya hai
← Day 2Day 4 →