Python Data Types
In Python, data types define the type of data a variable can store.
Python automatically assigns a data type when a value is assigned to a variable.
Understanding data types is essential for writing correct, efficient, and error-free programs.
What Are Data Types?
Data types specify:
- What kind of value a variable holds
- What operations can be performed on that value
- How much memory is required
Python is a dynamically typed language, meaning you don’t need to declare the data type explicitly.
python
x = 10 # int
y = 3.14 # float
name = "Py" # str
Built-in Data Types in Python
Python provides several built-in data types, grouped into categories:
| Category | Data Types |
|---|---|
| Text Type | str |
| Numeric Types | int, float, complex |
| Sequence Types | list, tuple, range |
| Mapping Type | dict |
| Set Types | set, frozenset |
| Boolean Type | bool |
| Binary Types | bytes, bytearray, memoryview |
| None Type | NoneType |
Numeric Data Types
Integer (int)
Used to store whole numbers.
python
x = 10
y = -50
print(type(x))
python
count = 100
print(count)
Float (float)
Used to store decimal numbers.
python
price = 99.99
print(type(price))
python
value = 10.5
print(value)
Complex (complex)
Used to store complex numbers.
python
z = 3 + 5j
print(type(z))
python
print(z.real, z.imag)
Text Data Type
String (str)
Used to store text data.
python
name = "Python"
print(type(name))
python
text = 'Hello World'
print(text)
Boolean Data Type
Boolean (bool)
Used to store True or False values.
python
is_active = True
print(type(is_active))
python
print(10 > 5)
Sequence Data Types
List (list)
Used to store multiple values in a single variable.
Lists are ordered and mutable.
python
numbers = [1, 2, 3, 4]
print(type(numbers))
python
numbers[0] = 10
print(numbers)
Tuple (tuple)
Used to store multiple values.
Tuples are ordered but immutable.
python
colors = ("red", "green", "blue")
print(type(colors))
python
print(colors[1])
Range (range)
Used to generate a sequence of numbers.
python
nums = range(5)
print(list(nums))
python
print(type(nums))
Mapping Data Type
Dictionary (dict)
Stores data in key-value pairs.
python
student = {"name": "Jayesh", "age": 25}
print(type(student))
python
print(student["name"])
Set Data Types
Set (set)
Stores unique values.
Sets are unordered and mutable.
python
items = {1, 2, 3, 3}
print(items)
python
print(type(items))
Frozenset (frozenset)
Immutable version of a set.
python
data = frozenset([1, 2, 3])
print(type(data))
Binary Data Types
Bytes (bytes)
Immutable sequence of bytes.
python
b = bytes(5)
print(b)
Bytearray (bytearray)
Mutable sequence of bytes.
python
ba = bytearray(5)
ba[0] = 100
print(ba)
None Data Type
NoneType
Represents absence of a value.
python
x = None
print(type(x))
python
print(x)
Get the Data Type
Use the
type() function to check the data type.python
x = 10
print(type(x))
python
print(type("Python"))
Dynamic Typing in Python
Python allows changing the data type of a variable at runtime.
python
x = 10
x = "Ten"
print(x)
Summary
- Data types define the kind of data stored
- Python assigns data types automatically
- Supports numeric, text, boolean, sequence, mapping, set, and binary types
- Use
type()to check data type - Python is dynamically typed
Exercise
- Create one variable for each data type
- Use
type()to check them - Change a variable’s data type and observe the result