Types and input() · Python from Zero
Why does "3" + "4" give 34, and how do you read what the user types?
Course contentsWhy you need it
Every value in Python has a type, and the type decides what operations mean. + adds numbers but joins strings. Programs that talk to people need input(), and input() always gives back text, so you must convert it before doing maths. Mixing up types is the most common beginner bug.
The intuition
A phone number and a quantity both look like digits, but you'd never add two phone numbers. Python keeps track of what kind of thing each value is:
| Type | Example | Meaning |
|---|---|---|
int | 42 | whole number |
float | 3.5 | decimal number |
str | "42" | text |
bool | True | yes / no |
Convert between them: int("42") → 42, str(42) → "42", float("3.5") → 3.5.
input() waits for the user to type a line and returns it as a str. To use it as a number: age = int(input()).
Definition
Data type
A data type is a category of values, such as int, float, str or bool, that determines which operations can be applied to them.
input()
input() reads one line typed by the user and returns it as a string.
On this site, type the input lines in the Input box before you press Run.
Examples
Asking for the type
Try it yourselftype() tells you the type of any value.
print(type(3))
print(type(3.5))
print(type("3"))
print(type(True))- L1A whole number is an int.
<class 'int'> - L2With a decimal point it's a float.
<class 'float'> - L3In quotes it's a str, even if it contains digits.
<class 'str'> - L4True and False are bool.
<class 'bool'>
Reading and converting
Try it yourselfChange the lines in the Input box and run again.
name = input()
age = int(input())
print(name, "will be", age + 1, "next year")Ada 36
- L1Reads the first input line as text: "Ada".
name = 'Ada' - L2Reads "36" (text), then int() turns it into the number 36.
age = 36 - L3age + 1 is 37 because age is a number.
Ada will be 37 next year
Common mistakes
Forgetting int() around input()
input() + 1 is a TypeError: you can't add a number to text. Convert first: int(input()) + 1.
int() only accepts whole numbers
int("3.5") and int("seven") are ValueErrors. Use float() for decimals.
Quick quiz
1. What does this print?
print("5" * 3)Show the answer
Answer: B
A string times a number repeats the string.
2. What does this print?
print(int("7") + 3, "7" + "3")Show the answer
Answer: A
int("7") is the number 7, so 7 + 3 = 10. "7" + "3" joins two strings.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictText or number?
Type exactly what this program prints.
- ModifyReally add them
This program reads two numbers but prints them stuck together (3 and 4 give 34). Fix it so it prints their sum.
- WriteMinutes to hours
Read a number of minutes. Print the whole hours on the first line and the minutes left over on the second line. For 135 the output is 2 and 15.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Data type | 数据类型 | A category of values that determines which operations apply to them. |
| int | 整数类型 | The type of whole numbers, such as 42. |
| float | 浮点数类型 | The type of numbers with a decimal point, such as 3.5. |
| input() | input() 输入函数 | A function that reads a line typed by the user and returns it as a string. |
| Type conversion | 类型转换 | Turning a value into another type, for example with int() or str(). |