Variables and Data Types · Lesson 5 of 39
Data Types: Numbers, Text, Booleans and None
Every value in Python has a type. The type decides what you can do with the value: you can divide numbers, make text uppercase, and use a boolean in an if. This lesson covers the five basic types you will use in every program.
In this lesson
- The core types:
int,float,str,bool,None - Checking types with
type()andisinstance() - Why
0.1 + 0.2is not exactly0.3 - Truthy and falsy values
The map of built-in types
Python's built-in types
Every value has a type
- Numbers
int42 ·float3.14 ·complex2+3j - Text
str"hello" - Truth
boolTrue / False - Nothing
NoneTypeNone - Collections
list·tuple·dict·set(chapters 7–8)
Checking a type
print(type(42))
print(type(3.14))
print(type("42"))
print(type(True))
print(type(None))
<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'NoneType'>
Notice that "42" in quotes is text, not a number. That difference causes many beginner bugs. In real code, prefer isinstance(value, int) to compare types – it also accepts subclasses.
Integers (int)
Whole numbers, positive or negative. Python integers have no size limit – they grow as big as your memory allows, which is rare among languages.
followers = 1_250_000
print(followers * 3)
print(2 ** 100) # 2 to the power 100
print(0b1010, 0o17, 0xff) # binary, octal, hex
3750000 1267650600228229401496703205376 10 15 255
Floats (float)
Numbers with a decimal point. Any division with / gives a float, even when the result is whole:
print(10 / 2)
print(7 / 2)
print(1.5e3) # scientific notation: 1.5 × 10³
5.0 3.5 1500.0
The famous 0.1 + 0.2 puzzle
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(round(0.1 + 0.2, 2))
0.30000000000000004 False 0.3
Computers store floats in binary, and 0.1 cannot be written exactly in binary – just as 1/3 cannot be written exactly in decimal (0.3333…). The tiny error is normal in every language.
Common mistake: comparing floats with ==
Use math.isclose(a, b) to compare floats. For money, never use floats – use the decimal module or store amounts in paise (whole integers).
import math
from decimal import Decimal
print(math.isclose(0.1 + 0.2, 0.3))
print(Decimal("0.10") + Decimal("0.20"))
True 0.30
Strings (str)
Text in single or double quotes. Strings are a big topic with their own chapter; here are the basics:
first = "Ada"
last = "Lovelace"
full = first + " " + last # join with +
print(full)
print(len(full)) # number of characters
print("=" * 12) # repeat with *
Ada Lovelace 12 ============
Booleans (bool)
A boolean is either True or False (capital first letter). Comparisons produce booleans, and if statements consume them:
age = 19
is_adult = age >= 18
print(is_adult)
print(type(is_adult))
print(True + True) # bool is a kind of int: True is 1
True <class 'bool'> 2
Truthy and falsy
Any value can be tested in an if. These count as false; everything else is true:
for value in [0, 0.0, "", [], {}, None, "0", " ", [0]]:
print(repr(value), "→", bool(value))
0 → False
0.0 → False
'' → False
[] → False
{} → False
None → False
'0' → True
' ' → True
[0] → TrueEmpty things and zero are falsy. The string "0" and a list containing 0 are not empty, so they are truthy – a classic interview trick.
None: the absence of a value
None means "nothing here yet" or "no result". Functions that do not return anything give back None. Always test for it with is:
discount = None
if discount is None:
print("No discount applied")
result = print("hi") # print() returns None
print(result)
No discount applied hi None
Mutable or immutable?
All the types in this lesson are immutable: you cannot change a value in place, you can only create a new one. Lists, dictionaries and sets are mutable. This idea comes back again and again, especially with functions.
| Immutable | Mutable |
|---|---|
int, float, bool, str, tuple, None | list, dict, set, most objects you create |
Practice
Try it yourself
Print the type of
7,7.0,"7"and7 > 3.Show solution
for value in [7, 7.0, "7", 7 > 3]: print(type(value).__name__)int float str bool
A cart has items costing 19.99, 5.01 and 0.10. Print the total rounded to 2 decimal places.
Show solution
total = 19.99 + 5.01 + 0.10 print(round(total, 2))25.1
Quick check
What is type(10 / 5)?
float – the / operator always returns a float (2.0).
Is "False" truthy or falsy?
Truthy. It is a non-empty string; only the boolean False is false.
How should you check whether a variable is None?
if x is None: – use is, not ==.
Summary
- Core types:
int(unlimited size),float,str,boolandNone. type()shows a type;isinstance()checks one.- Floats are approximate – use
math.iscloseorDecimalfor exact work. - Zero, empty values and
Noneare falsy; everything else is truthy.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.