Python Lab 1 - Cart1233/PHYS250 GitHub Wiki
Goal
The goal of this lab was to review important concepts in python like loops and lists, functions, conditionals, and plotting.
Overview
In this lab, I created a series of loops, lists, conditionals, functions and plots as a review for future python use.
Functions
For the first function problem, it asked to create to convert feet to miles and return miles.
def feet2miles(feet=100):
"""This function takes in a feet to miles calculation and returns miles"""
miles=feet/5280
return miles
The second one asked for an array of feet values ranging ding from 1000 to 20000 in increments of 1000.
myfeet=np.arange(100,21000,1000)
print(myfeet)
Conditionals
The first conditional question asked to test which of the given multiples were larger and to print it to the screen.
prod1=25*32
prod2=17*49
if prod1> prod2:
print("The product of 25*32 is larger!")
elif prod1<prod2:
print("The product of 17*49 is larger!")
elif prod1==prod2:
print("The two products are equal")
The rest were all similar.
Loops + Lists
The loops were mostly straightforward and asked things like the print the even numbers between 0-10.
for i in range (0,11,2):
#This gives the even numbers from 0-10
print(i)
We used a while loop to calculate multiples within a range.
total1=0
i=0
while i<=100:
if i %3==0:
total1 += 1
i+=1
print(total1)
We also looped over a list.
fib = [1,2,3,5,8,13,21,34,55,84]
length=len(fib)
# Loop over this list and print out the entries 2 ways:
# * Just iterating over each entry in the list
# * Using "range" and referencing the entries by index
for i in fib:
#Itterates over each entry in the list
print(i)
for b in range(length):
#Uses range to reference each index
print(fib[b])
As well as printing entries into a string from a list.
words = ['Once upon a ','midnight dreary ','while I pondered ','weak and weary']
mystring=''
for p in words:
mystring+=''+p
print(mystring)
For the lists portion it was all very straightforward questions.
Graphs and Figures
For our first plot we were asked to make figures of acceleration vs time, velocity vs time, and position vs time. We needed to find all their respective equations and use them to plot. For time I gave it 100 values ranging from 0-5 to make the figures.
h0=50
v0=15
g=9.8
t=np.linspace(0,5,100)
def h(t):
return h0 + v0*t - .5*g*(t**2)
def v(t):
return v0-g*t
def a(t):
return -g*np.ones(len(t))
plt.subplot(3,1,1)
plt.plot(t,a(t))
#plt.title('Acceleration vs. Time')
plt.xlabel('Time (Seconds)')
plt.ylabel('Acceleration (m/s^2)')
plt.legend()
plt.subplot(3,1,2)
plt.plot(t,v(t))
#plt.title('Velocity vs. Time')
plt.xlabel("Time (Seconds)")
plt.ylabel('Velocity (m/s)')
plt.legend()
plt.subplot(3,1,3)
plt.plot(t,h(t))
#plt.title('Height vs. Time')
plt.xlabel('Time (Seconds)')
plt.ylabel('Velocity (m/s)')
plt.legend()
For the second plot it simply asked for a sin and cos plot.
theta = np.linspace(0,2*pi,100)
y=np.sin(theta)
x=np.cos(theta)
plt.subplot(2,1,1)
plt.plot(theta,y, label='Sin(theta)')
plt.title("Sin(theta)")
plt.xlabel('Theta (radians)')
plt.ylabel('Sin(theta)')
plt.legend()
plt.subplot(2,1,2)
plt.plot(theta,x,label='Cos(theta)')
plt.title('Cos(theta)')
plt.xlabel("Theta (radians)")
plt.ylabel('Cos(theta)')
plt.legend()
plt.tight_layout()
plt.show()