#!/usr/bin/env python
# coding: utf-8
# This is the transcript of the lecture material from Friday, November 15, 2024.
#
# Let's start by building two collections that look like arrays in Fortran. Notice the difference in notation between a and b, however.
# Collection a uses ()'s, while b uses []'s.
# In[1]:
a = (2,3,4)
b = [2,3,4]
# Let's see what each one of these really is.
# In[2]:
type(a)
# In[3]:
type(b)
# 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.
# In[4]:
b[1] = 5
# In[5]:
print(b)
# 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.
# In[6]:
a[1] = 5
# 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.
# In[7]:
b = [91, 'Lions', 273.15]
# In[8]:
type(b[0])
# 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?
# In[9]:
print(a[3])
# 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.
# In[10]:
a = ['Lions', 'Tigers', 'Bears']
# Now, let's set list b equal to a and verify that the command worked.
# In[11]:
b = a
# In[12]:
print(a,b)
# Great. Everything looks good. Now, let's change the second element (index 1) of list a and print out our lists again.
# In[13]:
a[1] = 'Bananas'
# In[14]:
print(a,b)
# 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."
# In[15]:
c = a[:]
# In[16]:
print(a,b,c)
# In[17]:
a[1] = 'Apples'
# In[18]:
print(a,b,c)
# 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.
# In[19]:
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.
# In[20]:
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.
# In[21]:
print(x)
# Now that we have the list, let's do something cool with it like make a plot.
# In[22]:
plt.plot(x,x**2, color="red")
plt.xlabel('x')
plt.ylabel('y')
plt.title("$y = x^2$", fontdict={'size':22})
# 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.
# In[23]:
mylist = [91, 13, 5, 19, 9]
# In[24]:
print(mylist)
# Examine the following code. This is an example of a loop structure in python.
# In[25]:
for item in mylist:
print(item)
# 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.
# In[26]:
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.
# 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.
# In[27]:
for i in range(1,11):
print(i)
# See if you understand the output from the following code.
# In[28]:
for i in range(4,8,2):
print(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.
# In[29]:
for i in range(4,8,2):
print(i)