Day 1
Contents
1. Day 1#
1.1. Why Coding and Python?#
Great for data processing and simulations
Coding is fast at dealing with large amounts of data and math
Python, very versatile and popular
Very active community developing libraries for it
One of the more user beginner friendly languages
Skills transfer to other languages
The goal of this workshop is to teach you the practical basics and fundamentals, but also show you how to access resources and teach yourself.
1.2. Part 1: Variables#
Variables are at the core of coding. Each variable contains a piece of data once assigned.
Types
Integers
Floats (decimals)
Strings (text)
a = 4
b = 4.0
c = 'four'
examplevariable = 54
To see what code does, we need a way for it display information to us. That’s the print()
function. Whatever goes in the parentheses will be printed out for us. Notice that even though I inputted ‘b’ below, it showed us 4.0
instead of b
. That’s because we assigned b
as a variable above:
print(b)
4.0
Alternatively, we can directly tell the Python to print the float 4.0:
print(4.0)
4.0
print
is what we call a function in Python. It takes an input then perfroms an action. We’ll learn more about those tomorrow.
If we wanted to actually print ‘b’ instead of what we have assigned it to, we can directly put the string into the function:
print('b')
b
By putting quotes around it, ‘b’ is no longer a variable and is now just text part of a string. The print
function is incredibly useful in outputting data, debugging, and developing. As a final note, print can output multiple variables or objects with one command:
print(a,b,c, examplevariable)
4 4.0 four 54
print('The value of b is',b)
The value of b is 4.0
print(b,'is spelled',c)
4.0 is spelled four
Finally, we can change variable types using the following functions:
var = 7.2
print(var)
print(int(var))
print(str(var))
7.2
7
7.2
Variables can contain numbers, but not start with them!
2014total = 2
print( 2014total )
File "<ipython-input-9-0b5199b48ea9>", line 1
2014total = 2
^
SyntaxError: invalid syntax
1.3. Part 2: Operations and Basic Math#
With integers and floats we can perform a lot basic math with Python:
s = 4
t = 9
added = s + t
print(added)
subtracted = s - t
print(subtracted)
multiplied = s * t
print(multiplied)
print(t*s)
print(t/3)
print(t**2)
13
-5
36
36
3.0
81
Python will follow the basic order of operations, so use parentheses ot accomplish what you want:
print( t**1/2 )
print( t**(1/2))
4.5
3.0
As a note, floats and integers can be operated together even though they are different variable types.
m = 8.0
n = 2
o = '4'
print(m+n)
print(m+o)
10.0
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-c66be780a72b> in <module>
4
5 print(m+n)
----> 6 print(m+o)
TypeError: unsupported operand type(s) for +: 'float' and 'str'
Be careful when re-assigning variables! If a variables is assigned a second time, it will overwrite the first.
m = 6
p = m+n
print(p)
8
However, variables are not inherently linked. In the example below, when we assigned r
, it is equal to m + n
(6 + 2). Even though we change m = 5
in the next line, r
is still 6 + 2.
r = m + n
print(r)
m = 5
print(r)
r = m + n
print(r)
8
8
7
1.4. Part 3: Lists#
Lists are one of many ways to store multiple pieces of data. Use []
to create them and separate data with commas.
mylist = [a, b, c]
print(mylist)
mylist2 = [4, 4.0, 'four']
print(mylist2)
[4, 4.0, 'four']
[4, 4.0, 'four']
Most of the time, everything in your list will be the same variables type. Using the append()
function you can add more to your list.
mylist.append('five')
print(mylist)
[4, 4.0, 'four', 'five']
Unfortunately, operations don’t work as you would expect them to with lists.
list1 = [1,2,3,4,5]
list2 = [2,2,2,2,2]
print(list1+list2)
print(list1*3)
[1, 2, 3, 4, 5, 2, 2, 2, 2, 2]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
How can we do math with lists then? We first need to talk about indexing lists and loops. If we only want a specific piece of data in a list, we have to use it’s index.
months = ['jan','feb','march','april','may']
print(months[3])
april
print(list1[0]+list2[0])
3
So now, for each element in the list, I could manually add them together with each index. That sounds like a lot work though. This is where loops are helpful.
1.5. Part 4: Loops#
Loops are an essential part of coding. Many times, there is an operation we want to repeat over and over. Loops help us accomlish this. As we saw with mylist
above, we can’t perform math on lists. But we can “loop” over each individual element and perform the math. This is called a for loop.
list3 = [4,7,4,6,3]
newlist = []
#This line basically says, go over each element in list3 and assign that element to i
#So, the first iteration, i is equal to 4, then 7, and so forth
for i in list3:
newlist.append(i*3)
print (newlist)
[12]
[12, 21]
[12, 21, 12]
[12, 21, 12, 18]
[12, 21, 12, 18, 9]
Another way to make for loops is by iterating over a range of numbers. This lets us loop over the index of multiple lists:
list1 = [1,2,3,4,5]
list2 = [2,2,2,2,2]
#We need to create an empy list before we can append anything to it.
addedlist = []
#This time, by looping over a range, i will be 0 the first iteration, and increase by one each time.
for i in range(5):
print(i)
#The next line effectively adds the corresponding elements of list1 and list2 together
#ie, the first element of list1 is added to the first element of list2, the second element of list1 is added to the second element of list2
addedlist.append(list1[i] + list2[i])
print(addedlist)
0
1
2
3
4
[3, 4, 5, 6, 7]
Time permitting: review the len()
function. If you put a list into the len()
function, it will tell you how many elements are in that list. Using this, the for loop can detect the length of your list automatically to set the number of iterations.
print(len(list1))
5
addedlist = []
for i in range(len(list1)):
print(list1[i] + list2[i])
addedlist.append(list1[i] + list2[i])
print(addedlist)
3
4
5
6
7
[3, 4, 5, 6, 7]
Note, this is not the fastest way to add lists together but we will get more into that Day 2.
1.6. Part 5: Basic NumPy#
Numpy is a mathematical based library with extra capabilities. Libraries are “add-ons” developed by other programmers. They add extra functions and capabilities. To import NumPy use the command below:
import numpy as np
Now whenever we want to use a NumPy command, refer to it with np.
k = np.log(2)
print(k)
l = np.sqrt(100)
print(l)
0.6931471805599453
10.0
np.linspace
can be a useful tool. It creaes an array (similar to a list but more on that tomorrow) that starts and stops at the first 2 numbers. The third number you input contains the total number of elements evenly spaced.
numbers = np.linspace(0, 100, 51)
print(numbers)
[ 0. 2. 4. 6. 8. 10. 12. 14. 16. 18. 20. 22. 24. 26.
28. 30. 32. 34. 36. 38. 40. 42. 44. 46. 48. 50. 52. 54.
56. 58. 60. 62. 64. 66. 68. 70. 72. 74. 76. 78. 80. 82.
84. 86. 88. 90. 92. 94. 96. 98. 100.]
1.7. Part 6: Simple Plotting#
Similar to NumPy, plotting also requires a library. Here is how you import it:
import matplotlib.pyplot as plt
So any commands we need to do with plotting will have to use plt.
first. For now, let’s just show 2 basic types of plots. Create some arbitrary x values and y values to plot. Then, it’s just a matter of plugging in the values into the correct functions and displaying the graph.
x = np.linspace(0, 50, 21)
y = np.linspace(30,40, 21)
#for a line plot:
plt.plot(x,y)
#display graph command
plt.show()

