1/22/22, 3:30 PM GR5260_Class1 – Jupyter Notebook
x, y = 1.3, 20 x*y
(float, int, float)
1.6900000000000002
Copyright By PowCoder代写 加微信 powcoder
‘my name is kelly’
—————————————————————————
TypeError Traceback (most recent call last)
/var/folders/p2/sh79rjm97l5bmdth32jd_h7c0000gn/T/ipykernel_34463/1393991151.py in <
----> 1 z + y
TypeError: can only concatenate str (not “int”) to str
type(x), type(y), type(x*y)
x *= 2 # in-place arithmetic x
z = ‘my name is ‘ w = ‘kelly’
# Data display
print(‘value of v is: ‘, v, ‘ Good’)
value of v is: 0.13 Good
# string formatting
s = ‘value of v is: {0:.1f}’.format(v) print(s)
value of v is: 0.1
s1 = ‘two values: {0:.1f} vs {1:10.2f}’.format(v, v**2) print(s1)
two values: 0.1 vs 0.02
localhost:8888/notebooks/2022_GR5260/GR5260_Class1.ipynb
1/22/22, 3:30 PM GR5260_Class1 – Jupyter Notebook
# string interpolation
s2 = f’two values: {v:.1f} vs {v**2:10.2f}’ print(s2)
two values: 0.1 vs 0.02
# conditional execution # if, else, elif
#if v == 0.13:
tol = 1.0e-8
if abs(v – 0.13) < tol: print('good')
else: print('something')
# iterations
for i in range(0,5): if i == 1:
continue # skip if i > 3:
(6, 21.0, ‘aaaaaaaaaa’)
error: Unsupported operand types for + (“str” and “int”)
multiply(3,2), multiply(2.1), multiply(‘a’)
error: Argument 1 to “multiply” has incompatible type “str”; expected “float”
Found 2 errors in 1 file (checked 1 source file)
if v: print(v)
%load_ext mypy_ipython
# function definition
# type checking
# default value
def multiply(x: float, y: float=10) -> float:
return x*y
multiply(3,2), multiply(2.1), multiply(‘a’)
def func(x: float, y: float=10) -> float: f = x*3 + x*y – y**2
# type checks
Type checking failed
localhost:8888/notebooks/2022_GR5260/GR5260_Class1.ipynb
1/22/22, 3:30 PM GR5260_Class1 – Jupyter Notebook
# take home exercise: define a function to return the bond price for a given yield
localhost:8888/notebooks/2022_GR5260/GR5260_Class1.ipynb
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com