forked from techstay/python-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter4.py
More file actions
92 lines (66 loc) · 1.35 KB
/
chapter4.py
File metadata and controls
92 lines (66 loc) · 1.35 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
# Q1
guess_me = 7
if guess_me < 7:
print('too low')
elif guess_me > 7:
print('too high')
else:
print('just right')
# Q2
start = 1
while True:
if start < guess_me:
print('too low')
elif start == guess_me:
print('found it!')
break
else:
print('oops')
break
start += 1
# Q3
for i in [3, 2, 1, 0]:
print(i)
# Q4
even = [i for i in range(1, 10) if i % 2 == 0]
# Q5
squares = {k: k ** 2 for k in range(1, 10)}
# Q6
odd = {i for i in range(1, 10) if i % 2 != 0}
# Q7
for thing in (f'Got {i}' for i in range(1, 10)):
print(thing)
# Q8
def good():
return ['Harry', 'Ron', 'Hermione']
# Q9
def get_odds():
for i in range(1, 10, 2):
yield i
for count, number in enumerate(get_odds(), 1):
if count == 3:
print(f'Third odd is {number}')
# Q10
def test(func):
def new_func(*args, **kargs):
print('start')
result = func(*args, **kargs)
print('end')
return result
return new_func
@test
def hello():
print('Hello !')
hello()
# Q11
class OopsException(Exception):
pass
try:
raise OopsException()
except OopsException as ex:
print('Caught an oops')
# Q12
titles = ['Creature of Habit', 'Crewel Fate']
plots = ['A nun turns into a monster', 'A haunted yarn shop']
movies = dict(zip(titles, plots))
print(movies)