Data Types in Python
Level 2 · Values and variables
The kinds of value a program works with: whole numbers, decimals, text, true or false, and None. Why 0.1 + 0.2 is not quite 0.3.
Course contentsLesson 6 of 18
Level 1 · What a program is
Level 2 · Values and variables
Level 3 · Decisions and loops
Level 4 · Functions
Level 5 · Errors and debugging
Level 6 · Good code habits
After this lesson you can
- Name the basic data types in Python
- Explain why decimals can be slightly off
- Tell None apart from zero and empty text
12 mincovers 7 glossary entries
Worth reading first: Variables and Assignment
What kinds of value a program can hold
Every value has a data type: the kind of value it is. Python's main ones are whole numbers, decimal numbers, text, and true or false. The type decides what you can do. You can multiply two numbers, but you cannot multiply two words. Ask Python with type(7) and it answers int.
An integer is a whole number, such as 7, 0 or -12. In Python it is called int. Python integers can grow as big as memory allows, so 2 ** 100 works fine. Dividing two integers with / gives a decimal, so 7 / 2 gives 3.5. Use // to keep only the whole part, which gives 3.
A floating-point number is a number with a decimal part, such as 3.5. In Python it is called float. A float keeps about 15 to 17 digits, so it is close but not always exact. Type 0.1 + 0.2 and Python prints 0.30000000000000004. For money, people often count in whole cents instead.
A string is a piece of text, written inside quote marks, such as "Emma" or 'hello'. In Python it is called str. A string can hold one letter, a whole page, or nothing at all, written as "". Note that "42" in quote marks is text, not a number.
A boolean holds only one of two values, True or False. In Python it is called bool, and the capital letter matters. Booleans answer yes or no questions, such as is the cart empty. Every if test you will write later works out to one of them.
Null means no value at all. Python spells it None. It is not 0 and it is not empty text. A variable holding None is saying there is nothing here yet, such as a phone number a user never gave. Many crashes come from treating None as if it were a real value.
Underneath, every value is stored as bytes. A byte is 8 bits, and each bit is a 0 or a 1, so one byte has 256 possible values, from 0 to 255. The letter A takes 1 byte in most text files. File sizes and memory are both measured in bytes.
The basic data types in Python
Worked example
Emma's price total is off by a hair
Emma's till adds 0.10 and 0.20 and shows 0.30000000000000004. Her test expects exactly 0.3 and fails.
She changes the program to store prices in cents, as integers: 10 and 20. The total is exactly 30, and she divides by 100 only when she shows it.
The words for this lesson7
Check yourself
Why does 0.1 + 0.2 not give exactly 0.3 in Python?
Finished this lesson?
Marking it complete also marks its 7 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.