-
Notifications
You must be signed in to change notification settings - Fork 10
/
15-Tuple.py
166 lines (149 loc) · 3.59 KB
/
15-Tuple.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#Using python to manipulate tuples
'''
Python Tuple is used to store the sequence of immutable Python
objects.
Tuples are written as a list of "comma-separated"
values (items) between parentheses. ()
Tuples are immutable - this means that items can not be changed.
However,a tuple can contain mutable objects.
Tuple has 2 methods available.
count() Returns the number of elements with the specified value
index() Returns the index of the first element with the specified value
'''
#The basics - tuple packing
t = 12345, 54321, 'hello!'
t[0]
t
type(t)
T1 = (101, "Peter", 22)
T2 = ("Apple", "Banana", "Orange")
T3 = 10,20,30,40,50
print(type(T1))
print(type(T2))
print(type(T3)) # Class Tuple
#The tuple which is created without using parentheses
# is also known as tuple packing.
a = 'abc', 2, 4, 'd'
print(type(a)) # Class Tuple
#An empty tuple can be created as follows.
T4 = ()
#Creating a tuple with single element is slightly different.
# We will need to put comma after the element to declare the tuple
tup1 = ("JavaTpoint") #String
print(type(tup1))
#Creating a tuple with single element
tup2 = ("JavaTpoint",) #Tuple
print(type(tup2))
'''
Output
<class 'str'>
<class 'tuple'>
'''
#Example 1
tuple1 = (10, 20, 30, 40, 50, 60)
print(tuple1)
count = 0
for i in tuple1:
print("tuple1[%d] = %d"%(count, i))
count = count+1
'''
(10, 20, 30, 40, 50, 60)
tuple1[0] = 10
tuple1[1] = 20
tuple1[2] = 30
tuple1[3] = 40
tuple1[4] = 50
tuple1[5] = 60
'''
#Example 2
tuple1 = tuple(input("Enter the tuple elements ..."))
print(tuple1)
count = 0
for i in tuple1:
print("tuple1[%d] = %s"%(count, i))
count = count+1
'''
Output
Enter the tuple elements ...123456
('1', '2', '3', '4', '5', '6')
tuple1[0] = 1
tuple1[1] = 2
tuple1[2] = 3
tuple1[3] = 4
tuple1[4] = 5
tuple1[5] = 6
'''
#indexing
tuple = (1,2,3,4,5,6,7)
#element 1 to end
print(tuple[1:])
#element 0 to 3 element
print(tuple[:4])
#element 1 to 4 element
print(tuple[1:5])
# element 0 to 6 and take step of 2
print(tuple[0:6:2])
'''
Output
(2, 3, 4, 5, 6, 7)
(1, 2, 3, 4)
(1, 2, 3, 4)
(1, 3, 5)
'''
#Negative Indexing
tuple1 = (1, 2, 3, 4, 5)
print(tuple1[-1])
print(tuple1[-4])
print(tuple1[-3:-1])
print(tuple1[:-1])
print(tuple1[-2:])
'''
Output
5
2
(3, 4)
(1, 2, 3, 4)
(4, 5)
'''
#Deleting Tuple
'''
Unlike lists, the tuple items cannot be deleted by using the del keyword
as tuples are immutable. To delete an entire tuple, we can use the del
keyword with the tuple name.
'''
tuple1 = (1, 2, 3, 4, 5, 6)
print(tuple1)
del tuple1[0]
print(tuple1)
del tuple1
print(tuple1)
'''
Output
(1, 2, 3, 4, 5, 6)
Traceback (most recent call last):
File "tuple.py", line 4, in <module>
print(tuple1)
NameError: name 'tuple1' is not defined
'''
#Python Tuple inbuilt functions
'''
SN Function Description
1 cmp(tuple1, tuple2) It compares two tuples and returns true
if tuple1 is greater than tuple2 otherwise false.
2 len(tuple) It calculates the length of the tuple.
3 max(tuple) It returns the maximum element of the tuple
4 min(tuple) It returns the minimum element of the tuple.
5 tuple(seq) It converts the specified sequence to the tuple.
'''
#Where use tuple?
'''
1. Using tuple instead of list gives us a clear idea that tuple data
is constant and must not be changed.
2. Tuple can simulate a dictionary without keys. Consider the following
nested structure, which can be used as a dictionary.
[(101, "John", 22), (102, "Mike", 28), (103, "Dustin", 30)]
'''
# Tuples may be nested:
v = ([1, 2, 3], [3, 2, 1])
u = v, (1, 2, 3, 4, 5)
u