-
Notifications
You must be signed in to change notification settings - Fork 5
/
ipython_notes.py
268 lines (255 loc) · 5 KB
/
ipython_notes.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
# coding: utf-8
from __future__ import unicode_literals
s = 'abcd1213-=*&^тавдыжжфщушм'
s
s[0]
s[-1]
s[5:10]
'abc' + 'def'
str(1)
str([1, 2, 3, 'hello', (5, 6, 7), {'d', 'e', 'd'}])
s[0] = 'r'
del s[]0
del s[0]
b'abc'
type(b'abc')
b = b'abc'
b + 'abc'
b + b'abc'
b.decode()
get_ipython().run_line_magic('pinfo', 'b.decode')
s
get_ipython().run_line_magic('pinfo', 's.encode')
s.encode()
s = 'Hello'
s2 = "Hello"
s == s2
s
s2
s = 'Hello "World"'
s
s = "Hello 'World'"
s
s = '\''
s
'\'"'
'\'\"'
s =
s = 'Hello\nWorld'
s
print(s)
s = """Hello world"""
s = '''Hellow world'''
s
s = "Hello
s = """Hello
world
"""
s
print(s)
s.__doc__
sorted.__doc__
R''
s = r'\n'
print(s)
r'\n' !=
r'\n' != '\n'
type(r'\n')
'\\n' == r'\n'
x = 10
print('x =', x)
'x = ' + str(x)
'x = {}'.format(x)
'x = {} {}'.format(x, x**2)
'x = {1} {0} {1}'.format(x, x**2)
'x = {1:f} {0} {1}'.format(x, x**2)
'x = {1:f} {0} {1}'.format(x, 3.1415)
'x = {1:f} {0} {1} {{}}'.format(x, 3.1415)
'x = {1:f} {0} {key} {{}}'.format(x, 3.1415, key='default')
'x = {1:f} {0} {key} {{}}'.format(x, 3.1415)
'x = {1:f} {0} {key} {{}}'.format(x, 3.1415, key='default')
'x = {x}, pi = {pi:f}, l = {length}'.format(x=x, pi=3.14, length=2*3.14*x)
'x = {x}, pi = {pi:f}, l = {length:.2f}'.format(x=x, pi=3.14, length=2*3.14*x)
'pyformat.info'
f'x = {x}'
def unity():
x = 1
return x
one = unity()
print(one)
f = unity
f is unity
unity.__class__
a = [unity, 1, 2, 3]
a
def unity():
"""Returns one"""
return 1
get_ipython().run_line_magic('pinfo', 'unity')
unity.__doc__
def unity():
"""Returns one
This function ...
is very fine
"""
return 1
get_ipython().run_line_magic('pinfo', 'unity')
unity.__doc__
def none_function():
return
none_function() is None
def hello_name(name):
print('hello {}'.format(name))
hello_name('Masha')
hello_name('Vasya')
result = hello_name('Vasya')
result is None
def minus(x, y):
return x - y
x = 5
y = 3
minus(x, y)
minus(y, x)
minus(x=x, y=y)
get_ipython().run_line_magic('pinfo', 'minus')
minus(x=10, y=33)
minus(y=33, x=10)
get_ipython().run_line_magic('pinfo', 'sorted')
get_ipython().run_line_magic('pinfo', 'pow')
pow(2, 3, 4)
pow(2, 3)
pow(x=2, y=3)
pow(2, 3, z=4)
def hello_user(user='%username%'):
return 'hello {}'.format(user)
hello_user()
hello_user(user='Masha')
hello_user('Masha')
minus()
minus(x=1, 2)
def check_between(value, minimum=-1, maximum=1):
return minumum <= value <= maximum
check_between(10)
def check_between(value, minimum=-1, maximum=1):
return minimum <= value <= maximum
check_between(10)
check_between(10, minimum=-100, maximum=500)
check_between(10, -100, 500)
def check_between(value, *, minimum=-1, maximum=1):
return minimum <= value <= maximum
def check_between(value=0, *, minimum=-1, maximum=1):
return minimum <= value <= maximum
check_between()
check_between(0.5)
check_between(value=0.5)
check_between(0.5, -1, 1)
check_between(0.5, minimum=-10, maximum=100)
get_ipython().run_line_magic('pinfo', 'sorted')
check_between(maximum=100, value=0, minimum=33)
def f(a=1):
pass
f()
f.__defaults__
f.__defaults__[0]
import inspect
def f(a=[1]):
pass
f.__defaults__
def f(a=[1]):
a.append(a[-1] + 1)
def f(a=[1]):
a.append(a[-1] + 1)
return a
f()
f()
f()
f()
f().append(-100)
f()
class FunctionWithMemory:
__used_arguments = []
class FunctionWithMemory:
__used_arguments = []
class FunctionWithMemory:
__used_arguments = []
def __call__(self, arg):
import copy
self.__used_arguments.append(arg)
return copy.deepcopy(self.__used_arguments)
f = FunctionWithMemory()
f()
f(1)
f(3.14)
f(5.5)
def pair():
return 1, 2
pair()
type(pair())
def pair():
return 1,
pair()
def pair():
return (1 + 3)
pair()
def pair():
return 1, 2, 3
pair()
x, y, z = pair()
def f(a, b, c, x='x', y='y', z='z'):
pass
f(1, 2, 3, y=100, z=500)
t = (1, 2, 3)
def f(a, b, c, x='x', y='y', z='z'):
print(a, b, c, x, y, z)
f(t[0], t[1], t[2])
f(*t)
f(*[1, 2, 3])
d = {x: 1, y: 2, z: 3}
f(*t, **d)
d = {'x': 1, 'y': 2, 'z': 3}
f(*t, **d)
d = {'x': -100}
f(*t, **d)
f(*[1, 2, 3, 4])
f(**{'a': -1, 'b': -2, 'c': -3, 'x': 100})
def f(*args):
print(args)
f(1, 2, 3, 4, 5)
def f(x, *args):
print(x)
print(args)
f('x', 1, 2, 3,)
f('x')
def f(**kwargs):
print(kwargs)
f(a=1, b=2)
f(*args, **kwargs):
def f(*args, **kwargs):
kwargs['x'] = 50
f1(*args, **kwargs)
def f(x, *args, key='key', **kwargs):
kwargs['x'] = 50
f1(*args, **kwargs)
get_ipython().run_line_magic('pinfo', 'str.format')
s = '{}'
s
s.format(3.14)
sum(range(10))
def value_at_zero(f):
return f(0)
value_at_zero(abs)
import math
value_at_zero(math.cos)
def f(x):
return x**2 + 1
value_at_zero(f)
value_at_zero(lambda x: x**2 + 1)
from scipy.optimize import root
get_ipython().run_line_magic('pinfo', 'root')
root(lambda: 2*x + 1, 0.)
root(lambda x: 2*x + 1, 0.)
root(lambda x: 2*x + 1, 0.)
root(math.cos, 0.5)
root(lambda x: math.cos(2*x), 0.5)
root(lambda x: math.pow(3.14, x) - 5, 0.)
from functools import partial