-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTuple_Set.py
More file actions
62 lines (49 loc) · 1.53 KB
/
Tuple_Set.py
File metadata and controls
62 lines (49 loc) · 1.53 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Tuple is similar to List, but the difference is that Tuples are immutable (cannot be changed)
my_tuple = ('Ali', 21, True) # simple tuple
print(my_tuple)
print(my_tuple[1]) # it will give the value at index 1
print(my_tuple[0]) # it will give the value at index 0
# Looping over a tuple
for item in my_tuple:
print(item)
# Convert tuple to list
new_list = list(my_tuple)
print(new_list)
# Convert list back to tuple
new_tuple = tuple(new_list)
print(new_tuple)
# Tuple slicing
print(new_tuple[0:1])
print(new_tuple[1:])
# Tuple concatenation
car_tuple = ('Honda', 'Kia', 'BMW', 'BMW', 'GMC')
all_tuple = car_tuple + new_tuple
print(all_tuple)
# Conditional check in tuple
if 'Honda' in all_tuple:
print('Yes, Honda is present')
# Count method in tuple
print(all_tuple.count('BMW')) # how many times 'BMW' appears
# Tuple unpacking
color = ('red', 'green', 'black')
(red, green, black) = color
print(red)
# Nested tuple access
nested_tuple = (
'red',
'green',
('red', ('red', 'green', 'black'), 'green', 'black'),
'black'
)
print(nested_tuple[2][1][0]) # Accessing nested tuple value
#memebership testing
print(f"check it available or not: {"red" in nested_tuple}") # it will show in boolean
# Set
any_set_A = {"ali" , 'Haider' ,2,4,5,6,7}
any_set_B = {"ali" , "car" , 0 ,8,7,6,5}
Set_C = any_set_A | any_set_B # this will make it union all will come in orfer
print(f"set : {Set_C}")
set_D = any_set_A & any_set_B
print(f"set : {set_D}") # only common will come
set_F = any_set_A - any_set_B # neglect B from A
print(set_F)