#for a scatter plot
plt.scatter(x, y)
#Show plot
plt.show()

1.8. Part 7: Importing Data#
You always want to look at the file you’re importing first. First we need to upload the file to our notebook. Click the file icon to the left toobar on Google Colab, and then click to upload button. Choose “Day1example.dat” from your computer.
What does it contain? With the “Day1example.dat” file, be can see that the first row contains two headers, and the rows after contain random floats. How can we import this data to use?
Now lets get into the code:
#We need to create empty lists to store data:
xvals = []
yvals = []
#This line opens up the file. Make sure the include quotes around Day1example.dat to make it a string.
#otherwise Python will think Day1example.dat is some sort of variable.
#The 'r' in the open function tells Python we want to read this file.
#The 'as infile' portion assings the text in the file to the variable infile.
with open('Day1example.txt','r') as infile:
#We don't care about the first line, so to read over it and move on use this command
headers = infile.readline()
#Let's make sure it got rid of what we wanted
print(headers)
#Let's loop over the rest of the lines. The open command once it has read a line will get rid of it so we don't need to worry about that first line anymore.
for line in infile:
#This command splits up each line at every space.
#If we entered some text (string) into the parentheses of split(), then it would separate the lines
#at every instance of that text (for a comma separated file, we would want line.split(',') )
data1, data2 = line.split()
#add the data to the lists.
#IMPORTANT: any text read from a file is a string. We must convert it floats to perform any plotting or math
xvals.append(float(data1))
yvals.append(float(data2))
#Plot Them!
plt.scatter(xvals, yvals)
plt.show()
xvals yvals
