This is the transcript of the lecture material from Friday, November 15, 2024.
a = (2,3,4)
b = [2,3,4]
Let's see what each one of these really is.
type(a)
tuple
type(b)
list
Collection a is a tuple, while collection b is a list. What is the difference? Let's start by changing an element of list b.
b[1] = 5
print(b)
[2, 5, 4]
Ok. No problem. This worked. Notice that b[1] is the SECOND element of the list. Remember, python indexing starts with 0, not 1 like Fortran.
Great. Now let's try and change an element of tuple a.
a[1] = 5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[6], line 1 ----> 1 a[1] = 5 TypeError: 'tuple' object does not support item assignment
We get an error. Why? The reason is that lists are mutable (able to be changed), and tuples are immutable (not able to be changed).
When we tried to change an element of the tuple, python threw an error.
Let's look at another list.
b = [91, 'Lions', 273.15]
type(b[0])
int
In python, lists and tuples can contain elements of different types. Our new list b contains an integer, string, and float.
What happens with this line?
print(a[3])
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[9], line 1 ----> 1 print(a[3]) IndexError: tuple index out of range
We get an error because we are trying to reference and element that is outside of the defined range.
Tuple a contains three things (2,3,4), but you reference them through a[0], a[1], a[2]. The valid indices are 0, 1, and 2. Not 3.
Let's look at another feature of python. Here is a new list.
a = ['Lions', 'Tigers', 'Bears']
Now, let's set list b equal to a and verify that the command worked.
b = a
print(a,b)
['Lions', 'Tigers', 'Bears'] ['Lions', 'Tigers', 'Bears']
Great. Everything looks good. Now, let's change the second element (index 1) of list a and print out our lists again.
a[1] = 'Bananas'
print(a,b)
['Lions', 'Bananas', 'Bears'] ['Lions', 'Bananas', 'Bears']
What happened? Both list a and list b changed. This is because the statement b=a is what is called a "soft copy."
This is often not desireable. What do we do? We make something called a "hard copy."
c = a[:]
print(a,b,c)
['Lions', 'Bananas', 'Bears'] ['Lions', 'Bananas', 'Bears'] ['Lions', 'Bananas', 'Bears']
a[1] = 'Apples'
print(a,b,c)
['Lions', 'Apples', 'Bears'] ['Lions', 'Apples', 'Bears'] ['Lions', 'Bananas', 'Bears']
c = a[:] is called a "hard copy" and notice when we change list a again, b changed, but c didn't.
The reason is that list c is a "hard copy" of list a and list b is a "soft copy" of list a.
Ok, let's move on to something else.
import numpy as np
import matplotlib.pyplot as plt
These two lines import two packages from the python libraries.
Notice the use of np and plt as aliases to the numpy and matplotlib packages.
x = np.linspace(-2,2,200)
The line above creates a list of numbers from -2 to 2 with 200 equally spaced points.
Let's print the list to understand it bettter.
print(x)
[-2. -1.9798995 -1.95979899 -1.93969849 -1.91959799 -1.89949749 -1.87939698 -1.85929648 -1.83919598 -1.81909548 -1.79899497 -1.77889447 -1.75879397 -1.73869347 -1.71859296 -1.69849246 -1.67839196 -1.65829146 -1.63819095 -1.61809045 -1.59798995 -1.57788945 -1.55778894 -1.53768844 -1.51758794 -1.49748744 -1.47738693 -1.45728643 -1.43718593 -1.41708543 -1.39698492 -1.37688442 -1.35678392 -1.33668342 -1.31658291 -1.29648241 -1.27638191 -1.25628141 -1.2361809 -1.2160804 -1.1959799 -1.1758794 -1.15577889 -1.13567839 -1.11557789 -1.09547739 -1.07537688 -1.05527638 -1.03517588 -1.01507538 -0.99497487 -0.97487437 -0.95477387 -0.93467337 -0.91457286 -0.89447236 -0.87437186 -0.85427136 -0.83417085 -0.81407035 -0.79396985 -0.77386935 -0.75376884 -0.73366834 -0.71356784 -0.69346734 -0.67336683 -0.65326633 -0.63316583 -0.61306533 -0.59296482 -0.57286432 -0.55276382 -0.53266332 -0.51256281 -0.49246231 -0.47236181 -0.45226131 -0.4321608 -0.4120603 -0.3919598 -0.3718593 -0.35175879 -0.33165829 -0.31155779 -0.29145729 -0.27135678 -0.25125628 -0.23115578 -0.21105528 -0.19095477 -0.17085427 -0.15075377 -0.13065327 -0.11055276 -0.09045226 -0.07035176 -0.05025126 -0.03015075 -0.01005025 0.01005025 0.03015075 0.05025126 0.07035176 0.09045226 0.11055276 0.13065327 0.15075377 0.17085427 0.19095477 0.21105528 0.23115578 0.25125628 0.27135678 0.29145729 0.31155779 0.33165829 0.35175879 0.3718593 0.3919598 0.4120603 0.4321608 0.45226131 0.47236181 0.49246231 0.51256281 0.53266332 0.55276382 0.57286432 0.59296482 0.61306533 0.63316583 0.65326633 0.67336683 0.69346734 0.71356784 0.73366834 0.75376884 0.77386935 0.79396985 0.81407035 0.83417085 0.85427136 0.87437186 0.89447236 0.91457286 0.93467337 0.95477387 0.97487437 0.99497487 1.01507538 1.03517588 1.05527638 1.07537688 1.09547739 1.11557789 1.13567839 1.15577889 1.1758794 1.1959799 1.2160804 1.2361809 1.25628141 1.27638191 1.29648241 1.31658291 1.33668342 1.35678392 1.37688442 1.39698492 1.41708543 1.43718593 1.45728643 1.47738693 1.49748744 1.51758794 1.53768844 1.55778894 1.57788945 1.59798995 1.61809045 1.63819095 1.65829146 1.67839196 1.69849246 1.71859296 1.73869347 1.75879397 1.77889447 1.79899497 1.81909548 1.83919598 1.85929648 1.87939698 1.89949749 1.91959799 1.93969849 1.95979899 1.9798995 2. ]
Now that we have the list, let's do something cool with it like make a plot.
plt.plot(x,x**2, color="red")
plt.xlabel('x')
plt.ylabel('y')
plt.title("$y = x^2$", fontdict={'size':22})
Text(0.5, 1.0, '$y = x^2$')
The code in the box above creates a plot of x^2, labels the x and y axis, and gives the plot a title.
Feel free to play around with this. Change the plot color, the size of the title, etc.
In the next cells, I create a new list and print the list for verification.
This should look familiar.
mylist = [91, 13, 5, 19, 9]
print(mylist)
[91, 13, 5, 19, 9]
Examine the following code. This is an example of a loop structure in python.
for item in mylist:
print(item)
91 13 5 19 9
What this does is go through each element in the list mylist, place that element of the list into
the variable item and then prints the variable. Notice that each element in the list is printed separately.
We can take this another step with the following code. Look it over and make sure you understand it.
for item in mylist:
print(item, item**2, item**3)
# I could also do more here. It doesn't need to be just one line.
91 8281 753571 13 169 2197 5 25 125 19 361 6859 9 81 729
Also note the use of an inline comment in python using the hashtag.
The following code uses the range function to create a list and prints out the items of the list.
Notice it only goes from 1 to 10.
for i in range(1,11):
print(i)
1 2 3 4 5 6 7 8 9 10
See if you understand the output from the following code.
for i in range(4,8,2):
print(1)
1 1
Ok, I fooled you a bit there, but you should still understand the output.
The code below is what you were expecting and the output.
for i in range(4,8,2):
print(i)
4 6