Input, Output and Type Conversion
Reading what a user types, printing results back, and converting typed text into a number before doing arithmetic with it.
What a learner can do afterwards
- Read a value from the user and store it in a variable
- Convert typed input to a whole number or a decimal before calculating with it
- Explain why adding two typed numbers without converting them joins them as text
1 · Read
Your program can ask the user to type something with input(), and show things back with print(). Whatever is typed always arrives as text, even when it looks like a number. You store it in a variable so you can use it later.
A shop program needs a quantity and a price. It runs quantity = int(input()) and price = float(input()), then total = quantity * price, then print(total). With 3 and 5.0 typed in, it prints 15.0.
Use int() to turn typed text into a whole number, and float() for a decimal like a price. Mixing a whole number with a decimal gives a decimal, and dividing with / always gives a decimal too.
Never skip the conversion step. Adding two typed values without converting glues them as text, so 3 and 5 become 35 instead of 8.
Read text with input(), convert with int() or float(), then calculate and print.
2 · Watch
Take it off screen
Where it sits
Learn first
This opens up
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.