Python Data Types
A data type defines the kind of value stored in a Python variable.
Python automatically determines the data type when a value is assigned to
a variable. The type() function can be used to find the type
of a value.
Types of Data Types in Python
Python provides several built-in data types. The commonly used types are:
| Category | Data Types | Example |
|---|---|---|
| Numeric | int, float, complex | 10, 10.5, 2+3j |
| Boolean | bool | True, False |
| Text | str | "Python" |
| Sequence | list, tuple, range | [1,2,3], (1,2,3) |
| Set | set, frozenset | {1,2,3} |
| Mapping | dict | {"name":"Amit"} |
| Binary | bytes, bytearray, memoryview | b"Python" |
| None | NoneType | None |
1. Numeric Data Types
Python provides three main numeric data types: int, float and complex.
Integer (int)
The int type represents whole numbers without a decimal part.
Integers can be positive, negative or zero.
age = 20
temperature = -5
count = 0
print(type(age))
print(type(temperature))
<class 'int'> <class 'int'>
Float (float)
The float type represents numbers containing a decimal point.
price = 99.50
percentage = 85.75
print(type(price))
print(type(percentage))
<class 'float'> <class 'float'>
Complex (complex)
A complex number has a real part and an imaginary part. Python uses
j to represent the imaginary part.
z = 2 + 3j
print(z)
print(type(z))
(2+3j) <class 'complex'>
Numeric data types are covered in more detail in the next tutorial.
2. Boolean Data Type
The bool type represents one of two values:
True or False. Boolean values are commonly used
in conditions and decision-making statements.
is_student = True
is_logged_in = False
print(is_student)
print(type(is_student))
True <class 'bool'>
Boolean values are also produced by comparison expressions.
age = 20
print(age >= 18)
print(age == 15)
True False
3. String Data Type
The str type is used to store text. Strings can be written
using single quotes, double quotes or triple quotes.
name = "Rahul"
city = 'Noida'
print(name)
print(city)
print(type(name))
Rahul Noida <class 'str'>
Strings are sequences of characters and support indexing and slicing.
text = "Python"
print(text[0])
print(text[0:3])
P Pyt
4. List Data Type
A list is an ordered and mutable collection of items.
A list can contain values of different data types.
numbers = [10, 20, 30, 40]
print(numbers)
print(numbers[0])
numbers.append(50)
print(numbers)
[10, 20, 30, 40] 10 [10, 20, 30, 40, 50]
5. Tuple Data Type
A tuple is an ordered collection of items. Unlike a list,
a tuple is immutable, which means its elements cannot normally
be changed after creation.
numbers = (10, 20, 30)
print(numbers)
print(numbers[1])
print(type(numbers))
(10, 20, 30) 20 <class 'tuple'>
6. Range Data Type
The range type represents a sequence of numbers and is
commonly used with loops.
numbers = range(1, 5)
for n in numbers:
print(n)
1 2 3 4
7. Set Data Type
A set is an unordered collection of unique elements.
Duplicate values are automatically removed.
numbers = {10, 20, 20, 30}
print(numbers)
print(type(numbers))
{10, 20, 30}
<class 'set'>
8. Frozen Set
A frozenset is an immutable version of a set.
numbers = frozenset([10, 20, 30])
print(numbers)
print(type(numbers))
9. Dictionary Data Type
A dictionary stores data in key-value pairs.
Each key is used to access its corresponding value.
student = {
"name": "Rahul",
"age": 20,
"marks": 85
}
print(student["name"])
print(student["marks"])
Rahul 85
10. Bytes Data Type
The bytes type represents an immutable sequence of bytes.
It is commonly used when working with binary data.
data = b"Python"
print(data)
print(type(data))
b'Python' <class 'bytes'>
11. Bytearray Data Type
The bytearray type is similar to bytes but its contents
can be modified.
data = bytearray([65, 66, 67])
data[0] = 68
print(data)
bytearray(b'DBC')
12. Memoryview Data Type
A memoryview object provides a view of the memory of a
bytes-like object without making a copy of the data.
data = bytearray("Python", "utf-8")
view = memoryview(data)
print(view[0])
print(type(view))
80 <class 'memoryview'>
13. NoneType
The None value represents the absence of a value.
Its data type is NoneType.
result = None
print(result)
print(type(result))
None <class 'NoneType'>
Checking the Data Type
The type() function is used to determine the type of an object.
a = 10
b = 10.5
c = "Python"
d = [1, 2, 3]
print(type(a))
print(type(b))
print(type(c))
print(type(d))
Type Conversion
Python provides built-in functions such as int(),
float() and str() to convert compatible
values from one data type to another.
x = "100"
a = int(x)
b = float(x)
c = str(50)
print(a)
print(b)
print(c)
100 100.0 50
Mutable and Immutable Data Types
Python data types can also be classified as mutable and immutable.
| Type | Nature | Examples |
|---|---|---|
| Mutable | Can be modified after creation | list, set, dict, bytearray |
| Immutable | Cannot be modified after creation | int, float, complex, bool, str, tuple, frozenset, bytes |
Example of Mutable Data Type
numbers = [10, 20, 30]
numbers[0] = 100
print(numbers)
[100, 20, 30]
Example of Immutable Data Type
A string cannot be changed directly after it has been created.
name = "Python"
# A new string is created
name = "Java"
print(name)
Java
type() function can be used to identify the type of
a value.