This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.py
318 lines (213 loc) · 6.56 KB
/
learn.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# typescript (superset de js en compilant ts on aura du js) -> js angular node js reactjs = backend
print("hello world")
# ctrl+B to launch it in it is console and ESC to hide it
# espace before print is a identation error , a P maj is a syntax error
print(3)
print(1 + 1)
print((52 * 3) + (12 * 9))
print((365 * 24) * (60 * 60))
speed_of_light = 29979258
print(speed_of_light)
minutes = 60
minutes = minutes + 1
print(minutes)
name = 'amine' " hey there wass'up" ' my name also is " kacem "'
name2 = 'amine'+" hey there wass'up"+' my name also is " kacem "'
print(name + name2)
print("hello " + "!" * 3) # multiply the ! 3 times
name = "Andy"
print(name * 4)
print(name[0]) # print name[-4]
print(name[1]) # print name[-3]
print(name[2]) # print name[-2]
print(name[3]) # print name[-1]
print(name[2:4]) # print the letter from postion 2 to the letter before the postion 4
print(name[0:]) # from pos 0 to the end
print(name[:]) # from begining to the end
print(name[:3]) # selecting from the begining to the letter before 3
s = 'audacity'
print(s[1].upper()+s[2:]) # first letter maj concatinate to the rest
print(s.find('cit')) # search for 1 occuerence of that and give its postion
print(s.find('a', 0)) # from 0 start searching
print(s.find('a', 1)) # from 1 it starts searching
print(s.find('a', 4))
s += "mino"
print(s)
text = "zip files are zipped"
firstzip = text.find('zip')
print(text.find('zip', firstzip+1)) # to find the second occ of zip
print(text.find('zip', text.find('zip')+1))
sentence = "A NOUN went on a walk. They can VERB really well."
sentence = sentence.replace("NOUN", "duck") # fill this in
sentence = sentence.replace("VERB", "waddle") # fill this in
print(sentence)
def say_hello(n):
greeting = "Hello " + n + "!"
return greeting
print(say_hello("Miriam"))
print(2 < 3)
print(7*3 <= 21)
print(2 != 2)
def big(a, b):
if a > b:
return a
return b
print(big(4, 6))
def friend(n):
return n[0] == 'D'
def friend2(n):
if n[0] == 'a':
return True
else:
if n[0] == 'b':
return True
else:
return False
print(friend("Diane"))
print(friend2("ared"))
def biggest(a, b, c):
if a > b:
if a > c:
return a
else:
return c
else:
if b > c:
return b
else:
return c
def biggest2(a, b, c):
return big(big(a, b), c)
# return max(a,b,c)
print(biggest(4, 3, 5))
print(biggest2(4, 5, 11))
def median(a, b, c):
bigi = biggest(a, b, c)
if bigi == a:
return big(b, c)
else:
if bigi == b:
return big(a, c)
else:
return big(a, b)
print("------------------------median----------------")
print(median(7, 8, 7))
# This code demonstrates a while loop with a "counting variable"
i = 0
while i < 10:
print(i)
i = i+1
# This uses a while loop to remove all the spaces from a string of
# text. Can you figure out how it works?
def remove_spaces(text):
text_without_spaces = '' # empty string for now
while text != '':
next_character = text[0]
if next_character != ' ': # that's a single space
text_without_spaces = text_without_spaces + next_character
text = text[1:]
return text_without_spaces
print(remove_spaces("hello my name is andy how are you?"))
def numbers(x):
i = 1
while i <= x:
if i > x:
break
print(i)
i = i + 1
print(numbers(4))
def printinches(n):
x = str(n) + ' inches'
print(x)
# (int)n , (str)n -> conversion
printinches(42)
def remove(somestring, sub):
location = somestring.find(sub)
if location == -1:
return somestring
length = len(sub)
part_before = somestring[:location]
part_after = somestring[location + length:]
return part_before + part_after
print(remove('substring institution', 'string in'))
print(remove('ding', 'do')) # "do" isn't in "ding"; should print "ding"
def weekend(day):
return day is 'Saturday' or day == 'Sunday'
print(weekend('Monday'))
print(weekend('Saturday'))
print(weekend('July'))
# Let's put it all together. Write code for the function process_madlib, which takes in
# a string "madlib" and returns the string "processed", where each instance of
# "NOUN" is replaced with a random noun and each instance of "VERB" is
# replaced with a random verb. You're free to change what the random functions
# return as verbs or nouns for your own fun, but for submissions keep the code the way it is!
from random import randint
def random_verb():
random_num = randint(0, 1)
if random_num == 0:
return "run"
else:
return "kayak"
def random_noun():
random_num = randint(0, 1)
if random_num == 0:
return "sofa"
else:
return "llama"
def word_transformer(word):
if word == "NOUN":
return random_noun()
elif word == "VERB":
return random_verb()
else:
return word[0]
def process_madlib(madlib):
p = ""
i = 0
box = 4
while i < len(madlib):
frame = madlib[i: i + box]
add = word_transformer(frame)
p += add
if len(add) == 1:
i += 1
else:
i += 4
return p
# documentation: https://docs.python.org/2/library/functions.html#len
test_string_1 = "This is a good NOUN to use when you VERB your food"
test_string_2 = "I'm going to VERB to the store and pick up a NOUN or two."
print(process_madlib(test_string_1))
print(process_madlib(test_string_2))
string_list = ['HTML', 'CSS', 'Python']
print (string_list)
number_list = [3.14159, 2.71828, 1.61803]
print (number_list)
pi = number_list[0]
not_pi = number_list[1:]
print (pi)
print (not_pi)
mixed_list = ['Hello!', 42, "Goodbye!"]
print (mixed_list)
list_with_lists = [3, 'colors:', ['red', 'green', 'blue'], 'your favorite?']
print (list_with_lists)
days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]
def how_many_days(month_number):
return days_in_month[month_number-1]
print (how_many_days(1))
print (how_many_days(9))
nested_list = [['HTML', 'Hypertext'],['CSS' , 'Cascading'],['Python', 'programming language'],['Lists', 'Lists are a data structure']]
first_concept = nested_list[0]
print (first_concept)
first_title = first_concept[0]
first_description = first_concept[1]
print (first_title)
print (first_description)
spy = [0, 0, 7]
agent = spy
spy [2] = agent [2] + 1
print (agent, spy)
def replace_spy(list):
list [2] = list [2] + 1
return list
print (replace_spy(agen ksh ma y3awnkt))