1. Introduction to Python: Exercise

1. Introduction to Python: Exercise#

Name:

import numpy as np

Lists and loops#

Create the following lists, possibly in multiple ways.

  1. odd positive integers below 20.

  1. sums from 1 to n, for up to n=10.

  1. prime numbers below n=20.

  1. n=10 random numbers between 0 and k=5.
    (use np.random.randint())

np.random.randint?
  1. from two lists of the same length, make a list with the larger of the items at the same position.

  1. from a random list with 7 items, find the median

np.sort?

Arrays and matrices#

  1. an m-by-n matrix with random integers from 0 to k.

# for example
m=3
n=4
  1. from a matrix, make a sub matrix of items in odd rows and even columns.

  1. make a m-by-n matrix with (i,j) component as i/j.

  1. for a n-by-n matrix \(A\), compute the k-th power \(A^k\).