The float Data Type
The Python programming language has two main numeric data types. ints store integers, and floats store real numbers.
Dependencies
Students need to have these skills before learning this skill:
Goal
When you teach this skill, your goal is to make it so that students can write code that looks like this:
a = 1.5
b = 100.2
print(a + b)
Activities
Here are some activities that you can do in order to teach this skill.
What’s the difference?
-
Write this code on the board:
number_1 = 0
number_2 = 0.0 -
Ask students if there’s a difference between the two variables. Ask if the difference matters.
-
After accepting a few answers change the code so that it looks like this:
number_1 = 0
number_2 = 0.0
print(number_1)
print(number_2) -
Show students what that code does.
-
Now change the code on the board so that it looks like this:
number_1 = 0
number_2 = 0.0
number_3 = 0.00000000000
print(number_1)
print(number_2)
print(number_3) -
Ask students what they think that code is going to print.
-
Run the code and show students what it prints.
-
Explain to students the difference between
ints andfloats. -
Add some more examples of
floats to the board:height = 1.5
width = 3.1
area = height * width
print(area)