Values and types

Every piece of data in Python is a value with a type. Meet whole numbers, decimal numbers, text and True or False, and ask Python what type something is.

Lesson · 12 min

In this lesson you will

  • Explain what a value is
  • Recognise the four basic types int, float, str and bool
  • Use type() to ask Python what type a value is
  • Explain why "7" and 7 behave differently

In the last lesson you printed text and numbers. Each of those things, "Hello!", 5, 24 * 60, is a value: a single piece of data that a program can work with. Programs are really just machines for taking values in, changing them, and handing new values back out.

Values come in kinds

Think about the things you write on a shopping list: a quantity (3 apples), a price (1.25), a name (“oat milk”), a tick box (bought or not). They’re all data, but they’re different kinds of data. You can add two prices together; adding two tick boxes makes no sense.

Python sorts every value into a kind, called its type. The type decides what you’re allowed to do with the value. Here are the four you’ll use constantly:

TypeWhat it holdsExamples
intwhole numbers (integers)7, 0, -12, 2026
floatnumbers with a decimal point1.25, 3.0, -0.5
strtext (a “string” of characters)"oat milk", 'hi', "7"
booltrue or falseTrue, False

The odd name “float” comes from “floating point”, the way computers store decimal numbers. “String” is short for “string of characters”, like beads on a thread.

Ask Python: type()

You don’t have to guess a value’s type. Python will tell you. type(...) gives back the type of whatever is inside the brackets, and print shows it. Predict each answer before you run:

print(type(7))
print(type(1.25))
print(type("oat milk"))
print(type(True))

The answers look like <class 'int'>. You can read “class” as “type” for now; the important word is the one in quotes.

The decimal point matters

Is 3 the same as 3.0? To a person, yes. To Python, one is an int and the other a float:

print(type(3))
print(type(3.0))
print(3 == 3.0)

The last line asks “are these equal?” and Python says True: they’re the same amount, just stored as different types. You’ll learn more about == in the next section. For now, notice that a decimal point on its own is enough to make a number a float.

Quotes make text

This is the most important idea in the lesson. Quote marks turn anything into text. 7 is a number. "7" is a piece of text that happens to contain the character 7, the same way a house number on a door is a label, not something you’d do maths with.

print(type(7))
print(type("7"))
print(7 + 7)
print("7" + "7")

7 + 7 adds numbers and gives 14. "7" + "7" joins text and gives 77. Same symbol, different job, all because of the type. This is exactly why types exist: the type tells Python what an operation should mean.

You can use either single quotes 'hi' or double quotes "hi" for a string, as long as both ends match. Python doesn’t mind which.

True and False

The bool type has only two values: True and False. They’re named after George Boole, a mathematician who worked out the rules of true-and-false logic. They answer yes-or-no questions, and they’re what make programs able to decide things, which is the whole of the next section.

print(True)
print(type(False))
print(10 > 3)

10 > 3 asks “is 10 bigger than 3?”, and the answer is a bool. Notice the capital letters: True and False must start with a capital. true means nothing to Python.

Converting between types

Sometimes you have the wrong type and need to convert. Each type’s name doubles as a converter:

print(int("42") + 1)
print(float(3))
print(str(99) + " red balloons")
print(int(7.9))

int("42") turns the text "42" into the number 42, so we can add to it. str(99) turns a number into text so we can join it to other text. And look at the last one: int(7.9) gives 7. Converting a float to an int chops off the decimal part; it doesn’t round.

Not every conversion makes sense. int("seven") can’t work, because Python doesn’t read English. It stops with an error:

ValueError: invalid literal for int() with base 10: 'seven'

“Invalid literal” is a fussy way of saying “this text doesn’t look like a whole number”.

Your turn

Exercise · Python

Fix the types

The code below should work out how many stickers Ali has in total, but the values have the wrong types. Change only the values (not the names) so that:

  • stickers_owned is the whole number 24,
  • stickers_bought is the whole number 6,
  • total is their sum, 30, and
  • label is the text "stickers".
stickers_owned = "24"
stickers_bought = "6"
total = stickers_owned + stickers_bought
label = stickers
print(total, label)

Check yourself

Self-check · 4 questions

Values and types

1. What is the type of 3.0?
2. What does print("7" + "7") show?
3. What does int(7.9) give?
4. Which of these is a bool?