Unit 1 · Python Data Types
Python Data Types
Every piece of data in Python belongs to a specific type. Understanding these types is essential for writing correct, bug-free programs.
Introduction
A data type defines the kind of value a variable can hold, and what operations can be performed on it. Python is dynamically typed, meaning you don't need to declare a variable's type explicitly — Python figures it out automatically based on the value assigned.
Table of Contents
What is a Data Type?
A data type tells the interpreter how to interpret the value stored in a variable — whether it's a whole number, a decimal, text, or a collection of items — and what operations are valid on it.
Tip
Python's built-in data types can be grouped into: Numeric, Sequence, Mapping, Set, Boolean, and None types.
Numeric Types
int
Whole numbers, positive or negative. Example: 10,
-25
float
Decimal (floating-point) numbers. Example: 3.14,
-0.5
complex
Numbers with a real and imaginary part. Example: 3+4j
Sequence Types
str
A sequence of characters. Example: "Python"
list
An ordered, mutable collection. Example: [1, 2, 3]
tuple
An ordered, immutable collection. Example: (1, 2, 3)
Mapping and Set Types
dict
A collection of key-value pairs. Example: {"name": "Amit",
"age": 20}
set
An unordered collection of unique items. Example: {1, 2,
3}
Boolean and None Type
bool
Represents one of two values: True or
False.
NoneType
Represents the absence of a value: None.
The type() Function
You can check the data type of any value or variable using Python's built-in
type() function.
print(type(10)) # <class 'int'> print(type(3.14)) # <class 'float'> print(type("Hi")) # <class 'str'> print(type([1, 2])) # <class 'list'>
Mutable vs Immutable Types
Mutable types can be changed after creation, while immutable types cannot — any "change" actually creates a new object.
| Mutable | Immutable |
|---|---|
list, dict, set |
int, float, str,
tuple, bool |
Code Example
name = "Neha" age = 22 height = 5.6 is_active = True marks = [85, 90, 78] print(type(name), type(age), type(height), type(is_active), type(marks))
Interview Tip
A frequent question: "What's the difference between a list and a tuple?" The main answer: lists are mutable (can be changed), while tuples are immutable (cannot be changed once created).
Quick Revision
| Category | Types |
|---|---|
| Numeric | int, float, complex |
| Sequence | str, list, tuple |
| Mapping / Set | dict, set |
| Boolean / None | bool, NoneType |
Summary
Python offers a rich set of built-in data types — numeric, sequence, mapping, set, boolean, and none — that let you represent almost any kind of real-world data. Knowing which type to use, and whether it's mutable or immutable, is key to writing correct and efficient Python programs.