Data Types in Python
In Python, data types define the kind of value a variable holds. Python is dynamically typed, so you don’t have to declare the type explicitly — it’s inferred from the value you assign.
1. Numeric Types
a) Integer (int
)
Whole numbers without a decimal point.
x = 10
print(type(x)) # <class 'int'>
Supports positive and negative values:
a = -5
b = 0
b) Floating-point (float
)
Numbers with decimal points.
pi = 3.1415
print(type(pi)) # <class 'float'>
c) Complex (complex
)
Numbers with a real and imaginary part.
z = 2 + 3j
print(type(z)) # <class 'complex'>
print(z.real) # 2.0
print(z.imag) # 3.0
2. Boolean Type (bool
)
Represents logical values: True
or False
.
is_active = True
print(type(is_active)) # <class 'bool'>
print(10 > 5) # True
print(3 == 4) # False
3. Text Type
String (str
)
Sequence of characters enclosed in quotes (single '
, double "
, or triple '''
/"""
).
name = "Alice"
print(type(name)) # <class 'str'>
multiline = '''This is
a multi-line
string.'''
print(multiline)
Strings are immutable, meaning you can’t change them after creation.
4. Sequence Types
a) List (list
)
Ordered, mutable collection of items.
fruits = ["apple", "banana", "cherry"]
print(type(fruits)) # <class 'list'>
fruits.append("orange")
print(fruits) # ['apple', 'banana', 'cherry', 'orange']
print(fruits[1]) # banana
Lists can contain mixed data types:
mixed = [1, "two", 3.0, True]
b) Tuple (tuple
)
Ordered, immutable collection.
coordinates = (10, 20)
print(type(coordinates)) # <class 'tuple'>
# coordinates[0] = 5 # Error! Tuples are immutable
c) Range (range
)
Represents an immutable sequence of numbers, commonly used in loops.
r = range(1, 6) # 1 to 5
print(list(r)) # [1, 2, 3, 4, 5]
5. Set Types
Set (set
)
Unordered collection of unique elements.
myset = {1, 2, 3, 2, 1}
print(myset) # {1, 2, 3} duplicates removed
myset.add(4)
print(myset) # {1, 2, 3, 4}
Frozen Set (frozenset
)
Immutable version of set.
fs = frozenset([1, 2, 3])
# fs.add(4) # Error! Cannot modify frozen set
6. Mapping Type
Dictionary (dict
)
Key-value pairs, unordered (order preserved since Python 3.7).
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
print(type(person)) # <class 'dict'>
print(person["name"]) # Alice
person["age"] = 26 # Update
7. Binary Types
bytes
: Immutable sequences of bytes.
b = b"hello"
print(type(b)) # <class 'bytes'>
bytearray
: Mutable bytes.
ba = bytearray(b"hello")
ba[0] = 72 # ASCII for 'H'
print(ba) # bytearray(b'Hello')
Summary Table
Data Type | Description | Example |
---|---|---|
int |
Integer numbers | x = 10 |
float |
Floating-point numbers | pi = 3.14 |
complex |
Complex numbers | z = 2 + 3j |
bool |
Boolean values (True /False ) |
is_valid = True |
str |
Text strings | name = "Alice" |
list |
Ordered, mutable collection | [1, 2, 3] |
tuple |
Ordered, immutable collection | (10, 20) |
range |
Immutable sequence of numbers | range(1, 5) |
set |
Unordered, unique elements | {1, 2, 3} |
frozenset |
Immutable set | frozenset({1, 2, 3}) |
dict |
Key-value pairs | {"name": "Alice"} |
bytes |
Immutable bytes | b"hello" |
bytearray |
Mutable bytes | bytearray(b"hello") |
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.