1. Introduction to Python: Exercise Solutions#
import numpy as np
Lists and loops#
Create the following lists, possibly in multiple ways.
odd positive integers below 20.
N = 20
j = 0
y = []
x = [x + 1 for x in range(N)]
for i in range (N):
if x[i]%2!=0:
y.append(x[i])
print (y)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
odd = []
for i in range(20):
if i % 2 == 1:
odd = odd + [i]
print(odd)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
A = []
for i in range(0,10):
A = A + [2*i+1]
print(A)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
odd = [i for i in range (1,21,2)]
print(odd)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
print(list(range(1,21,2)))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
sums from 1 to n, for up to n=10.
n = 10
a = [a+1 for a in range (n)]
for i in range (1,n):
a[i] = a[i-1] + a[i]
print(a)
[1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
sums = []
sum_all = 0
for i in range(1, 11):
sum_all += i
sums.append(sum_all)
print(sums)
[1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
k=[sum(list(range(1,i))) for i in range(2,12)]
k
[1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
prime numbers below n=20.
n = 20
A = []
for i in range(2,n):
count = 0
for j in range(2,i):
if(i%j==0):
count = 1
if(count==0):
A.append(i)
print(A)
[2, 3, 5, 7, 11, 13, 17, 19]
n = 20
y = []
for i in range(2,n):
for j in range (2,i):
if i%j == 0:
break
else: # if the for loop doesn't break
y.append(i)
print(y)
[2, 3, 5, 7, 11, 13, 17, 19]
n = 20
list_d = list()
for i in range(2,n):
if all(i%j!=0 for j in range(2,i)):
list_d.append(i)
print(list_d)
[2, 3, 5, 7, 11, 13, 17, 19]
import sympy
list(sympy.primerange(0, 20))
[2, 3, 5, 7, 11, 13, 17, 19]
n=10 random numbers between 0 and k=5.
(use np.random.randint())
n = 10
k = 5
r = []
for i in range(n):
r.append(np.random.randint(k))
r
[1, 4, 4, 0, 4, 1, 4, 1, 4, 1]
n = 10
k = 5
[ np.random.randint(k) for i in range(n)]
[0, 4, 2, 2, 3, 1, 1, 0, 0, 4]
np.random.randint(0, 5, 10)
array([4, 1, 4, 0, 4, 2, 4, 4, 1, 1])
np.random.randint(5, size=10)
array([2, 4, 2, 3, 4, 1, 2, 4, 2, 1])
from two lists of the same length, make a list with the larger of the items at the same position.
n = 10
min = 0
max = 5
x = np.random.randint(min, max, n)
y = np.random.randint(min, max, n)
print(x, y)
z = np.zeros(n)
for i in range(n):
if x[i] > y[i]:
z[i] = x[i]
else:
z[i] = y[i]
print(z)
[0 1 0 1 3 0 3 3 2 2] [0 3 1 0 3 0 0 3 1 2]
[0. 3. 1. 1. 3. 0. 3. 3. 2. 2.]
n = 10
x = np.arange(n) # 0 to n-1
y = np.arange(n,0,-1) # n to 1
z = [ np.max((x[i], y[i])) for i in range(n)]
np.array(z)
array([10, 9, 8, 7, 6, 5, 6, 7, 8, 9])
np.max([x,y], axis=0)
array([10, 9, 8, 7, 6, 5, 6, 7, 8, 9])
from a random list with 7 items, find the median
n = 7
a = np.random.randint(0, 10, n)
print(a)
sorted_a = sorted(a)
print(sorted_a)
i = int((len(a))/2)
sorted_a[i]
[7 1 4 7 9 7 6]
[1, 4, 6, 7, 7, 7, 9]
7
a = np.random.randint(0, 10, n)
print(a)
a.sort()
print(a)
a[int((len(a))/2)]
[2 1 3 9 5 2 8]
[1 2 2 3 5 8 9]
3
np.median(a)
3.0
Arrays and matrices#
an m-by-n matrix with random integers from 0 to k.
# for example
m = 3
n = 4
k = 10
matrix = np.zeros((m,n))
for i in range(m):
for j in range(n):
matrix[i,j] = np.random.randint(k)
print(matrix)
[[9. 6. 6. 2.]
[3. 5. 7. 4.]
[3. 8. 0. 9.]]
m, n, k = 3, 4, 10
A = [[np.random.randint(0,k) for j in range(n)] for i in range(m)]
np.array(A)
array([[0, 8, 4, 4],
[4, 2, 1, 5],
[8, 9, 1, 6]])
m = 3
n = 4
k = 10
np.random.randint(0, k, (m,n))
array([[1, 8, 6, 4],
[5, 7, 0, 7],
[3, 5, 6, 3]])
from a matrix, make a sub matrix of items in odd rows and even columns.
m = 3
n = 4
k = 10
A = np.random.randint(0, k, (m,n))
print(A)
B = []
for i in range(0, m, 2):
b = []
for j in range(1, n, 2):
b.append(A[i,j])
B.append(b)
print(np.array(B))
[[6 9 7 2]
[9 8 4 7]
[3 5 8 0]]
[[9 2]
[5 0]]
m, n, k = 3, 4, 10
A = np.random.randint(0, k, (m,n))
print(A)
B = A[::2, 1::2]
print(B)
[[0 9 8 3]
[0 4 4 3]
[3 2 9 8]]
[[9 3]
[2 8]]
make a m-by-n matrix with (i,j) component as i/j.
m = 3
n = 4
A = np.zeros((m,n))
for i in range(m):
for j in range(n):
A[i,j] = (i+1)/(j+1)
print(A)
[[1. 0.5 0.33333333 0.25 ]
[2. 1. 0.66666667 0.5 ]
[3. 1.5 1. 0.75 ]]
A = [[(i+1)/(j+1) for j in range(n)] for i in range(m)]
np.array(A)
array([[1. , 0.5 , 0.33333333, 0.25 ],
[2. , 1. , 0.66666667, 0.5 ],
[3. , 1.5 , 1. , 0.75 ]])
for a n-by-n matrix \(A\), compute the k-th power \(A^k\).
A = np.array([[2,0],[0,3]])
print(A)
k = 3
B = A
if k>1:
for i in range(k-1):
B = B @ A
print(B)
[[2 0]
[0 3]]
[[ 8 0]
[ 0 27]]
n = 3
A = np.diag(np.random.randn(n))
print(A)
k = 2
B = np.eye(len(A))
for i in range(k):
B = B @ A
print(B)
[[-0.0775331 0. 0. ]
[ 0. -0.71579747 0. ]
[ 0. 0. 1.49434596]]
[[0.00601138 0. 0. ]
[0. 0.51236602 0. ]
[0. 0. 2.23306985]]