-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex37.py
151 lines (143 loc) · 2.5 KB
/
ex37.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
and
del #remove slices from list, or whole variables
from #from x import y
not
while
as #relabeling to perform try
with open("hello.txt", "wb") as f:
f.write("Hello Python!\n")
equiv to
f = open("hello.txt", "wb")
try:
f.write("Hello Python\n")
finally:
f.close()
elif
global #declaration for entire current code block, listed identifiers as globals
or
with #
assert #checks condition, true => nothing, false => raise AssertionError
else
if
pass #placeholder for when require syntax
def f(arg): pass
yield #normal function / subroutines
#function creates a series of values
#transfer temporary control back to point of call
#save the work of the function
#generators / coroutines
#yield just as return for generator functions
def simple_generator_function():
yield 1
yield 2
yield 3
for value in simple_generator_function():
print(value)
-> 1, 2, 3
our_generator = simple_generator_function()
next(our_generator)
-> 1
next(our_generator)
-> 2
next(our_generator)
-> 3
def get_primes(number):
while True:
if is_prime(number):
yield number
number += 1
#reach end of definition / return
#StopIteration exception raised
def get_primes(number):
while True:
if is_prime(number):
number = yield number
number += 1
def print_successive_primes(iterations, base=10):
prime_generator = get_primes(base)
prime_generator.send(None) #execute code from generator to first yield
for power in range(iterations):
print(prime_generator.send(base**power)) #send value to generator and returns value yielded by generator
break #terminates loop
except
import
print
class: #declare class
exec #execute a file / string of python code / code created by compile function
in
raise #raise errors
continue #continue next iteration loop
finally #try... except... finally...
is #identity comparison
return
def: #define function:
for
lambda: #create anonymous functions
lamda x: x%3 == 0
try
True
False
None
strings
numbers
floats
lists
\\
\'
\"
\a
\b
\f
\n
\r
\t
\v
%d
%i
%o
%u
%x
%X
%e
%E
%f
%F
%g
%G
%c
%r
%s
%%
+
-
*
** #power
/
// #floor division
%
<
>
<=
>=
==
!=
<> #similar to !=
( )
[ ]
{ }
@ #applies a decorator to a function
#decorator is a callable that takes a function as an argument and returns a replacement function
#(function within a function)
#simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
,
:
.
=
;
+=
-=
*=
/=
//=
%=
**=