CodeBlanks Logo Icon CodeBlanks

int and float

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     # float

int 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.50

The 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

main.py
# Define the numbers

Sample solution

main.py
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

Question
price = ____
print(price)
Answer
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.

All lessons in Beginner