-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtuples.py
111 lines (73 loc) · 1.99 KB
/
tuples.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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#Empty tuple
new_tuple = ()
print(new_tuple)
#Tuple with numbers
num_tuple = (1,2,3,4,5)
print(num_tuple)
#Tuple with strings and numbers
mixed_tuples = (1,"Hello",908,9.988)
print(mixed_tuples)
#List inside a tuple - Nested Tuples
list_tuples = (1,2,3,[1,231,2])
print(list_tuples)
#Create a tuple with only on element
element_tuple = ("Hello",)
print(element_tuple)
#packing and unpacking tuples
my_tuple = 1,2,3,"Hello","Wassup"
a,b,c,d,e = my_tuple
print(a)
print(b)
print(c)
print(d)
print(e)
#Accessing elements of tuple
#Three ways to access elements of a tuple
# Indexing
# Negative Indexing
# Slicing
name_tuple = ('a','r','y','a','n')
#get the second element from the tuple
print(name_tuple[1]) # 'r'
#get the last element of the tuple
print(name_tuple[4]) # 'n'
#Get elements from nested tuple
nested_tuple = ("Hello",[1,234,"Yes"],'No')
#Index of each element in the tuple
# Hello 1 234 "Yes" 'No'
# 0 [1][0] [1][1] [1][2] [2]
#get 'Yes' from the tuple
print(nested_tuple[2])
# NEGATIVE INDEXING
neg_tuple = ('a','b','c','d','e')
# a b c d e
# -5 -4 -3 -2 -1
#get the second element of the tuple
print(neg_tuple[-4])
#get the last element of the tuple
print(neg_tuple[-1])
# SLICING
my_tuple = ('a','b','c','d','e')
#print elements b c d
print(my_tuple[1:4])
# Make changes to a Tuple
# you cannot make changes to tuples since they are immutable
#og_tuple = (1,2,3,4,5)
#og_tuple[1] = 3
#print(og_tuple)
# But if you can make changes to nested tuples
my_tuple1 = (1,22,3,[2,34])
#want to replace 2 with 56
my_tuple1[3][0] = 56
print(my_tuple1)
#Tuples cannot be changed but can be reassigned
# print all the elements of a tuple
name_tuple = ("Aryan","Neil","Dev","Rohan")
for name in name_tuple:
print(name)
#Check if element is present in tuple
name_tuple = ("Aryan","Neil","Dev","Rohan")
#Check if Aryan is present in the tuple
print("Aryan" in name_tuple)
#check if x is present in the tuple
print("x" in name_tuple)