def txt_to_ls(filename: str) -> list[str]:
"""
Split a plain text file given by filename into lines, and return a list of
those lines. The newline/line break (\n) *is* included in the returned
strings.
"""
with open(filename) as fh:
return list(fh)
assert txt_to_ls("a-tale-of-two-cities.txt")[10] == "Title: A Tale of Two Cities\n", "Title line"
assert txt_to_ls("a-tale-of-two-cities.txt")[106] == "It was the best of times, it was the worst of times, it was the age of\n", "Famous first line"
def ls_to_txt(filename: str, lines: list[str], add_newline=True) -> list[str]:
"""
Write a list of strings, given by lines, to a text file, given by filename.
If add_newline is True, the default, then a newline is added after each
string. Returns the list of strings written, with the newlines if
applicable.
"""
def concat_newline(s: str) -> str:
"""
Return s concatenated to a newline.
"""
return s + "\n"
if add_newline:
lines = list(map(concat_newline, lines))
with open(filename, "w") as fh:
fh.writelines(lines)
return lines
assert ls_to_txt("tmp.txt", ["Hello"])[0] == "Hello\n", "Newline added by default"
assert ls_to_txt("tmp.txt", ["Hello"], add_newline=False)[0] == "Hello", "Newline not added with add_newline=False"
lines = txt_to_ls("a-tale-of-two-cities.txt")
lines.sort(key=len)
ls_to_txt("out.txt", lines, add_newline=False)
print("Hi")
Hi
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]))
print(sum([2, 4, 6, 0, 1]))
0.8414709848078965 0.8414709848078965 42 42 13 13
print([1, 2, "3", 4, (5, 6)])
[1, 2, '3', 4, (5, 6)]
arr = np.array([2, 4, 6, 0, 1])
print(arr)
arr = np.array([1, 2, 3.14159])
print(arr)
arr = np.array([3.14159, "3.14159"])
print(arr)
[2 4 6 0 1] [1. 2. 3.14159] ['3.14159' '3.14159']
arr = np.array([1, 2, 3.14159])
print(arr)
print(arr.dtype)
arr = np.array([1, 2, 3])
print(arr.dtype)
[1. 2. 3.14159] float64 int64
arr = np.array(["one", "two", "three"])
arr[0] = "seventy"
print(arr.dtype)
print(arr)
<U5 ['seven' 'two' 'three']
x = np.array([1.1, 2.2, 3.3])
y = np.array([2.0, 4.0, -1.0])
print(x * 2)
print(x - 1)
print(x * y)
print(x < 3)
lst = [1, 2, 3]
print(lst * 2)
print(lst)
[2.2 4.4 6.6] [0.1 1.2 2.3] [ 2.2 8.8 -3.3] [ True True False] [1, 2, 3, 1, 2, 3] [1, 2, 3]
x = np.array([1.1, 2.2, 3.3])
q = x
x *= 2
print(x)
print(q)
x -= 1
print(x)
x *= y
print(x)
for idx in range(len(x)):
x[idx] = x[idx]*2
print(x)
a = 42
a *= 3
print(a)
[2.2 4.4 6.6] [2.2 4.4 6.6] [1.2 3.4 5.6] [ 2.4 13.6 -5.6] [ 4.8 27.2 -11.2] 126
x = np.array([1, 2, 3])
y = x / 2
print(y)
x /= 2
print(x)
[0.5 1. 1.5]
--------------------------------------------------------------------------- UFuncTypeError Traceback (most recent call last) Input In [45], in <cell line: 4>() 2 y = x / 2 3 print(y) ----> 4 x /= 2 5 print(x) UFuncTypeError: Cannot cast ufunc 'divide' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'
print(np.array([3.14159], dtype=np.float32))
[3.14159]
y = np.array([2.0, 4.0, -1.0])
print(np.sin(y))
print(math.sin(y))
[ 0.90929743 -0.7568025 -0.84147098]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [54], in <cell line: 3>() 1 y = np.array([2.0, 4.0, -1.0]) 2 print(np.sin(y)) ----> 3 print(math.sin(y)) TypeError: only size-1 arrays can be converted to Python scalars