Data Types
The value 5 and the value "5" look almost the same, but Python treats them very differently: one is a number you can add, the other is text. The kind of a value is called its data type, and it decides what you can do with the value.
In this lesson you will meet Python's built-in data types, check a value's type with type() and isinstance(), and convert values from one type to another.
Every value has a type
You never declare a type in Python. Python looks at the value and knows. The built-in type() function tells you what it decided:
print(type(42))
print(type(3.75))
print(type("Jaipur"))
print(type(True))
print(type(None))
print(type([10, 20, 30]))<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'NoneType'> <class 'list'>
The word class in the output is Python's name for a type. You will build your own classes later in Classes & Objects.
The built-in types
Here are the types you will use most. Each has its own lesson later in this course.
| Type | What it holds | Example |
|---|---|---|
int | Whole numbers, any size | 42, -7, 10_00_000 |
float | Numbers with a decimal point | 3.75, 0.5 |
complex | Complex numbers | 3+4j |
str | Text | "Kochi", '₹500' |
bool | True or false | True, False |
NoneType | "No value" | None |
list | An ordered, changeable collection | [78, 85, 91] |
tuple | An ordered, fixed collection | (26.9, 75.8) |
range | A sequence of numbers | range(1, 11) |
dict | Key and value pairs | {"name": "Isha", "age": 20} |
set | Unique items, no order | {"red", "green"} |
You may also see bytes (raw binary data, such as the contents of an image file) and frozenset (a set that cannot change). You do not need them yet.
Numbers and text are different
A number in quotes is text. Python will not quietly convert one into the other, which protects you from silent mistakes.
print(5 + 5)
print("5" + "5")
print("5" * 3)
print(int("5") + 5)10 55 555 10
+ adds numbers but joins strings, and * repeats a string. When you mix the two, for example "Seats: " + 5, Python stops with TypeError: can only concatenate str (not "int") to str. Convert one side first, as the last line does with int("5").
Booleans are numbers too
A Boolean is a value that is either True or False, with capital letters. Booleans come from comparisons such as marks >= 40.
Here is a surprise that is worth knowing: bool is a subclass of int. That means True behaves like the number 1 and False like 0.
present = [True, False, True, True, False]
print(True + True)
print(isinstance(True, int))
print("Students present:", sum(present))2 True Students present: 3
This is handy: adding up a list of Booleans counts how many are True. It also means a check like isinstance(value, int) is true for True and False, which can catch you out when you only wanted real numbers.
None means "no value"
None is a special value that means "nothing here yet" or "no result". It is the only value of type NoneType. A variable for a student's phone number might start as None until the number is known. Check for it with is, not ==: write if phone is None:.
Checking a type in code
type() is great for exploring. In real code, use isinstance(), which answers yes or no and also accepts a tuple of types:
price = 1299.0
print(isinstance(price, float))
print(isinstance(price, (int, float)))
print(isinstance("1299", (int, float)))True True False
The second line asks "is price an int or a float?" This is the usual way to check that a value is a number.
Converting between types
Each type's name doubles as a function that converts a value to that type. This is called type conversion or casting.
print(int("42") + 8)
print(float("3.5"))
print(str(2026) + " batch")
print(int(9.99))
print(bool(0), bool(""), bool("Hi"))
print(list("Pune"))50 3.5 2026 batch 9 False False True ['P', 'u', 'n', 'e']
Notice two things. int(9.99) gives 9: it cuts off the decimal part, it does not round. And bool() gives False for "empty" values such as 0, "", [] and None, and True for almost everything else. That idea is called truthiness, and you will use it in if, elif & else.
Mutable and immutable
Some types can be changed after they are made; they are mutable. Lists, dictionaries and sets are mutable: you can add and remove items. Others are immutable: numbers, strings, tuples and frozensets never change. When you "change" a string, Python actually makes a new one. This difference explains a lot of Python behaviour, and you will see it again in Lists and Tuples.
Common mistakes
- Adding text and numbers.
"Total: " + 450fails. Usestr(450), or better, an f-string:f"Total: {450}". - Converting decimal text with
int().int("12.5")fails withValueError: invalid literal for int() with base 10: '12.5'. Usefloat("12.5")first. - Expecting
int()to round.int(7.9)is7. Useround()when you want rounding. - Lowercase booleans.
trueandfalseare just unknown names in Python. WriteTrueandFalse.
Key points
- Every value has a data type; Python works it out from the value.
type(x)shows the type;isinstance(x, int)checks it and accepts a tuple of types.- The core types are
int,float,complex,str,bool,None,list,tuple,range,dictandset. boolis a subclass ofint, soTrueacts like 1 andFalselike 0.int(),float(),str(),bool()andlist()convert values;int()cuts off decimals.- Lists, dicts and sets are mutable; numbers, strings and tuples are immutable.
Exercise
Create these variables: name = "Aarav", age_text = "19", height = 1.72 and is_member = True. Print the type of each one. Then convert age_text to a number, add 1 to it, and print "Next year Aarav will be 20" by building the sentence with str().
Show solution
name = "Aarav"
age_text = "19"
height = 1.72
is_member = True
print(type(name), type(age_text), type(height), type(is_member))
next_age = int(age_text) + 1
print("Next year " + name + " will be " + str(next_age))<class 'str'> <class 'str'> <class 'float'> <class 'bool'> Next year Aarav will be 20
age_text is a string even though it holds digits, so int() converts it before adding, and str() converts the result back to text before joining.
Quick Quiz
1. What does type("123") return?
The quotes make it a string, even though the characters are digits.
2. What is True + True?
bool is a subclass of int, so True acts like 1 and the sum is 2.
3. What is the value of int(6.8)?
int() cuts off the decimal part instead of rounding.
4. Which value is falsy, so bool() gives False?
An empty string is falsy. "0" and " " are non-empty strings, so they are truthy.
Learn Python with a mentor
Free lessons get you started. Our Python Programming training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
