Python Data Types
Python Data Types
Python data types are classifications that define the nature of data that can be stored and manipulated within a program. These data types form the foundation for Python programming and help developers understand how the data is represented in memory and how it behaves when operated upon.
Here’s a detailed explanation of Python’s main data types with examples:
1.Numeric Data Types
These types store numeric values and allow arithmetic operations.
1.1 Integer (int)
Represents whole numbers (positive, negative, or zero).
Example:
x = 10 # Integer y = -5 # Negative integer z = 0 # Zero print(type(x)) # Output: <class 'int'>
1.2. Floating-Point (float)
Represents real numbers with a decimal point.
Example:
a = 3.14 # Float b = -0.75 # Negative float print(type(a)) # Output: <class 'float'>
1.3. Complex Numbers (complex)
Represents numbers with a real and an imaginary part.
Example:
c = 2 + 3j # Complex number print(type(c)) # Output: <class 'complex'> print(c.real, c.imag) # Output: 2.0 3.0
2.Sequence Data Types
These are ordered collections of data.
2.1. String (str)
Represents sequences of characters enclosed in quotes.
Example:
c = 2 + 3j # Complex number print(type(c)) # Output: <class 'complex'> print(c.real, c.imag) # Output: 2.0 3.0
2.2. List
An ordered, mutable collection of items, which can store mixed data types.
Example:
my_list = [1, 2, 3, "Python", 3.14] print(type(my_list)) # Output: <class 'list'> my_list.append("New Element") print(my_list) # Output: [1, 2, 3, 'Python', 3.14, 'New Element']
2.3. Tuple
An ordered, immutable collection of items.
Example:
my_tuple = (1, 2, 3, "Immutable", 3.14) print(type(my_tuple)) # Output: <class 'tuple'> print(my_tuple[1]) # Output: 2
3.Mapping Data Type
Used to store data in key-value pairs.
3.1. Dictionary (dict)
A collection of key-value pairs, where keys are unique and values can be any data type.
Example:
my_dict = {"name": "Python", "year": 1991, "version": 3.10} print(type(my_dict)) # Output: <class 'dict'> print(my_dict["name"]) # Output: Python
4.Set Data Types
These types store unordered collections of unique items.
4.1. Set
A mutable collection of unique items.
Example:
my_set = {1, 2, 3, 3, 4} print(type(my_set)) # Output: <class 'set'> print(my_set) # Output: {1, 2, 3, 4}
4.2. Frozen Set
An immutable version of a set.
Example:
frozen_set = frozenset([1, 2, 3, 4]) print(type(frozen_set)) # Output: <class 'frozenset'>
5.Boolean Type (bool)
Represents one of two values: True
or False
.
Example:
is_active = True is_logged_in = False print(type(is_active)) # Output: <class 'bool'> print(10 > 5) # Output: True
6.Binary Types
Used to handle binary data.
6.1. Bytes
Immutable sequences of bytes.
Example:
byte_data = b"Hello" print(type(byte_data)) # Output: <class 'bytes'>
6.2. Bytearray
A mutable sequence of bytes.
Example:
mutable_bytes = bytearray(b"Hello") mutable_bytes[0] = 72 # Changing the first byte print(mutable_bytes) # Output: bytearray(b'Hello')
6.3. Memoryview
Provides a view of memory data without copying.
Example:
byte_data = b"Memory" mv = memoryview(byte_data) print(mv[0]) # Output: 77 (ASCII for 'M')
7.None Type
Represents the absence of a value.
Example:
value = None print(type(value)) # Output: <class 'NoneType'>
These data types allow Python to be flexible and versatile, supporting operations and applications in diverse fields such as web development, data science, and artificial intelligence.