-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuples.py
More file actions
118 lines (81 loc) · 1.57 KB
/
Copy pathtuples.py
File metadata and controls
118 lines (81 loc) · 1.57 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#nornal tuple
m = (1, 2, 3)
#tuple with different typles
info = ("Antonio", 24, "CPT", True)
#single-element tuple
x = (10,) # comma required
#tuple without parenthesis
t = 1, 2, 3
#Packing & unpacking tuples
#packing
t = 1, 2, 3
#unpacking
a, b, c = t
print(a, b, c) # 1 2 3
#extended unpacking
a, *b = (1, 2, 3, 4, 5)
print(a) # 1
print(b) # [2, 3, 4, 5]
#Indexing in tuples
n = (10, 20, 30, 40)
print(n[0]) # 10
print(n[-1]) # 40
print(n[2]) # 30
#slicing tuples
f = (10, 20, 30, 40, 50)
print(f[1:4]) # (20, 30, 40)
print(f[:3]) # (10, 20, 30)
print(f[::2]) # (10, 30, 50)
#allowed tuple manipulation
#concatenation
t1 = (1, 2)
t2 = (3, 4)
print(t1 + t2)
# (1, 2, 3, 4)
#repetition
print((1, 2) * 3)
# (1, 2, 1, 2, 1, 2)
#check if elements exists
t = (1, 2, 3)
print(2 in t) # True
#count elements
print((1,2,2,3).count(2)) # 2
#find index
print((10, 20, 30).index(20)) # 1
# tuple basics
t = (10, 20, 30, 40)
print("First element:", t[0])
print("Slice:", t[1:3])
print("Concatenation:", t + (50, 60))
print("Count of 20:", t.count(20))
print("Index of 30:", t.index(30))
# unpacking
a, b, c, d = t
print("Unpacked:", a, b, c, d)
#list manipulation
nums.append(50)
#insert
nums.insert(1, 999)
#extend
nums.extend([60, 70])
#remove by value
nums.remove(20)
#pop by index
nums.pop(2)
#delete item
del nums[1]
#clear ist
nums.clear()
#other useful list methods
#sorting
nums.sort()
#reverse
nums.reverse()
#count
nums.count(10)
#index
nums.index(30)
#list comprehension
squares = [x*x for x in range(5)]
print(squares)
# [0, 1, 4, 9, 16]