Lesson 15
type() - what type is a value?
After this lesson: You'll meet the types str, int and float and learn to check them with type().
Video
The video for this lesson is coming soon.
Explanation
The text "5" and the number 5 look almost the same. How does Python know whether to join or to add them? Every value has its type - and today you'll meet the scanner that shows it: type(). ๐
Every value has a type
So far you know three kinds of values. Python calls them types:
| Type | What it is | Examples |
|---|---|---|
str | text (short for string) - everything in quotes | "cat", "5", "2.5" |
int | a whole number (short for integer) | 5, 0, 2026 |
float | a number with a dot | 2.5, 5.0, 0.1 |
First a story: Bajtek gets parcels without labels and has to guess what's inside.
Bajtek and the unlabelled parcels ๐ฌ
Click Next to go step by step.
Two parcels
Bajtek works in a value sorting room. Two parcels arrived: one holds "5", the other 5. Kuba says they're the same. Are they?
type() in a program
type(value) gives the value's type, and print prints it. On the screen you'll see for example <class 'int'> - read it as class int. In Python a class is simply a type.
See the scanner in action: every value passes under the scanner and drops into its bin.
The type scanner ๐
Code
๐ฅ๏ธ Screen
The type() scanner reads every value that passes under it and says what type it is. Click Next - the values will drop into their bins one by one.
Three sevens
The same seven, three different types - see what the program prints.
1print(type(7))2print(type("7"))3print(type(7.0))
โWhat does the program print?
print(type("100"))
The type of a result
When there's a calculation inside type(), Python calculates first, then checks the type of the result:
3 + 4is7- int,"3" + "4"is the joined text"34"- str,10 / 2is5.0- float (division with/always gives a dot!),10 // 2is5- int.
The scanner and calculations ๐
Python calculates first, only the result goes under the scanner.
Code
๐ฅ๏ธ Screen
The type() scanner reads every value that passes under it and says what type it is. Click Next - the values will drop into their bins one by one.
The result and its type
Every line shows the result first, and its type next to it.
1print(3 + 4, type(3 + 4))2print("3" + "4", type("3" + "4"))3print(10 / 2, type(10 / 2))4print(10 // 2, type(10 // 2))
โWhat does the program print?
print(type(9 / 3))
The type of a variable
A variable is a box - and type() checks what's inside. A variable's type is the type of the value that's in it right now.
Three boxes
Look at the boxes: the name is text, the age is a whole number and the height is a number with a dot.
Click the arrow and watch the values move between the boxes.
1name = "Ola"2age = 113height = 1.454print(type(name))5print(type(age))6print(type(height))
โThe box changes its type
First the box x holds a number, then text. type() always says what's in it right now.
Click the arrow and watch the values move between the boxes.
1x = 52print(type(x))3x = "five"4print(type(x))
โinput() always gives text
Remember input() from the previous lesson? Let's scan what it gives us. Type anything - a word or a number - and type() will always say str.
Scan your input() โจ๏ธ
Type a number (e.g. 12) and press Enter โ. Then try again with a word - the type won't change!
1print("Type something:")2text = input()3print("You typed:", text)4print("Type:", type(text))
โSomeone typed 12. What does the program print?
number = input()print(type(number))
The error detective
When a program ends with a TypeError, add a print(type(...)) for a moment next to the suspicious variable. You'll see right away whether it's text or a number. In the next lesson you'll learn to turn one into the other!
Remember
type(value)tells you the value's type, e.g.<class 'int'>.str- text (in quotes),int- a whole number,float- a number with a dot.- Python calculates first, then checks the type:
10 / 2is a float,10 // 2is an int. input()always gives text -str.
You've got your own type scanner! 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.