Lab 01 - Curlss/Computational_physics GitHub Wiki

For Lab 1, we went through some of the basics of Python coding. The topics we went through were Python syntax, marking down, formatted printing, loops, conditionals, functions, and basic plotting. This lab was to familiarize yourself with some of these basics by changing or finishing the code provided.

In the first section, Python syntax, you were given a code, and you needed to fix it so the code would provide the proper result it would like. An example of one was that you were given the song and artist, and your goal was to print the sentence with the song and artist in it, the print statement was already given to us beside the {} around the song and artist so noticing that there wasn't any {} it wouldn't print the actual name of the song and artist.

song = "Someone Great"
artist = "LCD Soundsystem"

print(f'{song}is sung by {artist}') 

In the next section, we worked on Marking down, which is to help us review how to type on Python and write out bold and italics, as well as creating a bullet list and typing in equations so they turn out the way you would write it on paper and in order for that when writing an equation you must have $$ in the beginning and end so python knows it is an equation. An example would be

$v=v_o + at$

$\Delta{x}=v_ot+ {1/2}at^2$

The next section, formatted printing, was a review to help us shorten your decimals or have your print statement state a certain statement and print a certain number of decimals with that statement. Doing so the variable/name of whatever you are trying to get an answer from needs to be put in {} and to adjust the number of decimals displayed, is the :2( meaning 2 decimal places). An example was

print(f"my ratio = {myratio:.2f}")

The next section was creating and understanding different ways to create a list or pick out a specific word or number from the list to be printed. We used len() often to identify the sum of the list, and when trying to print a specific word or number, you must type the variable and then add [#], this addresses which word/number you are trying to find and the number in the [] will always start at 0 being the first number/word. An example is

words = ['Once upon a ','midnight dreary ','while I pondered ','weak and weary']
print(words[2])

In the section Loops, you were tested to understand how to create a while or for loops between ranges and to print out specific numbers such as evens or odds through the range. Also, you were tested to be able to place a list into a single string using a certain loop, which would be the for loop. An example is

words = ['Once upon a ','midnight dreary ','while I pondered ','weak and weary']
output = ""
for x in words:
  output= output + x
print(output)

The following section was Conditionals, a condition usually using if and else to make a statement that maybe one equation isn't the same as the other equation or they are the same. You can also use it to identify the differences and be able to point out a specific difference between the equations An example is

x = "00000000000000000000000000000000000000000000000000"
y = "00000000000000000000000000000000000000000000O00000"
if x == y:
  print('it is the same')
else:
  print('it is not the same')
if "O" in x:
  print('x contains O')
else:
  print('x does not contain O')
if "O" in y:
  print('y contains O')
else:
  print('y does not contain O')

The next section was creating a function using def, which is creating a function with a name of your choice and return so a value can be sent back to the function and provide the specific value, a function can be used with a while or for loop as well as an if statement. An example is

feet =100
def feet2miles(feet):
  return feet/5280



# This part is for testing the above.

m = feet2miles(5280)
print(m) # Should be 1

m = feet2miles(1)
print(m)

m = feet2miles(1000)
print(m)

In the final section of the lab, basic plotting was to learn how to plot the acceleration of a ball vs. time in the air as well as velocity vs. time and Position vs. time. Using kinematic equations to figure out velocity, acceleration, and position and use plt.plot to plot the graph of acceleration vs. time. This section also helped you understand how to label a graph as well as using plt.subplot which is (number of rows, number of columns, index) of the graph. An example is

pi = np.pi

theta= np.linspace(0,2*pi,100)

plt.figure(figsize=(10,10)) #figsize=(width,height)

plt.subplots_adjust(wspace=0.5, hspace=0.5)

plt.subplot(3,1,1)
plt.plot(theta, np.sin(theta),"bo", markersize= 4, alpha= .5)
plt.title('Sin Function')
plt.xlabel('Theta (radians)')
plt.ylabel('Sin (theta)')

plt.subplot(3,1,2)
plt.plot(theta, np.cos(theta), "k*");
plt.title('Cos Function')
plt.xlabel('Theta (radians)')
plt.ylabel('Cos (theta)')


plt.subplot(3,1,3)
plt.plot(theta, np.cos(theta), "k*");
plt.plot(theta,np.sin(theta), "bo")
plt.title('Sin and Cos Function')
plt.xlabel('Theta (radians)')
plt.ylabel('Sin and Cos (theta)')
plt.show()