Data types, loops, matplotlib, NumPy

Imports

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np
# !pip install matplotlib

Data types

Integer

a = 1
print(a)
type(a)
1
int
type(4)
int

String

a = 'syntax error'
print(a)
type(a)
syntax error
str
print("hello world!")
print('''hello

world!''') 
print('syntax error')
hello world!
hello

world!
syntax error

Float

a = 0.5
print(a)
type(a)
0.5
float

Type conversion / casting

a = str(10)
print(a)
type(a)
10
str
a = int('4')
print(a)
type(a)
4
int
a = float('5.2')
print(a)
type(a)
5.2
float

List

seq = [1, 1, 0, 1, 0, 0, 1, 1]
print(seq)
type(seq)
[1, 1, 0, 1, 0, 0, 1, 1]
list
seq.append(0)
print(seq)
[1, 1, 0, 1, 0, 0, 1, 1, 0]
len(seq)
9
len('syntax error')
12

Loops

seq[0]
1
seq[-1]
0
seq[:3]
[1, 1, 0]
seq[-4:]
[0, 1, 1, 0]
for i in range(3):
    print(seq[i])
1
1
0
for i in range(len(seq)):
    print(seq[i])
1
1
0
1
0
0
1
1
0
for value in seq:
    print(value)
1
1
0
1
0
0
1
1
0
for number in [1, 0.5, 245]:
    print(number)
1
0.5
245
for index, value in enumerate([1, 0.5, 245]):
    print(index, value)
0 1
1 0.5
2 245
for index, value in enumerate([1, 0.5, 245]):
    print(str(index) + ' ... ' + str(value))
0 ... 1
1 ... 0.5
2 ... 245
a = 5
b = "Let's say " + str(a)
print(b)
Let's say 5
a = 5
b = f"Let's say {a}"
print(b)
Let's say 5
for index, value in enumerate([1, 0.5, 245]):
    print(f"{index} ... {value}")
0 ... 1
1 ... 0.5
2 ... 245

Matplotlib

seq
[1, 1, 0, 1, 0, 0, 1, 1, 0]
print([seq])
[[1, 1, 0, 1, 0, 0, 1, 1, 0]]
plt.imshow([seq])
<matplotlib.image.AxesImage at 0x12d54a310>
_images/types_loops_plt_np_39_1.png
cmap = ListedColormap(['white', 'black'])
plt.xticks(ticks=[])
plt.yticks([])
plt.imshow([seq], cmap=cmap)
<matplotlib.image.AxesImage at 0x12a86a590>
_images/types_loops_plt_np_40_1.png

Functions

def plot_data(data):
    cmap = ListedColormap(['white', 'black'])
    plt.xticks(ticks=[])
    plt.yticks([])
    plt.imshow(data, cmap=cmap)
plot_data([seq])
_images/types_loops_plt_np_43_0.png
plot_data([[0, 1, 1], [1, 0, 0]])
_images/types_loops_plt_np_44_0.png

NumPy

For an official introduction see: https://numpy.org/doc/stable/user/absolute_beginners.html

print(seq)
type(seq)
[1, 1, 0, 1, 0, 0, 1, 1, 0]
list
arr = np.array(seq)
print(arr)
type(arr)
[1 1 0 1 0 0 1 1 0]
numpy.ndarray

Data type: tuple

a = (3, 3)
print(a)
type(a)
(3, 3)
tuple
arr.reshape((3, 3))
array([[1, 1, 0],
       [1, 0, 0],
       [1, 1, 0]])
arr
array([1, 1, 0, 1, 0, 0, 1, 1, 0])
arr = arr.reshape((3, 3))
plot_data(arr)
_images/types_loops_plt_np_52_0.png
a = np.ones(8, dtype=int)
print(a)
[1 1 1 1 1 1 1 1]
b = np.zeros(4).astype(int)
print(b)
[0 0 0 0]
seq = np.concatenate((a, b))
print(seq)
[1 1 1 1 1 1 1 1 0 0 0 0]
seq.shape
(12,)
plot_data([seq])
_images/types_loops_plt_np_57_0.png
np.random.shuffle(seq)
plot_data([seq])
_images/types_loops_plt_np_58_0.png
seq = seq.reshape(12, 1)
plot_data(seq)
_images/types_loops_plt_np_59_0.png
seq = seq.reshape(6, 2)
plot_data(seq)
_images/types_loops_plt_np_60_0.png
seq = seq.reshape(2, -1)
plot_data(seq)
_images/types_loops_plt_np_61_0.png
seq = seq.reshape(7, -1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[71], line 1
----> 1 seq = seq.reshape(7, -1)

ValueError: cannot reshape array of size 12 into shape (7,newaxis)
plot_data(seq.reshape(4, -1))
_images/types_loops_plt_np_63_0.png
plot_data(seq.reshape(3, 4))
_images/types_loops_plt_np_64_0.png
seq
array([[1, 1, 1, 0, 0, 0],
       [1, 0, 1, 1, 1, 1]])
seq = seq.flatten()
print(seq)
[1 1 1 0 0 0 1 0 1 1 1 1]
sum(seq)
8
sum([1, 2, 3])
6