-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_experimentation.py
238 lines (186 loc) · 5.57 KB
/
python_experimentation.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 9 18:09:07 2018
@author: Veeranjaneyulu Toka
"""
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x== pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left)+middle+quick_sort(right)
def basic_data_types():
print("numbers")
def numbers():
x = 3
print(type(x))
print(x)
print(x+1)
print(x-1)
print(x*2)
print(x**2)
x+= 1
print(x)
x*=2
print(x)
y = 2.5
print(type(y))
print(y, y+1, y-1, y*2, y**2)
numbers()
print("Booleans")
def booleans():
t = True
f = False
print(type(t))
print(t and f)
print(t or f)
print(not t)
print(t != f)
booleans()
print("strings")
def strings_v():
hello = 'hello'
world = 'world'
print(hello)
print(len(hello))
hw = hello + ' ' + world
print(hw)
hw12 = '%s %s %s'%(hello, world, 12)
print(hw12)
strings_v()
print("string functions")
def string_func():
s = 'hello'
print(s.capitalize())
print(s.upper())
print(s.rjust(7))
print(s.center(7))
print(s.replace('l', '(ell)'))
print('world '.strip())
string_func()
def containers_p():
print('lists in python')
def lists_p():
xs = [3, 1, 2]
print(xs, xs[2])
print(xs[-1])
xs[2] = 'foo'
print(xs)
xs.append('bar')
print(xs)
x = xs.pop()
print(x, xs)
print('slicing')
def slicing_lists_p():
nums = list(range(5))
print(nums)
print(nums[2:4])
print(nums[2:])
print(nums[:2])
print(nums[:])
print(nums[:-1])
nums[2:4]=[8,9]
print(nums)
slicing_lists_p()
print('loops')
def loops_lists_p():
animals = ['cat', 'dog', 'monkey']
for animal in animals:
print(animal)
for idx, animal in enumerate(animals):
print('#%d:%s'%(idx+1, animal))
loops_lists_p()
print("list comprehensions")
def list_comprehensions_p():
nums = [0, 1, 2, 3, 4]
squares = []
for x in nums:
squares.append(x**2)
print(squares)
squares = [x ** 2 for x in nums]
print(squares)
even_squares = [x**2 for x in nums if x%2 == 0]
print(even_squares)
list_comprehensions_p()
lists_p()
print("dictionaries in python")
def dict_p():
d = {'cat': 'cute', 'dog': 'furry'}
print(d['cat'])
print('cat' in d)
d['fish'] = 'wet'
print(d['fish'])
print(d.get('monkey', 'N/A'))
print(d.get('fish', 'N/A'))
del d['fish']
print(d.get('fish','N/A'))
def dict_loops_p():
d = {'person': 2, 'cat':4, 'spider':8}
for animal in d:
legs = d[animal]
print('A %s has %d legs' %(animal, legs))
for animal, legs in d.items():
print('A %s has %d legs' %(animal, legs))
dict_loops_p()
def dict_compreh_p():
nums = [0, 1, 2, 3, 4]
even_num_to_square = {x:x**2 for x in nums if x%2 == 0}
print(even_num_to_square)
dict_compreh_p()
dict_p()
print("sets in python")
def sets_p():
animals = {'cat', 'dog'}
print('cat' in animals)
print('fish' in animals)
animals.add('fish')
print('fish' in animals)
print(len(animals))
animals.add('cat')
print(len(animals))
animals.remove('cat')
print(len(animals))
def sets_loops_p():
animals = {'cat', 'dog', 'fish'}
for idx, animal in enumerate(animals):
print("%d :%s" %(idx+1, animal))
sets_loops_p()
sets_p()
print("touples in python")
def tuples_p():
d = {(x, x+1): x for x in range(10)}
t = (5, 6)
print(type(t))
print(d[t])
print(d[(1, 2)])
tuples_p()
def sign_func(x):
if x > 0:
return 'positive'
elif x < 0:
return 'negative'
else:
return 'zero'
class Generator(object):
def __init__(self, name):
self.name = name
def greet(self, loud=False):
if loud:
print("HELLO, %s!" % self.name.upper())
else:
print("Hello, %s" %self.name)
def main():
print(quick_sort([3, 6, 8, 10, 1, 2, 1]))
basic_data_types()
containers_p()
#functions
for x in [-1, 0, 1]:
print(sign_func(x))
#classes
g = Generator('Fred')
g.greet()
g.greet(loud=True)
if __name__ == "__main__":
main()