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:
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
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:
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.
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:
Summary#
- Every value has a type; check it with
type()orisinstance(). - 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.