6/25/2020 Week 1
You are currently looking at version 1.1 of this notebook. To download notebooks and datafiles, as well as get
help on Jupyter notebooks in the Coursera platform, visit the Jupyter Notebook FAQ
(https://www.coursera.org/learn/python-data-analysis/resources/0dhYG) course resource.
The Python Programming Language: Functions
add_numbers is a function that takes two numbers and adds them together.
In [1]:
def add_numbers(x, y):
return x + y
add_numbers(1, 2)
Out[1]:
add_numbers updated to take an optional 3rd parameter. Using print allows printing of multiple expressions
within a single cell.
In [2]:
def add_numbers(x,y,z=None):
if (z==None):
return x+y
else:
return x+y+z
print(add_numbers(1, 2))
print(add_numbers(1, 2, 3))
3
6
add_numbers updated to take an optional flag parameter.
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 1/28
6/25/2020 Week 1
In [4]:
def add_numbers(x, y, z=None, flag=False):
if (flag):
print('Flag is true!')
if (z==None):
return x + y
else:
return x + y + z
print(add_numbers(1, 2,3, flag=True))
Flag is true!
6
Assign function add_numbers to variable a.
In [6]:
def add_numbers(x,y):
return x+y
a = add_numbers
a(1,2)
Out[6]:
The Python Programming Language: Types and
Sequences
Use type to return the object's type.
In [7]:
type('This is a string')
Out[7]:
str
In [8]:
type(None)
Out[8]:
NoneType
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 2/28
6/25/2020 Week 1
In [9]:
type(1)
Out[9]:
int
In [10]:
type(1.0)
Out[10]:
float
In [11]:
type(add_numbers)
Out[11]:
function
Tuples are an immutable data structure (cannot be altered).
In [1]:
x = (1, 'a', 2, 'b')
type(x)
Out[1]:
tuple
Lists are a mutable data structure.
In [3]:
x = [1, 'a', 2, 'b']
type(x)
Out[3]:
list
Use append to append an object to a list.
In [4]:
x.append(3.3)
print(x)
[1, 'a', 2, 'b', 3.3]
This is an example of how to loop through each item in the list.
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 3/28
6/25/2020 Week 1
In [15]:
for item in x:
print(item)
1
a
2
b
3.3
Or using the indexing operator:
In [5]:
i=0
while( i != len(x) ):
print(x[i])
i = i + 1
1
a
2
b
3.3
Use + to concatenate lists.
In [17]:
[1,2] + [3,4]
Out[17]:
[1, 2, 3, 4]
Use * to repeat lists.
In [18]:
[1]*3
Out[18]:
[1, 1, 1]
Use the in operator to check if something is inside a list.
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 4/28
6/25/2020 Week 1
In [19]:
1 in [1, 2, 3]
Out[19]:
True
Now let's look at strings. Use bracket notation to slice a string.
In [20]:
x = 'This is a string'
print(x[0]) #first character
print(x[0:1]) #first character, but we have explicitly set the end character
print(x[0:2]) #first two characters
T
T
Th
This will return the last element of the string.
In [21]:
x[-1]
Out[21]:
'g'
This will return the slice starting from the 4th element from the end and stopping before the 2nd element from
the end.
In [22]:
x[-4:-2]
Out[22]:
'ri'
This is a slice from the beginning of the string and stopping before the 3rd element.
In [23]:
x[:3]
Out[23]:
'Thi'
And this is a slice starting from the 4th element of the string and going all the way to the end.
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 5/28
6/25/2020 Week 1
g g g g y
In [24]:
x[3:]
Out[24]:
's is a string'
In [25]:
firstname = 'Christopher'
lastname = 'Brooks'
print(firstname + ' ' + lastname)
print(firstname*3)
print('Chris' in firstname)
Christopher Brooks
ChristopherChristopherChristopher
True
split returns a list of all the words in a string, or a list split on a specific character.
In [26]:
firstname = 'Christopher Arthur Hansen Brooks'.split(' ')[0] # [0] selects the first elemen
lastname = 'Christopher Arthur Hansen Brooks'.split(' ')[-1] # [-1] selects the last elemen
print(firstname)
print(lastname)
Christopher
Brooks
Make sure you convert objects to strings before concatenating.
In [27]:
'Chris' + 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-1623ac76de6e> in <module>()
----> 1 'Chris' + 2
TypeError: must be str, not int
In [28]:
'Chris' + str(2)
Out[28]:
'Chris2'
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 6/28
6/25/2020 Week 1
Dictionaries associate keys with values.
In [29]:
x['Christopher Brooks'] # Retrieve a value by using the indexing operator
Out[29]:
'[email protected]'
In [33]:
x['Kevyn Collins-Thompson'] = None
x['Kevyn Collins-Thompson']
Iterate over all of the keys:
In [32]:
for name in x:
print(x[name])
[email protected]
[email protected]
None
Iterate over all of the values:
In [34]:
for email in x.values():
print(email)
[email protected]
[email protected]
None
Iterate over all of the items in the list:
In [35]:
for name, email in x.items():
print(name)
print(email)
Christopher Brooks
[email protected]
Bill Gates
[email protected]
Kevyn Collins-Thompson
None
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 7/28
6/25/2020 Week 1
You can unpack a sequence into different variables:
In [36]:
fname, lname, email = x
In [37]:
fname
Out[37]:
'Christopher'
In [38]:
lname
Out[38]:
'Brooks'
Make sure the number of values you are unpacking matches the number of variables being assigned.
In [39]:
x = ('Christopher', 'Brooks', '[email protected]', 'Ann Arbor')
fname, lname, email = x
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-39-9ce70064f53e> in <module>()
1 x = ('Christopher', 'Brooks', '[email protected]', 'Ann Arbor')
----> 2 fname, lname, email = x
ValueError: too many values to unpack (expected 3)
The Python Programming Language: More on
Strings
In [40]:
print('Chris' + 2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-40-82ccfdd3d5d3> in <module>()
----> 1 print('Chris' + 2)
TypeError: must be str, not int
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 8/28
6/25/2020 Week 1
In [41]:
print('Chris' + str(2))
Chris2
Python has a built in method for convenient string formatting.
In [43]:
sales_record = {
'price': 3.24,
'num_items': 4,
'person': 'Chris'}
sales_statement = '{} bought {} item(s) at a price of {} each for a total of {}'
print(sales_statement.format(sales_record['person'],
sales_record['num_items'],
sales_record['price'],
sales_record['num_items']*sales_record['price']))
Chris bought 4 item(s) at a price of 3.24 each for a total of 12.96
Reading and Writing CSV files
Let's import our datafile mpg.csv, which contains fuel economy data for 234 cars.
mpg : miles per gallon
class : car classification
cty : city mpg
cyl : # of cylinders
displ : engine displacement in liters
drv : f = front-wheel drive, r = rear wheel drive, 4 = 4wd
fl : fuel (e = ethanol E85, d = diesel, r = regular, p = premium, c = CNG)
hwy : highway mpg
manufacturer : automobile manufacturer
model : model of car
trans : type of transmission
year : model year
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 9/28
6/25/2020 Week 1
In [7]:
import csv
%precision 2
with open('mpg.csv') as csvfile:
mpg = list(csv.DictReader(csvfile))
mpg[:3] # The first three dictionaries in our list.
Out[7]:
[OrderedDict([('', '1'),
('manufacturer', 'audi'),
('model', 'a4'),
('displ', '1.8'),
('year', '1999'),
('cyl', '4'),
('trans', 'auto(l5)'),
('drv', 'f'),
('cty', '18'),
('hwy', '29'),
('fl', 'p'),
('class', 'compact')]),
OrderedDict([('', '2'),
('manufacturer', 'audi'),
('model', 'a4'),
('displ', '1.8'),
('year', '1999'),
('cyl', '4'),
csv.Dictreader has read in each row of our csv file as a dictionary. len shows that our list is comprised of
234 dictionaries.
In [8]:
len(mpg)
Out[8]:
234
keys gives us the column names of our csv.
In [9]:
mpg[0].keys()
Out[9]:
odict_keys(['', 'manufacturer', 'model', 'displ', 'year', 'cyl', 'trans', 'd
rv', 'cty', 'hwy', 'fl', 'class'])
This is how to find the average cty fuel economy across all cars. All values in the dictionaries are strings, so we
need to convert to float.
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 10/28
6/25/2020 Week 1
In [11]:
sum(float(d['cty']) for d in mpg)
Out[11]:
3945.00
Similarly this is how to find the average hwy fuel economy across all cars.
In [ ]:
sum(float(d['hwy']) for d in mpg) / len(mpg)
Use set to return the unique values for the number of cylinders the cars in our dataset have.
In [13]:
cylinders = set(d['cyl'] for d in mpg)
cylinders
Out[13]:
{'4', '5', '6', '8'}
Here's a more complex example where we are grouping the cars by number of cylinder, and finding the average
cty mpg for each group.
In [2]:
CtyMpgByCyl = []
for c in cylinders: # iterate over all the cylinder levels
summpg = 0
cyltypecount = 0
for d in mpg: # iterate over all dictionaries
if d['cyl'] == c: # if the cylinder level type matches,
summpg += float(d['cty']) # add the cty mpg
cyltypecount += 1 # increment the count
CtyMpgByCyl.append((c, summpg / cyltypecount)) # append the tuple ('cylinder', 'avg mpg
CtyMpgByCyl.sort(key=lambda x: x[0])
CtyMpgByCyl
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-6a38e09e7d36> in <module>()
1 CtyMpgByCyl = []
2
----> 3 for c in cylinders: # iterate over all the cylinder levels
4 summpg = 0
5 cyltypecount = 0
NameError: name 'cylinders' is not defined
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 11/28
6/25/2020 Week 1
Use set to return the unique values for the class types in our dataset.
In [15]:
vehicleclass = set(d['class'] for d in mpg) # what are the class types
vehicleclass
Out[15]:
{'2seater', 'compact', 'midsize', 'minivan', 'pickup', 'subcompact', 'suv'}
And here's an example of how to find the average hwy mpg for each class of vehicle in our dataset.
In [3]:
HwyMpgByClass = []
for t in vehicleclass: # iterate over all the vehicle classes
summpg = 0
vclasscount = 0
for d in mpg: # iterate over all dictionaries
if d['class'] == t: # if the cylinder amount type matches,
summpg += float(d['hwy']) # add the hwy mpg
vclasscount += 1 # increment the count
HwyMpgByClass.append((t, summpg / vclasscount)) # append the tuple ('class', 'avg mpg')
HwyMpgByClass.sort(key=lambda x: x[1])
HwyMpgByClass
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-189fb3d201ff> in <module>()
1 HwyMpgByClass = []
2
----> 3 for t in vehicleclass: # iterate over all the vehicle classes
4 summpg = 0
5 vclasscount = 0
NameError: name 'vehicleclass' is not defined
The Python Programming Language: Dates and
Times
In [8]:
import datetime as dt
import time as tm
time returns the current time in seconds since the Epoch. (January 1st, 1970)
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 12/28
6/25/2020 Week 1
In [9]:
tm.time()
Out[9]:
1573149245.2140586
Convert the timestamp to datetime.
In [10]:
dtnow = dt.datetime.fromtimestamp(tm.time())
dtnow
Out[10]:
datetime.datetime(2019, 11, 7, 17, 54, 42, 650901)
Handy datetime attributes:
In [14]:
dtnow.year, dtnow.month, dtnow.day, dtnow.hour, dtnow.minute, dtnow.second # get year, mont
Out[14]:
(2019, 11, 7, 17, 54, 42)
timedelta is a duration expressing the difference between two dates.
In [15]:
delta = dt.timedelta(days = 100) # create a timedelta of 100 days
delta
Out[15]:
datetime.timedelta(100)
date.today returns the current local date.
In [16]:
today = dt.date.today()
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 13/28
6/25/2020 Week 1
In [17]:
today - delta # the date 100 days ago
Out[17]:
datetime.date(2019, 7, 30)
In [18]:
today > today-delta # compare dates
Out[18]:
True
The Python Programming Language: Objects and
map()
An example of a class in python:
In [19]:
class Person:
department = 'School of Information' #a class variable
def set_name(self, new_name): #a method
self.name = new_name
def set_location(self, new_location):
self.location = new_location
In [20]:
person = Person()
person.set_name('Christopher Brooks')
person.set_location('Ann Arbor, MI, USA')
print('{} live in {} and works in the department {}'.format(person.name, person.location, p
Christopher Brooks live in Ann Arbor, MI, USA and works in the department Sc
hool of Information
Here's an example of mapping the min function between two lists.
In [21]:
store1 = [10.00, 11.00, 12.34, 2.34]
store2 = [9.00, 11.10, 12.34, 2.01]
cheapest = map(min, store1, store2)
cheapest
Out[21]:
<map at 0x7f79f46617f0>
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 14/28
6/25/2020 Week 1
Now let's iterate through the map object to see the values.
In [22]:
for item in cheapest:
print(item)
9.0
11.0
12.34
2.01
The Python Programming Language: Lambda and List
Comprehensions
Here's an example of lambda that takes in three parameters and adds the first two.
In [23]:
my_function = lambda a, b, c : a + b
In [24]:
my_function(1, 2, 3)
Out[24]:
3
Let's iterate from 0 to 999 and return the even numbers.
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 15/28
6/25/2020 Week 1
In [25]:
my_list = []
for number in range(0, 1000):
if number % 2 == 0:
my_list.append(number)
my_list
Out[25]:
[0,
2,
4,
6,
8,
10,
12,
14,
16,
18,
20,
22,
24,
26,
28,
30,
32,
34,
Now the same thing but with list comprehension.
In [26]:
my_list = [number for number in range(0,1000) if number % 2 == 0]
my_list
Out[26]:
[0,
2,
4,
6,
8,
10,
12,
14,
16,
18,
20,
22,
24,
26,
28,
30,
32,
34,
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 16/28
6/25/2020 Week 1
The Python Programming Language: Numerical Python
(NumPy)
In [27]:
import numpy as np
Creating Arrays
Create a list and convert it to a numpy array
In [28]:
mylist = [1, 2, 3]
x = np.array(mylist)
x
Out[28]:
array([1, 2, 3])
Or just pass in a list directly
In [29]:
y = np.array([4, 5, 6])
y
Out[29]:
array([4, 5, 6])
Pass in a list of lists to create a multidimensional array.
In [30]:
m = np.array([[7, 8, 9], [10, 11, 12]])
m
Out[30]:
array([[ 7, 8, 9],
[10, 11, 12]])
Use the shape method to find the dimensions of the array. (rows, columns)
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 17/28
6/25/2020 Week 1
In [31]:
m.shape
Out[31]:
(2, 3)
arange returns evenly spaced values within a given interval.
In [33]:
n = np.arange(0, 30, 2) # start at 0 count up by 2, stop before 30
n
Out[33]:
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28])
reshape returns an array with the same data with a new shape.
In [34]:
n = n.reshape(3, 5) # reshape array to be 3x5
n
Out[34]:
array([[ 0, 2, 4, 6, 8],
[10, 12, 14, 16, 18],
[20, 22, 24, 26, 28]])
linspace returns evenly spaced numbers over a specified interval.
In [35]:
o = np.linspace(0, 4, 9) # return 9 evenly spaced values from 0 to 4
o
Out[35]:
array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. ])
resize changes the shape and size of array in-place.
In [36]:
o.resize(3, 3)
o
Out[36]:
array([[ 0. , 0.5, 1. ],
[ 1.5, 2. , 2.5],
[ 3. , 3.5, 4. ]])
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 18/28
6/25/2020 Week 1
ones returns a new array of given shape and type, filled with ones.
In [37]:
np.ones((3, 2))
Out[37]:
array([[ 1., 1.],
[ 1., 1.],
[ 1., 1.]])
zeros returns a new array of given shape and type, filled with zeros.
In [38]:
np.zeros((2, 3))
Out[38]:
array([[ 0., 0., 0.],
[ 0., 0., 0.]])
eye returns a 2-D array with ones on the diagonal and zeros elsewhere.
In [39]:
np.eye(3)
Out[39]:
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
diag extracts a diagonal or constructs a diagonal array.
In [40]:
np.diag(y)
Out[40]:
array([[4, 0, 0],
[0, 5, 0],
[0, 0, 6]])
Create an array using repeating list (or see np.tile)
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 19/28
6/25/2020 Week 1
In [41]:
np.array([1, 2, 3] * 3)
Out[41]:
array([1, 2, 3, 1, 2, 3, 1, 2, 3])
Repeat elements of an array using repeat.
In [42]:
np.repeat([1, 2, 3], 3)
Out[42]:
array([1, 1, 1, 2, 2, 2, 3, 3, 3])
Combining Arrays
In [43]:
p = np.ones([2, 3], int)
p
Out[43]:
array([[1, 1, 1],
[1, 1, 1]])
Use vstack to stack arrays in sequence vertically (row wise).
In [44]:
np.vstack([p, 2*p])
Out[44]:
array([[1, 1, 1],
[1, 1, 1],
[2, 2, 2],
[2, 2, 2]])
Use hstack to stack arrays in sequence horizontally (column wise).
In [45]:
np.hstack([p, 2*p])
Out[45]:
array([[1, 1, 1, 2, 2, 2],
[1, 1, 1, 2, 2, 2]])
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 20/28
6/25/2020 Week 1
Operations
Use +, -, *, / and ** to perform element wise addition, subtraction, multiplication, division and power.
In [46]:
print(x + y) # elementwise addition [1 2 3] + [4 5 6] = [5 7 9]
print(x - y) # elementwise subtraction [1 2 3] - [4 5 6] = [-3 -3 -3]
[5 7 9]
[-3 -3 -3]
In [47]:
print(x * y) # elementwise multiplication [1 2 3] * [4 5 6] = [4 10 18]
print(x / y) # elementwise divison [1 2 3] / [4 5 6] = [0.25 0.4 0.5]
[ 4 10 18]
[ 0.25 0.4 0.5 ]
In [48]:
print(x**2) # elementwise power [1 2 3] ^2 = [1 4 9]
[1 4 9]
Dot Product:
[]
y1
[ x1 x2 x3 ] ⋅ y2 = x 1y 1 + x 2y 2 + x 3y 3
y3
In [49]:
x.dot(y) # dot product 1*4 + 2*5 + 3*6
Out[49]:
32
In [50]:
z = np.array([y, y**2])
print(len(z)) # number of rows of array
Let's look at transposing arrays. Transposing permutes the dimensions of the array.
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 21/28
6/25/2020 Week 1
In [51]:
z = np.array([y, y**2])
z
Out[51]:
array([[ 4, 5, 6],
[16, 25, 36]])
The shape of array z is (2,3) before transposing.
In [52]:
z.shape
Out[52]:
(2, 3)
Use .T to get the transpose.
In [53]:
z.T
Out[53]:
array([[ 4, 16],
[ 5, 25],
[ 6, 36]])
The number of rows has swapped with the number of columns.
In [54]:
z.T.shape
Out[54]:
(3, 2)
Use .dtype to see the data type of the elements in the array.
In [55]:
z.dtype
Out[55]:
dtype('int64')
Use .astype to cast to a specific type.
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 22/28
6/25/2020 Week 1
In [56]:
z = z.astype('f')
z.dtype
Out[56]:
dtype('float32')
Math Functions
Numpy has many built in math functions that can be performed on arrays.
In [ ]:
a = np.array([-4, -2, 1, 3, 5])
In [ ]:
a.sum()
In [ ]:
a.max()
In [ ]:
a.min()
In [ ]:
a.mean()
In [ ]:
a.std()
argmax and argmin return the index of the maximum and minimum values in the array.
In [ ]:
a.argmax()
In [ ]:
a.argmin()
Indexing / Slicing
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 23/28
6/25/2020 Week 1
In [ ]:
s = np.arange(13)**2
s
Use bracket notation to get the value at a specific index. Remember that indexing starts at 0.
In [ ]:
s[0], s[4], s[-1]
Use : to indicate a range. array[start:stop]
Leaving start or stop empty will default to the beginning/end of the array.
In [ ]:
s[1:5]
Use negatives to count from the back.
In [ ]:
s[-4:]
A second : can be used to indicate step-size. array[start:stop:stepsize]
Here we are starting 5th element from the end, and counting backwards by 2 until the beginning of the array is
reached.
In [ ]:
s[-5::-2]
Let's look at a multidimensional array.
In [ ]:
r = np.arange(36)
r.resize((6, 6))
r
Use bracket notation to slice: array[row, column]
In [ ]:
r[2, 2]
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 24/28
6/25/2020 Week 1
And use : to select a range of rows or columns
In [ ]:
r[3, 3:6]
Here we are selecting all the rows up to (and not including) row 2, and all the columns up to (and not including)
the last column.
In [ ]:
r[:2, :-1]
This is a slice of the last row, and only every other element.
In [ ]:
r[-1, ::2]
We can also perform conditional indexing. Here we are selecting values from the array that are greater than 30.
(Also see np.where)
In [ ]:
r[r > 30]
Here we are assigning all values in the array that are greater than 30 to the value of 30.
In [ ]:
r[r > 30] = 30
r
Copying Data
Be careful with copying and modifying arrays in NumPy!
r2 is a slice of r
In [ ]:
r2 = r[:3,:3]
r2
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 25/28
6/25/2020 Week 1
Set this slice's values to zero ([:] selects the entire array)
In [ ]:
r2[:] = 0
r2
r has also been changed!
In [ ]:
To avoid this, use r.copy to create a copy that will not affect the original array
In [ ]:
r_copy = r.copy()
r_copy
Now when r_copy is modified, r will not be changed.
In [ ]:
r_copy[:] = 10
print(r_copy, '\n')
print(r)
Iterating Over Arrays
Let's create a new 4 by 3 array of random numbers 0-9.
In [57]:
test = np.random.randint(0, 10, (4,3))
test
Out[57]:
array([[3, 3, 2],
[4, 5, 9],
[3, 1, 2],
[3, 7, 6]])
Iterate by row:
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 26/28
6/25/2020 Week 1
In [58]:
for row in test:
print(row)
[3 3 2]
[4 5 9]
[3 1 2]
[3 7 6]
Iterate by index:
In [59]:
for i in range(len(test)):
print(test[i])
[3 3 2]
[4 5 9]
[3 1 2]
[3 7 6]
Iterate by row and index:
In [60]:
for i, row in enumerate(test):
print('row', i, 'is', row)
row 0 is [3 3 2]
row 1 is [4 5 9]
row 2 is [3 1 2]
row 3 is [3 7 6]
Use zip to iterate over multiple iterables.
In [61]:
test2 = test**2
test2
Out[61]:
array([[ 9, 9, 4],
[16, 25, 81],
[ 9, 1, 4],
[ 9, 49, 36]])
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 27/28
6/25/2020 Week 1
In [62]:
for i, j in zip(test, test2):
print(i,'+',j,'=',i+j)
[3 3 2] + [9 9 4] = [12 12 6]
[4 5 9] + [16 25 81] = [20 30 90]
[3 1 2] + [9 1 4] = [12 2 6]
[3 7 6] + [ 9 49 36] = [12 56 42]
In [ ]:
In [ ]:
https://vkxxljrcgrcikgyhgouxyg.coursera-apps.org/notebooks/Week 1.ipynb 28/28