-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuple.py
More file actions
76 lines (47 loc) · 1.63 KB
/
tuple.py
File metadata and controls
76 lines (47 loc) · 1.63 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# A tuple is a collection which is ordered and unchangeable.
# Ordered
# Unchangeable
# Allow Duplicates
# Since tuples are indexed, they can have items with the same value:
tup = (1,2,3,4,5)
# tup = (1,)
# If you are making tuple of size 1 then , is necessary other wise interpreter get confused
# List Are Changable
# list1 = [1,2,3,4,5,6]
# list1[4] = 89
# print(list1)
# Tuple is unchangle
# tup[0] = 11 -> this is not supported
print(type(tup))
print(tup)
# Access Tuple by Index
print(tup[1])
print(tup[2:5])
# Change Tuple Values
# Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.
# But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.
tup1 = (1,2,3,4,5)
l1 = list(tup1)
l1[3] = 98
tup1 = tuple(l1)
print(tup1)
# Add Items in tuple -> First Convert it to list then insert an item in it
tupl = list(tup1)
tupl.append(89)
tup1 = tuple(tupl)
print(tup1)
# 2. Add tuple to a tuple. You are allowed to add tuples to tuples, so if you want to add one item, (or many), create a new tuple with the item(s), and add it to the existing tuple:
tup_check1 = (1,2,3)
tup_check2 = ("lokesh",)
tup_check1 += tup_check2
print(tup_check1)
# For Removing item from Tuple you have to convert it to list and then perform remove operation and after that convert it to tuple
# Del keyword in tuple -> delete the whole tuple
print(tup1)
del tup1
# Tuple Methods
# Count a particular element in the tuple
t = (1,2,3,4,5,6,7,8,98,88,8,8)
print(t.count(8))
# Find the index of particular element in tuple
print(t.index(88))