int and float
- Python course
- Beginner
- Lesson 13/116
- 4 min
Learn int and float types and conversions.
int and float
In Python numbers come in two basic types: int (integer) and float (decimal).
age = 25 # int
price = 19.99 # floatint holds whole numbers; Python has no integer limit (as long as memory allows). float is for decimal numbers.
When you mix the two, the result is usually a float: 5 + 2.0 gives 7.0. This is called automatic type conversion.
Large numbers and underscores
Python accepts underscores in large numbers to make them easier to read:
population = 7_900_000_000
budget = 1_250_000.50The underscores are ignored by Python; the number has the same value. This works like a thousands separator and improves readability.
Scientific notation is also supported: 1e6 means one million (float).
Practice Print int and float
Assign 100 (int) to count, assign 2.5 (float) to weight. Then print print(count, weight).
Starter code
# Define the numbersSample solution
count = 100
weight = 2.5
print(count, weight)Expected output
100 2.5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the result of 5 + 2.0?
- 7
- 7.0 Correct answer
- 5.0
- It raises an error
Explanation: int + float = float, the result is 7.0.
2 Assign a decimal price
price = ____
print(price)
price = 19.99
print(price)
Explanation: price = 19.99 assigns a float value.
3 Which of these is a float?
- 5
- '5.0'
- 5.0 Correct answer
- "5"
Explanation: 5.0 is a decimal number, a float.