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.
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,0or-12. - float: a number with a decimal point, like
5.0or2.75. Even5.0is a float, though it is a whole amount. - str (string): text, written in quotes, like
"hello"or"5". To Python, the5inside"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,floatandstrare 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" + 5into 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.
Sources
- Python Software Foundation, "Built-in Types: Numeric Types int, float": whole numbers and floating-point numbers; mixing int and float gives a float
- Python Software Foundation, "Built-in Types: Text Sequence Type str": strings are text
- Python Software Foundation, "type()" built-in function: asking for a value's type
- Python Software Foundation, "Built-in Exceptions: TypeError": an operation on the wrong type
- Python Software Foundation, "Floating-Point Arithmetic: Issues and Limitations": why floats are approximate
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"
- "Intro to Programming" EP2Compiled vs interpreted
- "Intro to Programming" EP3Your first line of code
- "Intro to Programming" EP7If statements
- "Intro to Programming" EP8Comparison and logic