⚡0 XP🔥0

Data Types

Lesson 5 of 33 ⏱ 4 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Python

Python's built-in data types - int, float, complex, str, bool, None, and an overview of list, tuple, set and dict - with type() and mutability.

What is a data type?#

Every value in Python has a type that tells Python what kind of data it is and what you can do with it. For example, you can multiply two numbers, but you cannot multiply two names.

Use the built-in type() function to check a value's type:

▶ Try it YourselfPython
Ctrl + Enter to run

Overview of built-in types#

Category Type Example
Numbers int 25, -7, 1_000_000
float 3.14, -0.5, 2e3
complex 3 + 4j
Text str "Python", 'A'
Boolean bool True, False
Nothing NoneType None
Sequences list [1, 2, 3]
tuple (1, 2, 3)
range range(5)
Mapping dict {"name": "Ravi", "age": 20}
Sets set {1, 2, 3}

Numbers#

int (integers)#

Whole numbers, positive or negative, with no size limit in Python.

students = 45
temperature = -3
big = 12345678901234567890123
readable = 1_00_000     # underscores are allowed for readability -> 100000

float (decimal numbers)#

pi = 3.14159
price = 99.0
distance = 1.5e3       # scientific notation = 1.5 x 10^3 = 1500.0
Common Mistake

Floats are stored in binary, so some decimals are not exact:

print(0.1 + 0.2)    # 0.30000000000000004

This is normal in all programming languages. Use round(0.1 + 0.2, 2) when displaying money, or the decimal module for exact accounting.

complex#

Used in engineering and science: z = 3 + 4j. z.real is 3.0 and z.imag is 4.0. You will rarely need it in everyday programs.

Strings (str)#

Text inside single, double or triple quotes:

first = 'Ravi'
last = "Sharma"
message = """This string
spans multiple lines."""

Strings get two full lessons later in this course.

Booleans (bool)#

Only two values: True and False. They are the result of comparisons and are used in decisions:

▶ Try it YourselfPython
Ctrl + Enter to run
Did you know?

Internally, True behaves like 1 and False like 0. So True + True gives 2. That is why sum([True, False, True]) counts the True values.

None#

None means "no value yet" or "nothing". For example, a function that doesn't return anything gives None.

winner = None          # no winner decided yet
print(winner)          # None
print(winner is None)  # True

Collections (preview)#

These hold many values in one variable. Each has its own lesson later.

▶ Try it YourselfPython
Ctrl + Enter to run

Notice the set printed "red" only once - sets remove duplicates.

Mutable vs immutable#

This is one of the most important ideas in Python.

Immutable (cannot change) Mutable (can change)
int, float, bool, str, tuple list, dict, set
name = "ravi"
# name[0] = "R"     -> TypeError: 'str' object does not support item assignment

marks = [70, 80]
marks[0] = 75       # allowed - lists are mutable
print(marks)        # [75, 80]
हिंदी में समझें

Immutable का मतलब है "जिसे बदला नहीं जा सकता"। जैसे string "ravi" का पहला अक्षर आप सीधे नहीं बदल सकते - आपको नई string बनानी पड़ती है।

Mutable का मतलब है "जिसे बदला जा सकता है"। List में आप किसी भी position की value बदल सकते हैं, नई value जोड़ सकते हैं या हटा सकते हैं।

यह फर्क आगे functions और lists में बहुत काम आएगा - इसलिए इसे अभी अच्छे से याद कर लें।

isinstance() - checking a type#

isinstance(value, type) returns True or False. It is the preferred way to check types in real code:

▶ Try it YourselfPython
Ctrl + Enter to run

Summary#

  • Every value has a type; check it with type() or isinstance().
  • Numbers: int, float, complex. Text: str. Logic: bool (True/False). Nothing: None.
  • Collections: list, tuple, set, dict.
  • Immutable: numbers, strings, tuples. Mutable: lists, dicts, sets.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Identify the types

Print the type of each variable using type(), one per line, in the order a, b, c, d.

Expected output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
2

Create variables of each type

Create these variables:

  • count - an integer 10
  • price - a float 99.5
  • course - the string Python
  • passed - the boolean True
  • result - the value None

Quick Quiz

Q1.

What is the output of print(type(3.0))?

Q2.

What is type("25")?

Q3.

Which value is of type bool?

Q4.

Which data type is immutable?

Q5.

What does None represent?