[2, 3, 4, 1, 1, 2]
[3 4 6]
CSCI 1012
Hello world!
ex1.py
ex2.py
ex3.py
.zip
with all the .py
filesWrite a function list_sum
that takes two list arguments:
All elements of both lists will be numeric (int or float)
Return a new list that sums the argument lists, element-by-element
Assume both lists are of equal length, greater than 0
list_sum([0, 1], [2, 3])
returns list [2, 4]
list_sum([0, -1.0, 2.0], [1, 2, 3])
returns list [1, 1.0, 5.0]
Starter code:
Submit as ex1.py
A = [2, 3, 4, 5] # all ints
B = [0, 1, "A", False] # mix of int, str, bool
C = [{"B": "C"}, 2.1, [0, 1], ("okay",)] #dict, float, list, tuple
A.append("E")
print(A)
print([2, 3] + [1, 2])
[2, 3, 4, 5, 'E']
[2, 3, 1, 2]
append
and concatenationmath
3
math
gives us functions
math.sqrt
, math.ceil
, math.floor
, etc.math
also gives us a few constant values
math.pi
, math.e
(there are others)[2 3 4]
numpy
included in visualizernumpy
(“numeric Python”) provides new objects
math.floor(2.1)
floor
function associated with math
.
in a float is just a decimal'Thank you.'.upper()
upper
function associated with string 'Thank you.'
.
in a string is just a periodas
Contrast:
vs.
as
keyword used in importsnumpy
as np
literally just saves us some typingimport numpy as np
# list concatenation
Q = [2, 3, 4]
R = [1, 1, 2]
print("Lists:", Q + R)
# numpy addition
X = np.array([2, 3, 4])
Y = np.array([1, 1, 2])
print("Numpy arrays:", X + Y)
[2, 3, 4, 1, 1, 2]
[3 4 6]
import numpy as np
# lists
Q = [0, 2, 3, 4]
print("Lists:", Q * 5)
# numpy
X = np.array([0, 2, 3, 4])
print("Numpy arrays:", X * 5)
Lists: [0, 2, 3, 4, 0, 2, 3, 4, 0, 2, 3, 4, 0, 2, 3, 4, 0, 2, 3, 4]
Numpy arrays: [ 0 10 15 20]
3
[0 2 1 4]
import numpy as np
# lists
Q = [0, 2, 3, 4]
R = Q.append(5)
print("Q:", Q)
print("R:", R)
# numpy
X = np.array([0, 2, 3, 4])
Y = np.append(X, 5)
print("Array X:", X) # X is not changed!
print("Array Y:", Y)
Q: [0, 2, 3, 4, 5]
R: None
Array X: [0 2 3 4]
Array Y: [0 2 3 4 5]
import numpy as np
Z = np.array([3, 1, 2, 1])
print("Initial Z:", Z)
Z.resize(6, refcheck=False) # in place
print("Resized Z:", Z)
W = Z.reshape(2, 3) # returns new array
print("Final Z:",Z)
print("W:\n", W)
Initial Z: [3 1 2 1]
Resized Z: [3 1 2 1 0 0]
Final Z: [3 1 2 1 0 0]
W:
[[3 1 2]
[1 0 0]]
Z = np.zeros(6)
Y = np.ones(5)
print("Z:", Z)
print("Y:", Y)
X = np.append(Z, Y) # different from list append!
print("X:", X)
Z: [0. 0. 0. 0. 0. 0.]
Y: [1. 1. 1. 1. 1.]
X: [0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
Write a function array_sum
that takes two (1D, numeric) numpy array arguments:
Starter code:
array_sum(np.array([0, 1, 2]), np.array([2, 3]))
returns array [2 4 2]
array_sum(np.array([1, 2]), np.array([2, 3]))
returns array [3 4]
array_sum(np.array([1, 1]), np.array([1, 5, 3]))
returns array [2 6 3]
import numpy as np
A1 = np.array([3, 1, 4, 1])
A2 = np.array([1, 1, 2, 4])
print("+ ", A1 + A2)
print("- ", A1 - A2)
print("/ ", A1 / A2)
print("* ", A1 * A2)
print("== ", A1 == A2)
print("> ", A1 > A2)
print("< ", A1 < A2)
+ [4 2 6 5]
- [ 2 0 2 -3]
/ [3. 1. 2. 0.25]
* [3 1 8 4]
== [False True False False]
> [ True False True False]
< [False False False True]
import numpy as np
A1 = np.array([4, 2, 8, 1])
print("+ ", A1 + 3)
print("- ", A1 - 2)
print("/ ", A1 / 2)
print("* ", A1 * 5)
print("== ", A1 == 2)
print("> ", A1 > 2)
print("< ", A1 < 3)
+ [ 7 5 11 4]
- [ 2 0 6 -1]
/ [2. 1. 4. 0.5]
* [20 10 40 5]
== [False True False False]
> [ True False True False]
< [False True False True]
This is a Python thing and is not true everywhere
2
True
is 1False
is 02
Why do we care?
import numpy as np
np.sum([2, 3, 1, 7, 1])
np.mean([2, 3, 1, 7, 1])
np.median([2, 3, 1, 7, 1])
np.std([2, 3, 1, 7, 1])
data = [1, 2, 8, 3, 4, 1, 0, 4, 5]
standardized_data = (data - np.std(data))/np.mean(data)
print(standardized_data)
[-0.90578946 -0.47673129 2.0976177 -0.04767313 0.38138504 -0.90578946
-1.33484762 0.38138504 0.8104432 ]
Write a function above_mean
that takes one argument: a list of numbers (ints or floats):
Starter code:
above_mean([2, 3, 1, 8, 0, -1, 2, 3, 5])
returns int 4
above_mean([2, 3, 1, 80, 0, -1, 2, 3, 5])
returns int 1
above_mean([2, 3, 1, -80, 0, -1, 2, 3, 5])
returns int 8
Submit as ex3.py
.
numpy
is actually a linear algebra library
@
operator for matrix multiplicationimport numpy as np
A1 = np.array([[1, 2], [3, 4]])
A2 = np.array([[2, 1], [4, 4]])
print("+\n", A1 + A2)
print("@\n", A1 @ A2)
+
[[3 3]
[7 8]]
@
[[10 9]
[22 19]]
import numpy as np
V1 = np.array([[1, 2, 3, 4]])
V2 = np.array([[2, 1, 4, 4]])
print("+\n", V1 + V2)
print("\n1x4 @ 4x1\n", V1 @ V2.T)
print("\n4x1 @ 1x4\n", V1.T @ V2)
+
[[3 3 7 8]]
1x4 @ 4x1
[[32]]
4x1 @ 1x4
[[ 2 1 4 4]
[ 4 2 8 8]
[ 6 3 12 12]
[ 8 4 16 16]]
import numpy as np
V1 = np.array([[1, 2, 3, 4]])
print("V1: ", V1, " V1 Shape:", V1.shape)
V2 = np.array([2, 1, 4, 1])
print("V2: ", V2, " V2 Shape:", V2.shape)
V3 = V1.T
print("V3:\n", V3, "\nV3 Shape:", V3.shape)
V4 = V2.T
print("V4: ", V4, " V4 Shape:", V4.shape)
V1: [[1 2 3 4]] V1 Shape: (1, 4)
V2: [2 1 4 1] V2 Shape: (4,)
V3:
[[1]
[2]
[3]
[4]]
V3 Shape: (4, 1)
V4: [2 1 4 1] V4 Shape: (4,)
No! 😌
np.array
initialization and assignment+ - * / == > < >= <=
5 May “Extra” Lecture:
[
and ]
(
and )
{
and }
w = "confounded!" # string
x = [2, 3, 4] # list
y = (3, 4, 5) # tuple
z = {"A": 1, 2: 3} # dict
print(w[2], x[0], y[1], z["A"])
n 2 4 1
355
…parentheses for tuple definitions are often optional
(1, 2, 3)
<class 'tuple'>
With parentheses:
(1, 2, 3)
<class 'tuple'>
x[2]
or y["A"]
f(4, 5)
or print("hello", "world")
z = {1: 2}
Function definitions always have the def
keyword
Parentheses not after a name and with a comma indicate a tuple:
(2, 3)
or ("yes",)
Parentheses not after a name, and with no comma, indicate an expression:
(5)
or ("otis")