-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuple.py
More file actions
46 lines (34 loc) · 901 Bytes
/
tuple.py
File metadata and controls
46 lines (34 loc) · 901 Bytes
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
# creating a tuple
colours=("red","yellow","green")
# creating a tuple with 1 items
# fruit=("apple",)
fruit=tuple(("apple"))
# check a type tuple
print(type(fruit))
# check the length of tuole
print(len(colours))
# accessing item in tuple
print(colours[1]) #+sitive indexing
print(colours[-2]) #-itive indexing
print(colours[1:3]) #range indexing
print(colours[-2:]) #negtive range indexing
#check if an exists in tuple
if "green" in colours:
print("green is part of tuple")
# traverse the tuple
for i in colours:
print(i)
#concatenate 2 tuple
more_colour=("blue","brown")
colours=colours + more_colour
print(colours)
#unpacking a tuple
# colour1, colour2, colour3= colours
# print(colour1,colour2,colour3)
#reverse tuple
n=(1,2,3,4,5,6)
list=[]
for x in reversed (n):
list.append(x)
output=tuple(list)
print(output)