Skip to content

Types and conversion

3 min read

Every value in Python has a type, and the type decides what you can do with it. You've met three already: whole numbers, decimals, and text. By the end of this lesson you'll check a value's type, convert between types on purpose, and know the conversions that surprise people.

Every value has a type

type() tells you what kind of value something is. Predict all four lines.

What will this print?

Decide before you look. Guessing wrong is how this sticks.

print(type(250))
print(type(2.5))
print(type("250"))
print(type(True))

Python prints

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>

250 is a whole number (int), 2.5 has a decimal point (float), and "250" is text (str) even though it's made of digits. True is a bool, a yes-or-no value you'll use a lot in Module 2.

The is why "5" + 3 failed in lesson 1.3, and why input() answers had to be converted in lesson 1.5. Python won't treat text as a number, or a number as text, unless you ask it to.

type() is also a good way to check your own guesses. When something behaves strangely, print the type of each value involved. Try it now on 10 / 2.

print(type(10 / 2))

In the editor, press Escape then Tab to move on.

Converting between types

Each type's name is also a function that converts to it: int(), float(), and str(). Step through a price typed as text becoming a number and then a whole number. Watch the quotes disappear, then the decimals.

Step through it

Press Next to run one line at a time and watch each name change.

text = "3.7"
price = float(text)
whole = int(price)
print(whole)
Names and their values after line 1
NameValue after line 1
text'3.7'new
pricenot made yet
wholenot made yet

Output so far

Step 1 of 4

Step 1 of 4. Line 1: text = "3.7". text is now '3.7'.

Notice that int(3.7) gave 3, not 4. int() doesn't round; it cuts off everything after the decimal point.

Quick check

What does int(9.99) give?

int() is strict about text, though. It accepts text that looks like a whole number, spaces around it included, but not text with a decimal point:

print(int(" 42 "))

In the editor, press Escape then Tab to move on.

str() goes the other way and turns a number into text, so you can join it to other text:

total = 450
print("Total: " + str(total) + " birr")
print(f"Total: {total} birr")

In the editor, press Escape then Tab to move on.

Both lines print the same. The f-string converts for you and is easier to read, so you'll rarely need str() for printing. You'll need it when you truly want text, for example to count the digits in a number with len(str(total)).

True, False, and None

A bool is either True or False. You can convert other values to a bool too. What do you think these give?

What will this print?

Decide before you look. Guessing wrong is how this sticks.

print(bool("False"))
print(bool(""))
print(bool(0))
print(bool(250))

Python prints

True
False
False
True

bool() of text is True for any text that isn't empty, even the text "False": Python doesn't read the words. Only the empty string "" is False. For numbers, 0 is False and every other number is True.

That first line surprises everyone. Remember it when you meet if in Module 2: a user's answer of "no" is still non-empty text, so it counts as True.

One more value: None means "no value here". It's Python's way of saying nothing has been given yet, and its type is NoneType. You'll meet it properly with functions in Module 5.

Exercise

Add VAT to a typed price

A price arrives as text, "1250", the way it would from input(). Convert it to a number, add 15% VAT, and print the VAT and the total with two decimals.

Output
VAT: 187.50 birr
Total: 1,437.50 birr

Your code

price_text = "1250"

In the editor, press Escape then Tab to move on.

Show a solution

This is one way to solve it, not the only one. If yours prints the same thing, it works.

price_text = "1250"
price = float(price_text)
vat = price * 15 / 100
total = price + vat
print(f"VAT: {vat:.2f} birr")
print(f"Total: {total:,.2f} birr")

Key takeaways

  • Every value has a type: int, float, str, bool, and None's own type. type() shows it.
  • int(), float(), and str() convert. int() cuts decimals off without rounding.
  • int() reads whole-number text (spaces are fine), but not decimal text: use float() first.
  • f-strings convert numbers to text for you; you'll rarely need str() just to print.
  • bool() of any non-empty text is True, even "False". Empty text and 0 are False.