In Python, data types define the type of data a variable can hold. Python has a rich set of built-in data types, categorized into several groups:
1. Numeric Types
int: Represents whole numbers (positive, negative, or zero) without decimals.
Example:
x = 20
y = -10
z = 0
float: Represents numbers with decimals (floating-point numbers).
x = 20.15
y = -10.57
complex: Represents complex numbers with a real and imaginary part.
Example:
z = 5 + 10j
2. Text Type
string (str): A sequence of characters, or simply text. Strings are enclosed in quotes.
Example:
name = "John"
greeting = 'Hello Friends'
3. Sequence Types(Ordered collections)
Collections of items that follow a specific order.
list: A flexible, ordered collection of items. You can add, remove, or change items in a list. It can hold different data types.
Example:
fruits = ["apple", "banana", "orange", 75]
Tuple: Similar to a list, but immutable (you can’t change it after it’s created). It’s faster and safer for fixed collections.
Example:
coordinates = (10.1, 20.5, 30.5)
Range: Represents a sequence of numbers. Commonly used in loops.
Example
range(0, 10) represents numbers from 0 to 9.
4. Mapping Type
Dictionary (dict): Stores key-value pairs. You use keys (unique) to access the values.
person = {"name": "John", "age": 35}
To access the name:
person["name"] gives "John".
5. Set Types (Unordered collections)
Collections where each item must be unique.
Set: An unordered collection of unique items (no duplicates allowed).
my_set = {1, 2, 3, "apple"}
Frozen Set: An immutable version of a set. Once created, it cannot be changed.
frozen_set = frozenset([1, 2, 3])
6. Boolean Type
Boolean (bool): Represents True
or False
Example:
is_loggedin= true, is_open = false
7. Binary Types
binary types are used to handle and manipulate binary data (sequences of bytes). Binary data typically refers to raw data in its byte form rather than human-readable text. Python provides two primary binary types to represent and work with binary data:
Bytes:
1. An immutable sequence of bytes.
2. Each element in an bytes
object is an integer ranging from 0 to 255, representing an 8-bit byte.
3. Useful for representing raw binary data, such as reading from or writing to a file in binary mode or handling binary network protocols.
4. You define bytes
by prefixing a string literal with a b
.
binary_data = b"hello" # 'b' indicates this is a bytes object
print(binary_data) # Output: b'hello'
print(binary_data[0]) # Output: 104 (ASCII value of 'h')
Bytearray:
1. A mutable sequence of bytes.
2. Similar to bytes
, but allows for modification of its elements.
3. Often used when you need to manipulate or modify binary data, such as changing bytes or appending new data.
4. You can create a bytearray
from an bytes
object or from an iterable of integers.
Example:
mutable_binary_data = bytearray(b"hello")
print(mutable_binary_data) # Output: bytearray(b'hello')
# Modify the first byte
mutable_binary_data[0] = 72 # ASCII value of 'H'
print(mutable_binary_data) # Output: bytearray(b'Hello')
Memoryview:
1. A memoryview object exposes the buffer interface, allowing you to work with large datasets without copying the data.
2. Useful for slicing and modifying parts of large binary objects without creating new copies.
Example
binary_data = b"hello world"
mv = memoryview(binary_data)
print(mv[0:5]) # Output:
print(mv.tobytes()) # Output: b'hello world'
8. None Type
None: Represents the absence of a value or a null value. It is often used as a default value or a placeholder.
Example:
result = None