Tuple: Tuples are an ordered sequence.
Ratings = (10, 9, 6, 5, 10)
tpl1 = (‘Hello’, 9, 0.5)
type(tpl1) -> tuple
tpl1[0] -> Hello
-3 | 0 | Hello |
-2 | 1 | 9 |
-1 | 2 | 0.5 |
tpl1[-3] = Hello
tpl2 = tpl1 + (“Hi”, 10)
tpl2 = (‘Hello’, 9, 0.5, ‘Hi’, 10)
Now,
tp1 = (‘Hello’, 9, 0.5)
tp2 = tp1 + (1)
print(tp2)
But this will give TypeError as it can only concatenate tuple. Not int to tuple. So single item in tuple gives an error whether it is string, float or int.
So, tp1 = (1) -> type(tp1) = int
tp1 = (‘Hi’) -> type(tp1) = str
tp1 = (9.9) -> type(tp1) = float
Now, tpl2 = (‘dance’, 9, 0.5, ‘hip hop’, 99)
tpl2[0:3] -> (‘dance’, 9, 0.5)
tpl2[3:5] -> (‘hip hop’, 99)
len(tpl2) -> 5
Note: Tuples are immutable.
Rating = (10, 9, 6, 5, 10, 8, 9, 6, 2)
RatingSorted = sorted(Rating)
RatingSorted = (2, 5, 6, 6, 8, 9, 9, 10, 10)
Tuples Nesting:
NT = (1,2, (‘pop’, ‘song’), (3,4), (‘dance’, (8, 9)))
NT[2] = (‘pop’, ‘song’)
NT[2][1] = ‘song’
List: Lists are also ordered sequences.
L = [“Captain Planets”, 9.99, 9]
Major difference between list and tuple is that list is mutable.
List can also contain tuples.
Lst = [“Hello”, 9.9, 007, [1,2], (‘Hi’, 5) ]
Lst= [“Hello”, 1, 2 ]
-3 | 0 | Hello |
-2 | 1 | 1 |
-1 | 2 | 2 |
Lst.extend([“World”, 365 ])
So, Lst = [“Hello”, 1, 2, [“World”, 365]]
A = [“Hi”, 1, 2]
A[0] = “Hello”
A = [“Hello”, 1, 2]
del(A[1])
A = [“Hello”, 2]
Convert string to list
“Hello World”.split() = [“Hello”, “World”]
“A, B, C, D”.split(“,”) = [“A”, “B”, “C”, “D”]
Aliasing
A =[“Hi”, 9, 1.1]
B = A (reference is created)
A —————-> [“Hi”, 9, 1.1] <———————- B
A & B both are alias.
So, A[0] = “Hello”
Then , B[0] also becomes “Hello”
A =[“Hello”, 9, 1.1]
B =[“Hello”, 9, 1.1]
Clone list
A = [“Hi”, 9, 365]
B = A[:]
So,
A -> [“Hi”, 9, 365]
B -> [“Hi”, 9, 365]
Now A[0] = “Hello”
So, now A[0] becomes “Hello” but B[0] remains “Hi”
A -> [“Hello”, 9, 365]
B -> [“Hi”, 9, 365]
Dictionary: Type of collection
Key is index by label
key1 | value1 |
key2 | value2 |
key3 | value3 |
… | … |
Dictionaries denoted by { }
Keys have to be immutable and unique.
{“key1”:9, “key2”:”hello”, “key3”:[9, 9], “key4”: (7, 8, 9), (‘key5’):5}
Value can immutable, mutable & duplicates.
Key Value
Hi | 1960 |
Hello | 1980 |
Bye | 2100 |
DICT[“Hello”] = 1980
DICT[“Hi”] = 1960
Adding new value to dictionary DICT[“Again”, 2110]
Hi | 1960 |
Hello | 1980 |
Bye | 2100 |
Again | 2110 |
del(DICT(“Hi”)) -> deletes both – Hi key entry & value of Hi key
‘Hi’ in DICT -> returns true if found else returns false.
DICT.keys() = [“Hi”, “Hello”, “Bye”, “Again”]
DICT.values() -> [1960, 1980, 2100, 2110] returns values list
Sets : also type of collection
Unlike lists & tuples, they are unordered.
Sets only have unique element.
Sets uses { } bracket.
st = {“A”, “A”}
when created set st has {“A”}.
Converting list to set called type casting.
A = [“Hello”, 99, “Hello”, 1.0]
Aset = set(A)
Aset = { “Hello”, 99, 1.0}
Set operation
A.add(“World”) -> adding new element
A.remove(“Hello”) -> removing element
‘Hello’ in A -> if in set returns true else returns false
A = {“Hi”, “Hello”, “Again”}
B = {“Bye”, “Hi”}
A&B -> intersection
A.union(B) -> union
B.issubset(A) -> subset check
String to List convert
spacelight = ‘Earth is our home planet’
print(‘Given string:’, spacelight)
lst= spacelight.split(‘ ‘)
print(‘Required List:’, lst)
output
Given string: Earth is our home planet
Required List: [‘Earth’, ‘is’, ‘our’, ‘home’, ‘planet’]