Lesson 4
Whole numbers, numbers with a dot and text
After this lesson: You'll meet three kinds of values: whole numbers, numbers with a dot and text - and see how they differ.
Video
The video for this lesson is coming soon.
Explanation
You can already print text and calculate. But did you know Python has different kinds of values? 7, 7.0 and "7" look similar, but they're three completely different things! ๐
Three kinds of values
| Kind | What it looks like | Examples | Python calls it |
|---|---|---|---|
| a whole number | digits, no dot | 7, 0, 2026, -3 | int |
| a number with a dot | digits with a dot | 2.5, 7.0, 0.1 | float |
| text | anything in quotes | "cat", "7", "2.5" | str |
First a story: Bajtek goes to the market.
Bajtek at the market ๐ฌ
Click Next to go step by step.
Three kinds of things
At the stall Bajtek sees three kinds of information: how many pieces (3 apples), how much something weighs (2.5 kg of plums) and what's written on the sign. In Python these are three kinds of values.
The value sorter ๐
Every value passes under the scanner and drops into its bin. Watch for the dot and the quotes!
Code
๐ฅ๏ธ Screen
The scanner reads every value that passes under it and says whether it's text, a whole number or a number with a dot. Click Next - the values will drop into their bins one by one.
Three sevens on the screen
Watch what each print prints. The number 7 and the text "7" look the same on the screen - you only see the difference in the code.
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print(7)2print(7.0)3print("7")
โA dot, not a comma
In some countries people write 2,5, but in Python numbers with a dot are written with a dot: 2.5. A comma in print separates different values - that's why print(2,5) prints two numbers: 2 5.
A comma or a dot?
The first line has a comma - Python sees two numbers and puts a space between them. The second has a dot - it's one number, 2.5.
Click the arrow - you'll see what print puts between the values.
1print(2,5)2print(2.5)
โWhich value is a number with a dot?
What comes out of a calculation?
- a whole number with a whole number - a whole number:
3 + 4is7, - when there's even one number with a dot - the result has a dot too:
2 + 0.5is2.5, and2 + 2.0is4.0, - text in quotes calculates nothing:
"3 + 4"is just text.
The result sorter ๐
Python calculates first, and only the result goes under the scanner.
Code
๐ฅ๏ธ Screen
The scanner reads every value that passes under it and says whether it's text, a whole number or a number with a dot. Click Next - the values will drop into their bins one by one.
The results
Compare: where does the result have a dot, and where doesn't it?
1print(3 + 4)2print(2 + 0.5)3print(2 + 2.0)4print("3 + 4")
โWhat does print(5 + 1.0) print?
What does print(2,5) print?
Why different kinds?
Python calculates with numbers, and only prints and keeps text. When you know which kind you have, you also know what Python will do with it. You'll use this all the time in the next lessons!
Remember
- A whole number (int) - no dot:
7,0,-3. - A number with a dot (float) - with a dot:
2.5,7.0. A dot, not a comma! - Text (str) - anything in quotes, even digits:
"7". - When a calculation has a number with a dot, the result has a dot too:
2 + 2.0is4.0.
Now you can recognise every value! Time for the tasks. ๐
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.