Comprehensive Notes on Python Data
Types
In Python, data types represent the type of value a variable can hold. Python is a
dynamically typed language, so the interpreter automatically determines the data type at
runtime based on the value assigned. Understanding data types and their subtypes is
essential for writing efficient and error-free code.
Main Categories of Python Data Types
Python data types can be broadly classified into the following categories:
1. Numeric Types
2. Sequence Types
3. Set Types
4. Mapping Types
5. Boolean Type
6. None Type
1. Numeric Types
Numeric types deal with numbers and are used for mathematical operations. Python
provides three subtypes:
a) **int** (Integer): Whole numbers, positive or negative, without decimals.
Example: 0, -10, 25
b) **float** (Floating-point number): Numbers with a decimal point.
Example: 3.14, -2.5, 0.0
c) **complex**: Numbers with a real and an imaginary part, represented as 'a + bj'.
Example: 2 + 3j
Code Example:
x = 10 # int
y = 3.14 # float
z = 2 + 3j # complex
print(type(z)) # Output: <class 'complex'>
2. Sequence Types
Sequence types store collections of items in an ordered manner. The items can be of the
same or different data types.
a) **List**:
- Ordered, mutable collection of elements.
- Elements can be modified, added, or removed.
Example: numbers = [1, 2, 3, 'apple']
numbers.append('banana')
print(numbers)
b) **Tuple**:
- Ordered, immutable collection of elements.
- Once defined, elements cannot be modified.
Example: coordinates = (10, 20, 30)
print(coordinates[0])
c) **String**:
- Sequence of characters enclosed in single (' '), double (" "), or triple quotes (''' ''').
- Strings are immutable.
Example: name = 'Python'
print(name[0]) # Output: P
d) **Range**:
- Represents a sequence of numbers, commonly used for looping.
Example: for i in range(1, 5):
print(i) # Output: 1 2 3 4
3. Set Types
Set types are used to store unordered collections of unique elements.
a) **Set**:
- Mutable collection of unique items.
- Does not allow duplicate values.
Example: fruits = {'apple', 'banana', 'cherry'}
fruits.add('orange')
print(fruits)
b) **Frozen Set**:
- Immutable version of a set.
- Once created, elements cannot be added or removed.
Example: immutable_set = frozenset([1, 2, 3])
print(immutable_set)
4. Mapping Types
Mapping types represent a collection of key-value pairs. Python's built-in mapping type is
the dictionary.
- **Dictionary**:
- Mutable, unordered collection where data is stored as key-value pairs.
- Keys must be unique and immutable (strings, numbers, tuples).
Example:
student = {'name': 'John', 'age': 21, 'course': 'CSE'}
print(student['name']) # Output: John
student['age'] = 22 # Updating value
5. Boolean Type
The Boolean type represents two values: True or False. It is mainly used in conditional
statements and logical operations.
Example:
is_active = True
has_passed = False
print(is_active and has_passed) # Output: False
print(is_active or has_passed) # Output: True
6. None Type
The None type represents the absence of a value or a null value. It is often used for default
function arguments or to indicate that a variable is empty.
Example:
result = None
if result is None:
print('No value assigned')
Type Conversion (Type Casting)
Python provides functions to convert one data type to another. This is known as type
casting.
Common functions:
- int() -> Converts to integer
- float() -> Converts to float
- str() -> Converts to string
- list() -> Converts to list
- tuple() -> Converts to tuple
- set() -> Converts to set
- dict() -> Converts to dictionary (from a list of tuples)
- bool() -> Converts to Boolean
Example:
x = int(3.99) # Output: 3
y = float(5) # Output: 5.0
z = str(100) # Output: '100'
a = list('Python') # Output: ['P', 'y', 't', 'h', 'o', 'n']