Data Types

Python · Lesson 5 of 56 6 min read Updated

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:

PythonRun it yourself
print(type(42))
print(type(3.75))
print(type("Jaipur"))
print(type(True))
print(type(None))
print(type([10, 20, 30]))
Output
<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.

TypeWhat it holdsExample
intWhole numbers, any size42, -7, 10_00_000
floatNumbers with a decimal point3.75, 0.5
complexComplex numbers3+4j
strText"Kochi", '₹500'
boolTrue or falseTrue, False
NoneType"No value"None
listAn ordered, changeable collection[78, 85, 91]
tupleAn ordered, fixed collection(26.9, 75.8)
rangeA sequence of numbersrange(1, 11)
dictKey and value pairs{"name": "Isha", "age": 20}
setUnique 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.

PythonRun it yourself
print(5 + 5)
print("5" + "5")
print("5" * 3)
print(int("5") + 5)
Output
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.

PythonRun it yourself
present = [True, False, True, True, False]

print(True + True)
print(isinstance(True, int))
print("Students present:", sum(present))
Output
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:

PythonRun it yourself
price = 1299.0

print(isinstance(price, float))
print(isinstance(price, (int, float)))
print(isinstance("1299", (int, float)))
Output
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.

PythonRun it yourself
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"))
Output
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: " + 450 fails. Use str(450), or better, an f-string: f"Total: {450}".
  • Converting decimal text with int(). int("12.5") fails with ValueError: invalid literal for int() with base 10: '12.5'. Use float("12.5") first.
  • Expecting int() to round. int(7.9) is 7. Use round() when you want rounding.
  • Lowercase booleans. true and false are just unknown names in Python. Write True and False.

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, dict and set.
  • bool is a subclass of int, so True acts like 1 and False like 0.
  • int(), float(), str(), bool() and list() 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
Python
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))
Output
<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?

2. What is True + True?

3. What is the value of int(6.8)?

4. Which value is falsy, so bool() gives False?

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