1. Introduction to Python: Exercise Solutions

1. Introduction to Python: Exercise Solutions#

import numpy as np

Lists and loops#

Create the following lists, possibly in multiple ways.

  1. 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]
  1. 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]
  1. 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]
  1. 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
[2, 1, 3, 3, 2, 2, 0, 4, 0, 4]
n = 10
k = 5
[ np.random.randint(k) for i in range(n)]
[2, 3, 2, 2, 1, 2, 4, 0, 3, 4]
np.random.randint(0, 5, 10)
array([4, 0, 0, 4, 0, 2, 2, 4, 1, 0])
np.random.randint(5, size=10)
array([3, 0, 0, 2, 1, 2, 0, 0, 2, 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 3 0 0 2 4 1 0 3 2] [4 4 1 4 4 4 3 0 3 2]
[4. 4. 1. 4. 4. 4. 3. 0. 3. 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])
  1. 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]
[6 4 1 7 0 0 9]
[0, 0, 1, 4, 6, 7, 9]
4
a = np.random.randint(0, 10, n)
print(a)
a.sort()
print(a)
a[int((len(a))/2)]
[6 9 5 4 6 1 0]
[0 1 4 5 6 6 9]
5
np.median(a)
5.0

Arrays and matrices#

  1. 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)
[[7. 1. 8. 5.]
 [1. 6. 8. 7.]
 [2. 7. 9. 6.]]
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([[4, 4, 9, 2],
       [8, 9, 8, 4],
       [0, 4, 3, 6]])
m = 3
n = 4
k = 10
np.random.randint(0, k, (m,n))
array([[9, 2, 1, 0],
       [9, 9, 6, 8],
       [8, 0, 4, 3]])
  1. 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))
[[5 8 2 9]
 [5 6 1 8]
 [4 3 5 6]]
[[8 9]
 [3 6]]
m, n, k = 3, 4, 10
A = np.random.randint(0, k, (m,n))
print(A)
B = A[::2, 1::2]
print(B)
[[7 2 9 2]
 [4 6 9 2]
 [4 9 5 5]]
[[2 2]
 [9 5]]
  1. 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      ]])
  1. 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.29365523  0.          0.        ]
 [ 0.          0.56277411  0.        ]
 [ 0.          0.          0.24297028]]
[[0.0862334  0.         0.        ]
 [0.         0.31671469 0.        ]
 [0.         0.         0.05903456]]