-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path17_Tuples.py
More file actions
56 lines (44 loc) · 968 Bytes
/
17_Tuples.py
File metadata and controls
56 lines (44 loc) · 968 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
47
48
49
50
51
52
53
54
55
tup = ("Geeks", "For", "Geeks", 4, 5)
print(type(tup))
# Converting a list to a tuple
lst= [34,234,"akhil", 45.67, 89]
print(tuple(lst))
# using tuple() constructor
a = tuple([2,3,3,32,21,2])
print(a)
# Creating a Tuple with nested tuples
tup1 = (0, 1, 2, 3)
tup2 = ('python', 'geek')
tup3 = (tup1, tup2)
print(tup3)
# Creating a Tuple with repetition
tup4 = ('Geeks',) * 3
print(tup4)
# Creating a Tuple with the use of range
tup5 = tuple(range(5))
print(tup5)
# Accessing elements in a Tuple
tup6 = ('Geeks', 'For', 'Geeks', 4, 5)
print(tup6[0])
print(tup6[-1])
# Accessing a range of elements using slicing
print(tup6[1:4])
print(tup6[:3])
print(tup6[::-1])
# Concatenating two tuples
tup7 = (1, 2, 3)
tup8 = (4, 5, 6)
tup9 = tup7 + tup8
print(tup9)
# Tuple unpacking
tup10 = ("Geeks", "For", "Geeks")
# This line unpack values of Tuple1
a, b, c = tup10
print(a)
print(b)
print(c)
tup11 = (1, 2, 3, 4, 5)
a, *b, c = tup11
print(a)
print(b)
print(c)