Watch on Instagram

Code Bytes · "Intro to Programming" · Episode 5 · Data

Data types

Every value in code has a type that tells the computer what it is and what it can do. In Python, 5 is an int (a whole number), 5.0 is a float (a number with a decimal point) and "5" is a str (text), so the same-looking values behave differently.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

Every value has a type

To you, 5, "5" and 5.0 all look like "five". To a computer they are three different kinds of value. A value's type tells the computer two things: what the value is, and what it can do.

You can ask Python for the type of any value with type():

``python print(type(5), type("5"), type(5.0)) ``

`` <class 'int'> <class 'str'> <class 'float'> ``

int, float and str

  • int (integer): a whole number with no quotes and no decimal point, like 5, 0 or -12.
  • float: a number with a decimal point, like 5.0 or 2.75. Even 5.0 is a float, though it is a whole amount.
  • str (string): text, written in quotes, like "hello" or "5". To Python, the 5 inside "5" is just a character, like a letter. It is not a number at all.

Same plus sign, different results

The type decides what + does:

``python print(5 + 5) print("5" + "5") print(5.0 + 5) ``

`` 10 55 10.0 ``

  • Two ints add up: 10.
  • Two strings get stuck together, one after the other: the text 55, not the number fifty-five. (Joining text like this is called concatenation; the next lesson, Strings, covers it.)
  • A float plus an int gives a float, so the answer keeps its decimal point: 10.0.

Mixing text and numbers: an error

``python print("5" + 5) ``

`` TypeError: can only concatenate str (not "int") to str ``

Python won't guess whether you meant to add numbers or join text, so it stops with a TypeError. Types matter.

Try it yourself

Open any free online Python editor (search "online Python") and run:

``python print(7 + 1) print("7" + "1") print(7.5 + 1) print(type(7), type("7"), type(7.5)) ``

You should see:

`` 8 71 8.5 <class 'int'> <class 'str'> <class 'float'> ``

Then try print("7" + 1) and read the TypeError it gives.

Good to know

  • Every snippet on this page was run with Python 3 and printed exactly what is shown.
  • int, float and str are Python's own names for these built-in types; type() shows them as <class 'int'>, <class 'float'> and <class 'str'>.
  • "A number with a decimal point" is the beginner's description of a float. Floats are stored in binary, so some decimals have tiny rounding errors; the Python tutorial's page on floating-point arithmetic explains why.
  • Adding an int and a float always gives a float.
  • Other languages treat mixed types differently: JavaScript, for example, joins "5" + 5 into text instead of raising an error. A later lesson covers that.

Answer to the comment question

What does print(2.0 + 3) show? 5.0. 2.0 is a float and 3 is an int, and a float plus an int gives a float, so Python prints 5.0, with the decimal point, not 5.

Test your knowledge

Quick quiz: Data types

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What type is the value "5" (with quotes) in Python?
2What does print("5" + "5") show?
3What does print(5.0 + 5) show?
4What happens when you run print("5" + 5)?

Sources

Video transcript

Why are 5, "5" and 5.0 different to a computer?

Every value in code has a type. It tells the computer what the value is, and what it can do.

5 with no quotes is an integer, a whole number. Python calls it int.

5.0 is a float: a number with a decimal point.

And "5" in quotes is a string: text. To Python, it's just a character, like a letter.

Watch. 5 plus 5 gives 10. "5" plus "5" sticks them together: 55. 5.0 plus 5 gives 10.0.

Mix text and a number with plus, and you get an error. Types matter.

What does this print? Comment your answer below!

That's your byte.

More from "Intro to Programming"

All "Intro to Programming" lessons