-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths20_advanced_python_objects.py
More file actions
147 lines (99 loc) · 1.98 KB
/
s20_advanced_python_objects.py
File metadata and controls
147 lines (99 loc) · 1.98 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Advanced numbers
# Hex representation
print(hex(12))
# Binary representation
print(bin(12))
# Equivalent of 2**4
print(pow(2, 4))
# Absolute value
print(abs(-3))
# Round
print(round(3.141, 2))
# Advanced strings
s = 'hello world'
print(s.capitalize())
print(s.upper())
print(s.lower())
# Count o in the string
print(s.count('o'))
# Print index of the first 'o'
print(s.find('o'))
# Center 'hello world' between z's
print(s.center(20, 'z'))
'hello\thi'.expandtabs()
st = 'hello'
# Check if the string is aplhanumeric
print(st.isalnum())
# Check if the string is alpha
print(st.isalpha())
# Check if it is in lowercase
print(s.islower())
# Check if there is whitespace
print(s.isspace())
txt = "Hello, And Welcome To My World!"
print(txt.istitle())
st = 'HELLO'
print(st.isupper())
print(st.endswith('O'))
# Split will do on every instance
print(s.split('e'))
# Partition - on first instance
print(s.partition('e'))
# Advanced sets
s = set()
s.add(1)
s.add(2)
s.add(2)
print(s)
sc = s.copy()
print(sc)
s.add(4)
print(s.difference(sc))
s1 = {1, 2, 3}
s2 = {1, 4, 5}
s1.difference_update(s2)
print(s1)
s1.discard(1)
print(s1)
s1 = {1, 2, 3}
s2 = {1, 2, 5}
# Elements common for both of the sets
s1.intersection(s2)
# Update s1 with common elements of both sets
s1.intersection_update(s2)
print(s1)
s1 = {1, 2}
s2 = {1, 2, 4}
s3 = {5}
print(s1.isdisjoint(s2))
print(s1.isdisjoint(s3))
print(s1.issubset(s2))
print(s2.issuperset(s1))
print(s1.symmetric_difference(s2))
print(s1.union(s2))
# Dictionaries
d = {'k1': 1, 'k2': 2}
print({x: x ** 2 for x in range(10)})
print({k: v ** 2 for k, v in zip(['a', 'b'], range(2))})
for k in d.items():
print(k)
for k in d.values():
print(k)
# Advanced list
l = [1, 2, 3]
l.append(4)
print(l)
print(l.count(10))
print(l.count(1))
# append - [1, 2, 3, [4, 5]]
x = [1, 2, 3]
x.append([4, 5])
print(x)
# extend - [1, 2, 3, 4, 5]
x = [1, 2, 3]
x.extend([4, 5])
print(x)
print(x.index(2))
l.insert(2, 'inserted')
print(l)
print(l.reverse())