Repository navigation
Expand file tree
/
Copy pathTuples.py
More file actions
102 lines (75 loc) · 2.15 KB
/
Copy pathTuples.py
File metadata and controls
102 lines (75 loc) · 2.15 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
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
#A tuple is a collection which is ordered and unchangeable. In Python tuples are written with #round brackets.
#tuple
thistuple = ["apple", "banana", "cherry"]
print(thistuple)
#Access Items
thistuple = ["apple", "banana", "cherry"]
print(thistuple[1])
#Negative Indexing
thistuple = ["apple", "banana", "cherry"]
print(thistuple[-1])
#Range of Indexes
thistuple = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thistuple[2:5])
thistuple = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thistuple[:4])
thistuple = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thistuple[2:])
#Change Item Value
thistuple = ["apple", "banana", "cherry"]
thistuple[1] = "blackcurrant"
print(thistuple)
#Loop Through a tuple
thistuple = ["apple", "banana", "cherry"]
for x in thistuple:
print(x)
#Check if Item Exists
thistuple = ["apple", "banana", "cherry"]
if "apple" in thistuple:
print("Yes, 'apple' is in the fruits tuple")
#tuple Length
thistuple = ["apple", "banana", "cherry"]
print(len(thistuple))
#Add Items
thistuple = ["apple", "banana", "cherry"]
thistuple.append("orange")
print(thistuple)
thistuple = ["apple", "banana", "cherry"]
thistuple.insert(1, "orange")
print(thistuple)
#Remove Item
thistuple = ["apple", "banana", "cherry"]
thistuple.remove("banana")
print(thistuple)
thistuple = ["apple", "banana", "cherry"]
thistuple.pop()
print(thistuple)
thistuple = ["apple", "banana", "cherry"]
del thistuple[0]
print(thistuple)
thistuple = ["apple", "banana", "cherry"]
del thistuple
thistuple = ["apple", "banana", "cherry"]
thistuple.clear()
print(thistuple)
thistuple = ["apple", "banana", "cherry"]
mytuple = thistuple.copy()
print(mytuple)
thistuple = ["apple", "banana", "cherry"]
mytuple = tuple(thistuple)
print(mytuple)
tuple1 = ["a", "b" , "c"]
tuple2 = [1, 2, 3]
tuple3 = tuple1 + tuple2
print(tuple3)
tuple1 = ["a", "b" , "c"]
tuple2 = [1, 2, 3]
for x in tuple2:
tuple1.append(x)
print(tuple1)
tuple1 = ["a", "b" , "c"]
tuple2 = [1, 2, 3]
tuple1.extend(tuple2)
print(tuple1)
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)