AlgoBazaAlgoBaza
Python for kids ยท Module 1

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:

TypeWhat it isExamples
strtext (short for string) - everything in quotes"cat", "5", "2.5"
inta whole number (short for integer)5, 0, 2026
floata number with a dot2.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.

?Bajtek
They're both fives!๐Ÿง’
๐Ÿ“ฆ"5"?๐Ÿ“ฆ5
1

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?

Step 1 / 8

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 ๐Ÿ”

type( )
?Bajtek
๐Ÿ”ค strtext
๐Ÿ”ข intwhole number
๐Ÿ”Ÿ floatnumber with a dot

Code

๐Ÿ–ฅ๏ธ Screen

1

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.

Step 1 / 8

Three sevens

The same seven, three different types - see what the program prints.

Code
1print(type(7))
2print(type("7"))
3print(type(7.0))
Screen
โ–
1/4 ยท line 1 runs next
?

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 + 4 is 7 - int,
  • "3" + "4" is the joined text "34" - str,
  • 10 / 2 is 5.0 - float (division with / always gives a dot!),
  • 10 // 2 is 5 - int.

The scanner and calculations ๐Ÿ”

Python calculates first, only the result goes under the scanner.

type( )
?Bajtek
๐Ÿ”ค strtext
๐Ÿ”ข intwhole number
๐Ÿ”Ÿ floatnumber with a dot

Code

๐Ÿ–ฅ๏ธ Screen

1

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.

Step 1 / 9

The result and its type

Every line shows the result first, and its type next to it.

Code
1print(3 + 4, type(3 + 4))
2print("3" + "4", type("3" + "4"))
3print(10 / 2, type(10 / 2))
4print(10 // 2, type(10 // 2))
Screen
โ–
1/5 ยท line 1 runs next
?

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.

not yet
๐Ÿ“ฆ name
not yet
๐Ÿ“ฆ age
not yet
๐Ÿ“ฆ height

Click the arrow and watch the values move between the boxes.

Code
1name = "Ola"
2age = 11
3height = 1.45
4print(type(name))
5print(type(age))
6print(type(height))
Screen
โ–
1/7 ยท line 1 runs next

The box changes its type

First the box x holds a number, then text. type() always says what's in it right now.

not yet
๐Ÿ“ฆ x

Click the arrow and watch the values move between the boxes.

Code
1x = 5
2print(type(x))
3x = "five"
4print(type(x))
Screen
โ–
1/5 ยท line 1 runs next

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!

Code
1print("Type something:")
2text = input()
3print("You typed:", text)
4print("Type:", type(text))
Screen
โ–
1/3 ยท line 1 runs next
?

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 / 2 is a float, 10 // 2 is 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.