Nearby lessons
14 of 108Python - Data Types
Introduction
A Data Type represents the type of value stored in a variable.
In Python, you do not need to specify the data type manually.
Python automatically determines the data type based on the assigned value.
Because of this feature, Python is called a Dynamically Typed Language.
Example of Dynamic Typing
main.py
No output captured.
How Python Assigns Data Types
| Variable | Value | Data Type |
|---|---|---|
| a | 10 | int |
| b | 10.5 | float |
| c | "Durga" | str |
| d | True | bool |
Built-in Data Types in Python
| Data Type | Description | Example |
|---|---|---|
| int | Integer numbers | 10 |
| float | Decimal numbers | 10.5 |
| complex | Complex numbers | 3+5j |
| bool | Boolean values | True |
| str | Text/String | "Hello" |
| list | Ordered mutable collection | [1,2,3] |
| tuple | Ordered immutable collection | (1,2,3) |
| set | Unordered unique values | {1,2,3} |
| dict | Key-value pairs | {"a":1} |
| bytes | Immutable binary data | bytes([1,2]) |
| bytearray | Mutable binary data | bytearray([1,2]) |
| range | Sequence of numbers | range(5) |
| frozenset | Immutable set | frozenset({1,2}) |
| None | No value | None |
Important Built-in Functions
Python provides several built-in functions related to data types.
1. type() Function
The type() function returns the data type of a variable.
main.py
No output captured.
2. id() Function
The id() function returns the identity (memory reference) of an object.
Note: The returned value may be different on different systems.
main.py
140723456789
3. print() Function
The print() function displays output on the screen.
main.py
Python
Everything is an Object
In Python, everything is treated as an object.
Examples include:
- Variables
- Functions
- Strings
- Lists
- Numbers
Classification of Python Data Types
Python data types can be grouped into the following categories.
1. Numeric Data Types
Numeric data types store numbers.
- int
- float
- complex
main.py
Numeric values created successfully.
2. Boolean Data Type
The bool data type stores True or False.
main.py
True
3. String Data Type
The str data type stores text.
main.py
Durga
4. Sequence Data Types
Sequence data types store multiple values in order.
- list
- tuple
- range
main.py
[10, 20, 30]
5. Set Data Types
Set data types store unique values.
- set
- frozenset
main.py
{10, 20, 30}6. Mapping Data Type
The dict data type stores data as key-value pairs.
main.py
{'name': 'Rahul', 'marks': 90}7. Binary Data Types
Binary data types store binary values.
- bytes
- bytearray
main.py
b'\n\x14\x1e'
8. None Data Type
None represents the absence of a value.
main.py
None
Dynamic Typing
Python automatically changes the data type based on the assigned value.
main.py
No output captured.
Advantages of Dynamic Typing
- Easy to write programs.
- Less code is required.
- Provides flexibility.
- Speeds up development.
Key Points
- Python automatically assigns data types.
- Python is a dynamically typed language.
- Everything in Python is an object.
- Python provides 14 built-in data types.
- The
type()function checks the data type. - The
id()function returns the object's identity. - The
print()function displays output.
Quick Summary
| Category | Data Types |
|---|---|
| Numeric | int, float, complex |
| Boolean | bool |
| String | str |
| Sequence | list, tuple, range |
| Set | set, frozenset |
| Mapping | dict |
| Binary | bytes, bytearray |
| Special | None |