Data Types In Python
Introduction
In programming, data types define the kind of values a variable can hold and the
operations that can be performed on them.
Python is a dynamically typed language, which means we don’t need to explicitly
declare the data type of a variable. The type is decided automatically when we assign a
value.
Primitive Data Type
Primitive data types are the simplest types in Python.
They represent basic values and are immutable.
This means their values cannot be changed once they are created.
Non-Primitive Data Type
Non-primitive data types are more complex and can hold multiple values.
These types are mutable, meaning their contents can be modified.
Non-primitive (or composite/collection) data types are built using primitive types.
They can store multiple values and are useful for organizing data.
Mutable Data Types
Definition: Objects whose values can be changed after they are created.
Effect: When modified, the same memory location is updated.
Examples: list, dict, set, bytearray
Immutable Data Types
Definition: Objects whose values cannot be changed once created.
Effect: Any modification creates a new object in memory.
Examples: int, float, bool, str, tuple, frozenset, bytes
Homogeneous Data
Definition: A collection in which all elements are of the same data type.
Behavior in Python: Although Python allows mixed data types, we can still create
homogeneous collections by keeping all elements of the same type.
numbers = [1, 2, 3, 4, 5] # List of integers
vowels = ('a', 'e', 'i', 'o', 'u') # Tuple of strings
Heterogeneous Data
Definition: A collection in which elements can be of different data types.
Behavior in Python: Python collections (like list, tuple, set, dict) can store mixed data
types, making them heterogeneous by nature.
mixed_list = [1, "Python", 3.14, True]
student = {"name": "John", "age": 21, "is_active": True}
Types Of Primitive Data Types
Number
Numbers in Python are used to represent numeric values. They are further divided into:
● Integer (int) → Whole numbers (e.g., 10, -5)
● Float (float) → Decimal numbers (e.g., 3.14, -2.5)
● Complex (complex) → Numbers with real and imaginary parts (e.g., 3+4j)
Boolean
Booleans represent truth values – True or False. They are mainly used in
decision-making and conditional statements.
is_active = True
is_logged_in = False
String
A string is a sequence of characters enclosed within single (' '), double (" "), or
triple quotes (''' ''' or """ """).
name = "Python"
message = 'Hello'
paragraph = """This is
a multi-line string."""
Types of Non-Primitive Data Types
List
Creation:
fruits = ["apple", "banana", "mango"]
Properties:
● Ordered (elements have a defined index).
● Mutable (can be changed after creation).
● Allows duplicate values.
● Can store heterogeneous data.
Tuple
Creation:
coordinates = (10, 20, 30)
Properties:
● Ordered.
● Immutable (cannot be changed after creation).
● Allows duplicate values.
● Can store heterogeneous data.
Dictionary (dict)
Creation:
student = {"name": "John", "age": 21, "is_active": True}
Properties:
● Stores data in key-value pairs.
● Mutable (values can be changed).
● Keys are unique, values can be duplicate.
● Unordered (Python 3.7+ maintains insertion order).
Set
Creation:
unique_numbers = {1, 2, 3, 4}
Properties:
● Unordered collection.
● Mutable (elements can be added/removed).
● Does not allow duplicate values.
● Can store heterogeneous data (but only immutable objects inside).
None in Python
None is a special data type in Python.
It represents the absence of a value or a null value.
Creation:
None is created simply by assigning it to a variable.
x = None
Properties:
● Belongs to its own data type: NoneType.
● Only one object of None exists in Python.
● Any variable assigned None refers to the same object.
● The None object cannot be modified.
Thank You