-
Notifications
You must be signed in to change notification settings - Fork 1
/
042_Program_Containers In Python.py
31 lines (17 loc) · 1020 Bytes
/
042_Program_Containers In Python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# There are four classes in Python that provide container like behaviour; that is data types for holding collections of other objects, these are - Tuples, Lists, Sets, Dictionary
# TUPLE ( )
# It is not possible to ADD or REMOVE elements from a Tuple; they are IMMUTABLE.
tup1 = (6, 5, 8, 'plum', 7, 4, 'apple') # USE 'ROUND BRACKET'
print(tup1[2]) # accessing the elements of a Tuple using index value
print(tup1[1:3]) # slicing a Tuple using index value
print(len(tup1)) # length of a Tuple
print(tup1.index('plum')) # find the index of an item in a Tuple
list1 = [1, 2, 3]
tup = tuple(list1) # transforming other collection type to 'Tuple'
print(tup)
# Nested Tuples
tuple1 = (1, 3, 5, 7)
tuple2 = ('John', 'Denise', 'Phoebe', 'Adam')
tuple3 = (42, tuple1, tuple2, 5.5)
print(tuple3)
# In fact, a Tuple can have nested within it not just other Tuples but any type of container, and thus it can contain Lists, Sets, Dictionaries etc.