Computing with Formulas¶

da PowerShell -- jupyter nbconvert --to html notebook.ipynb¶

Tasso di interesse - calcoli¶

$$\large A=P\left( 1+\left(\frac{r}{100}\right) \right)^n $$
In [4]:
P = 100; r = 5.0; n = 7
print(100*(1 + 5.0/100)**7)
140.71004226562505

Variables and Variable Types¶

In [7]:
primary = 100
r = 5.0
n = 7
amount = primary * (1+r/100)**n 
print(amount)
140.71004226562505

a variable in a Python program can be any word containing the letters a–z, A–Z, underscore _ and the digits 0-9, but it cannot start with a digit. Variable names in Python are also case-sensitive, for instance, $a$ is different from $A$.¶

In [10]:
a = 5
print(a == 5)
True

Comments are useful for explaining¶

In [13]:
# program for computing the growth of 
# money deposited in a bank
primary = 100 # initial amount
r = 5.0 # interest rate in %
n = 7 # the number of years
amount = primary * (1+r/100)**n 
print(amount)
140.71004226562505

All variables have types¶

In [18]:
primary = 100.01 # initial amount
r = 5.0 # interest rate in %
n = 7 # the number of years
hello = "Hello, World!" 

print(type(hello)) 
print(type(r)) 
print(type(primary)) 
print(type(n))
<class 'str'>
<class 'float'>
<class 'float'>
<class 'int'>
In [20]:
x1 = 2
x2 = "2" 
print(x1+x1) 
print(x2+x2)
4
22

Formatting Text Output¶

In [23]:
primary = 100 # initial amount
r = 5.0 # interest rate in %
n = 7 # the number of years
amount = primary * (1+r/100)**n
print(f"After {n} years, 100 EUR has grown to {amount} EUR.")
After 7 years, 100 EUR has grown to 140.71004226562505 EUR.
In [25]:
t = 1.234567
print(f"Default output gives t = {t}.")
print(f"We can set the precision: t = {t:.2}.")
print(f"Or control the number of decimals: t = {t:.2f}.")
Default output gives t = 1.234567.
We can set the precision: t = 1.2.
Or control the number of decimals: t = 1.23.
In [27]:
print(f"We may set the space used for the output: t = {t:8.2f}.")
We may set the space used for the output: t =     1.23.
In [29]:
r = 87
print(f"Integer set to occupy exactly 8 chars of space: r = {r:8d}")
Integer set to occupy exactly 8 chars of space: r =       87
In [31]:
a = 786345687.12
b = 1.2345
print(f"Without the format specifier: a = {a}, b = {b}.") 
print(f"With the format specifier: a = {a:g}, b = {b:g}.")
Without the format specifier: a = 786345687.12, b = 1.2345.
With the format specifier: a = 7.86346e+08, b = 1.2345.

Importing Modules¶

In [34]:
import math
r = math.sqrt(2)
# or
from math import sqrt
r = sqrt(2)
# or
from math import * # import everything in math 
r = sqrt(2)

Esempio con la Gaussiana¶

$$\large f(x) = \frac{1}{\sqrt(2*\pi)*s} * exp \left(-0.5*\left( \frac{(x-m)}{s} \right)^2 \right) $$
In [40]:
from math import sqrt, pi, exp
m = 0
s = 2
x = 1.0
f = 1/(sqrt(2*pi)*s) * exp(-0.5*((x-m)/s)**2) 
print(f)
0.17603266338214976

Finding information about Python modules¶

In [43]:
import math 
#print(dir(math))
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
In [47]:
import math
#help(math.pow)
Help on built-in function pow in module math:

pow(x, y, /)
    Return x**y (x to the power of y).

Insidie ​​nella programmazione matematica¶

In [54]:
# problemi di arrorondamento - errore approssimativamente 10^-16
v1 = 1/49.0*49 
v2 = 1/51.0*51 
print(f"{v1:.16f} {v2:.16f}")
print(v1 == 1) 
print(v2 == 1)

tol = 1e-14 
print(abs(v1-1) < tol) 
print(abs(v2-1) < tol)
0.9999999999999999 1.0000000000000000
False
True
True
True

In Python 2 la divisione per un intero può essere un problema¶

In [ ]: