In [2]:
# Method 1:
lines = []
with open("in.txt") as ifh:
    for line in ifh:
        lines.append(line)
lines.sort(key=len)
with open("out.txt", "w") as ofh:
    for line in lines:
        ofh.write(line)
        
# Method 2:
with open("in.txt") as ifh:
    with open("out.txt", "w") as ofh:
        for line in sorted(ifh, key=len):
            ofh.write(line)
In [7]:
import numpy as np
import math

print(np.sin(1))
print(math.sin(1))
print(np.abs(-42))
print(abs(-42))
print(np.sum([2, 4, 6, 0, 1]))
0.8414709848078965
0.8414709848078965
42
42
13
In [10]:
print(np.array([2, 4, 6, 0, 2]))
print(np.array([1, 2, 3.14159]))
lst = []
with open("in.txt") as ifh:
    for line in ifh:
        lst.append(len(line))
print(np.array(lst))
[2 4 6 0 2]
[1.      2.      3.14159]
[53  5 70 ... 66 67 59]
In [14]:
x = np.array([2, 4, 6, 0, 1])
print(x.dtype)
y = np.array([1, 2, 3.14159])
print(y.dtype)
a: float = y[1]
math.sin(a)
int64
float64
Out[14]:
0.9092974268256817
In [17]:
x = np.array(["one", "two", "three"])
print(x.dtype)
x[0] = "seventy"
print(x)
<U5
['seven' 'two' 'three']
In [22]:
lst = [1.1, 2.2, 3.3]
x = np.array(lst)
y = np.array([2.0, 4.0, -1.0])
print(lst * 2)
print(x*2)
print(x-1)
print(x<3)
print(x*y)
[1.1, 2.2, 3.3, 1.1, 2.2, 3.3]
[2.2 4.4 6.6]
[0.1 1.2 2.3]
[ True  True False]
[ 2.2  8.8 -3.3]
In [25]:
x = np.array([1.1, 2.2, 3.3])
y = x
x *= 2
x += 12
print(y)
[14.2 16.4 18.6]
In [27]:
n = 42.0
n /= 7.0
print(n)
l = [1, 2, 3]
l *= 2
print(l)
6.0
[1, 2, 3, 1, 2, 3]
In [29]:
x = np.array([1.1, 2.2, 3.3])
print(np.sin(x))
[ 0.89120736  0.8084964  -0.15774569]
In [31]:
y = np.array([2.0, 4.0, -1.0])
print(np.all(y > 0))
# assert np.all(y > 0), "..."

assert np.all(
    np.abs(makeAnArray(...) - np.array([1.1, 2.2, 3.3])) < 0.001
), "..."
False
In [ ]:
import numpy.typing as npt

def f(arr: npt.NDArray[np.float64]) -> None:
    ...
In [37]:
print(np.zeros(4))
print(np.ones(4))
print(np.zeros(4, dtype=int))
print(np.ones(4, dtype=bool))

x = np.zeros(4)
x[0] = 42
print(x)
[0. 0. 0. 0.]
[1. 1. 1. 1.]
[0 0 0 0]
[ True  True  True  True]
[42.  0.  0.  0.]
In [40]:
print(np.linspace(0, 4, 5))
print(np.linspace(1.1, 2.2, 3))
print(np.linspace(0, 4, 5, endpoint=False))
[0. 1. 2. 3. 4.]
[1.1  1.65 2.2 ]
[0.  0.8 1.6 2.4 3.2]