Data Type in Python
Data Type in Python
x = 20 int
x = 20.5 float
x = 1j complex
x = range(6) range
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType
Setting the Specific Data Type
Tuples
Description: Ordered, immutable collections of items. Tuples allow duplicates.
Example:
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple) # Output: (1, 2, 3, 4, 5)
# Tuples are immutable, so you cannot modify them after creation.
Sets
Description: Unordered collections of unique items. Sets do not allow
duplicates and do not maintain order.
Example:
my_set = {1, 2, 3, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5}
my_set.add(6) # Adds 6 to the set
print(my_set) # Output: {1, 2, 3, 4, 5, 6}
my_set.add(3) # No effect, as 3 is already in the set
print(my_set) # Output: {1, 2, 3, 4, 5, 6}
Dictionaries
Description: Unordered collections of key-value pairs. Keys must be unique
and immutable, while values can be of any data type.
Example:
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3}
my_dict['d'] = 4 # Adds a new key-value pair
print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Collections from the collections module:
NamedTuple
Description: Subclass of tuples with named fields.
Example:
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
print(p) # Output: Point(x=10, y=20)
print(p.x) # Output: 10
Deque
Description: Double-ended queue; a list-like container with fast appends and
pops from both ends.
Example:
from collections import deque
d = deque([1, 2, 3])
d.append(4) # Adds 4 to the right end
d.appendleft(0) # Adds 0 to the left end
print(d) # Output: deque([0, 1, 2, 3, 4])
d.pop() # Removes 4 from the right end
d.popleft() # Removes 0 from the left end
print(d) # Output: deque([1, 2, 3])
Counter
Description: A dict subclass for counting hashable objects.
Example:
from collections import Counter
c = Counter('gallahad')
print(c) # Output: Counter({'a': 3, 'l': 2, 'g': 1, 'h': 1, 'd': 1})
OrderedDict
Description: A dict subclass that remembers the order of keys.
Example:
from collections import OrderedDict
d = OrderedDict(a=1, b=2, c=3)
print(d) # Output: OrderedDict([('a', 1), ('b', 2), ('c', 3)])
d.move_to_end('b') # Moves 'b' to the end
print(d) # Output: OrderedDict([('a', 1), ('c', 3), ('b', 2)])
Note: Each of these collection types has its own strengths and is suited for
different use cases, so it's useful to choose the one that best fits the needs of
your application.
In Details
Delete Tuples
We cannot delete individual items of a tuple. However, we can delete the tuple
itself using the del statement. For example,
Example Usage
# Creating a list
my_list = [1, 2, 3, 4, 5]
print(my_list) # Output: [1, 2, 3, 4, 5]
# Accessing elements
print(my_list[0]) # Output: 1
print(my_list[2]) # Output: 3
# Modifying elements
my_list[1] = 10
print(my_list) # Output: [1, 10, 3, 4, 5]
# Adding elements
my_list.append(6) # Adds 6 to the end of the list
print(my_list) # Output: [1, 10, 3, 4, 5, 6]
# Removing elements
my_list.remove(10) # Removes the first occurrence of 10
print(my_list) # Output: [1, 20, 3, 4, 5, 6]
# Slicing lists
sub_list = my_list[1:4] # Gets elements from index 1 to 3 (excluding 4)
print(sub_list) # Output: [20, 3, 5]
# List comprehension
squared_numbers = [x**2 for x in range(5)]
print(squared_numbers) # Output: [0, 1, 4, 9, 16]
Frozenset
A frozenset in Python is a built-in data type that is similar to a set, but with a
key difference: it is immutable. This means that once a frozenset is created, its
contents cannot be changed—no additions, deletions, or updates are allowed.
This immutability makes frozensets hashable, which means they can be used as
keys in dictionaries or elements in other sets, whereas regular sets cannot.
Key Characteristics of frozenset
1. Immutable: You cannot modify the frozenset after it has been created.
This includes adding or removing elements.
2. Hashable: Because frozenset is immutable, it can be used as a dictionary
key or as an element of another set.
3. Unordered: Like regular sets, frozensets do not maintain order among
elements.
Creating a frozenset
You create a frozenset using the frozenset() constructor, which can take an
iterable (like a list, tuple, or set) as an argument.
# It returns True if all the items of a set (frozenset) are common in another set
(frozenset)
Subset = numbers1.issubset(numbers2)
print("Subset:", Subset)
# It returns True if a set (frozenset) has all items present in another set
(frozenset)
Superset = numbers1.issuperset(numbers2)
print("Superset:", Superset)