"Welcome. In this tutorial, we're going to work through the absolute basics of the Python programming language. I highly recommend reading Chapter 2 of the textbook either before, or concurrently, with working through this tutorial. It will cover much the same ground, but here you'll get to actively practice the techniques described. \n",
"\n",
"Let's start with the concept of a **declaration**. In Python, our primary goal is to perform calculations - you can think of it as an extremely powerful powerful calculator, and indeed, much of what we build in python amount to pipelines that string simple mathematical computations together and perform them on data. \n",
"\n",
"In order to work with more complex data than a pocket calculator (in which we type in the numbers to be computed directly), Python allows us to declare **variables** to store those values for later use. See below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"variable_1 = 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"variable_2 = 6"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"output = variable_1 + variable_2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(output)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All I did was store the numbers 5 and 6 into variables (which I lazily and poorly named 'variable_1' and 'variable_2'), and then computed their sum. **Note:** One additional takeaway here is the underscore in the variable names - you cannot have spaces in variable names, so often programmers use underscores when needed. We'll talk later about best practices when naming variables. \n",
"\n",
"You might be asking, \"Why did you go through the work of declaring those variables and adding them, when you could've just done:\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5+6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this particular case, you are absolutely right. If I wanted to know the sum of 5 and 6, I could've just typed it in. Indeed, even if I needed to save that output somewhere, I could've done"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"output2 = 5+6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now, I can at any time look at that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(output2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or, I can use it in further calculations:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"output3 = output2*5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(output3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now might be a good time to go over the native mathematical operations available to you in Python (we've seen 2 so far). "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"addition = 5 + 5\n",
"subtraction = 5 - 5\n",
"multiplication = 5 * 5\n",
"division = 5 / 6 #be careful of Python version!!\n",
"exponentiation = 5**2\n",
"modulus = 5 % 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What did I mean by \"Be careful of Python Version\" in the comment above (**note:** you can use comments in lines of code, via the # symbol, to leave descriptions and instructions in your code that aren't seen or run by the interpreter). \n",
"\n",
"Let's do an experiment:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(division)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you're running this notebook in Python version 2.xx, the result will be 0. \n",
"\n",
"This is related to the **datatypes**. In Python version 2.xx (as opposed to any version of Python 3), numbers like those I've been using are treated as **integers** - the computer only knows their values to the \"one's place,\" and thus finds 5/6 to be 0. \n",
"\n",
"In Python 3, they resolved this issues by performing all calculations in **floating point** -- which means including the decimial values. We can do that manually ourselves in a few ways:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5.0 / 6.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"float(5) / 6 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In Python 2.x the above illustrates 3 points. The first is that when directly typing in numbers, just adding a \".\" turns the integer into a float, meaning the calculation is done correctly. The second is that you can force any integer to be a float by **typecasting** it using the command I showed (the same goes in reverse, you can use int(some_variable) to round what might be a floating point number to an integer. The third is that only one number in a calculation needs to be float for the whole calculation to be performed as a floating point operation - I could've chose either the 5 or the 6 to make a float, but only need to choose one. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In python three you can do integer division using the // operator: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5.0//6.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we see that Python carries out the integer division, but then the returned data type is a float. This is because the input values were float too. Note the subtle difference when the inputs are integers: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5//6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 1: A simple calculation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the box below, create 3 variables which hold your age and the ages of both of your parents. Then, set a variable named \"age_average\" that is equal to the average of your three ages. Be careful of order of operations! You can group operations, just like in PEMDAS math, using soft parenthesis \"()\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#your code here"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(age_average)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data Types in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far, we have been working entirely with numbers (integers and floats). You can tell what data type a variable is at any time using the \"type()\" command:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 5\n",
"y = 6.0\n",
"print(type(x))\n",
"print(type(y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"While, at the very bottom of things, your data will always be numbers like this, Python's power comes in when you start looking at it's other data types, which are primarily set up to contain numbers in an organized way. Here are the basic data types in Python:\n",
"\n",
" * Integers\n",
" * Floats\n",
" * Booleans (True or False)\n",
" * Lists (collections of items) \n",
" * Dictionaries (collections accessed via \"keys\") \n",
" * Strings (contained in quotes \"like this\")\n",
" * Tuples (like lists, but immutable (unchangeable)) \n",
" \n",
"In the next few sections, we are going to learn about these data types (skipping integers and floats, which we've mostly covered)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Booleans"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Booleans have a one of two states,\"True\" or \"False\". Try setting a variable equal to True or False in the box below - you should see Python \"color\" the word to indicate syntactically that it is a special word in Python that has a specific meaning."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"true = True\n",
"false = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"true"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These come in handy when we are employing **condiditional statements** (coming up below) in which we say \"Hey code, if some condition is True, do \"X,\" else if some other condition is True, do \"Y\". \n",
"\n",
"Often we are using booleans without recognizing it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lists"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lists are the most forgiving container in Python. Basically, we can shove whatever we want into a list, be they different data types, or even lists within lists. Of course, the usefulness of non-uniform containers becomes limited - the advantage to storing lots of numbers in a list is that you can then perform operations on them all without worrying that some won't work. \n",
"\n",
"Here's how we define a list:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list_1 = ['a string', 5, 6.0, True, [5,6,6]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The list above is kind of a mess - you'd almost never want a list to contain such a variety of things in it, but I wanted to highlight that, in principle, Python doesn't care what you stick inside a list. In addition to manually specifiying what is in a list, we can use some generative functions to make lists for us when they have a regular form:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"count_to_10 = list(range(1,11))\n",
"print(count_to_10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What I've done above is run the \"range()\" function, which generates an \"iterator\" type. We won't go into the details of what that means here, but by **typecasting** it as a list, it creates a list of numbers counting up. \n",
"\n",
"The form of the function is range(start, stop, step), where \"start\" is inclusive and \"stop\" is exclusive (e.g. [1,11) ). If I wanted 0-9, I would do"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list(range(10))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"using the shortcut that if only 1 argument is used, \"start\" is assumed to be 0 and \"step\" is assumed to be 1 (it has to be an integer). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2: skip-count\n",
"Generate a list below containing the numbers 2, 4, 6, 8, ... 100 and save it into a variable called skip_count. Then, below, print it to see you did it right."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"skip_count = "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The tutorial on Loops and Conditionals is a great place to jump to once you have the hang of what lists are and how they're defined - that tutorial shows how we use them.\n",
"\n",
"### Indexing\n",
"When we want to see what is in a list, we print it. But sometimes we want to \"pull out\" individual elements of a list and use them in calculations. For that, we need to \"slice\" or **index** the list for the element we want. Lists are indexed such that the first element is assigned \"0\" (to remember this, I got into the habit of calling it the \"0th index\"). For example, using the list from above:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list_1[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Basically, we put closed brackets at the end of the variable name and specify which index we want. We can also pull multiple:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list_1[0:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that the 2 is not inclusive (it pulls 0th and 1st). We can also specify a skip:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list_1[0:5:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So what about that list within a list? If we want a specific number from it, we can use double indexing. Also, I'll use this as a spot to show **negative indexing** which lets you count backwards from the end of a list (if that happens to be easier):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list_1[-1][1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What I did was pull the -1st element (final element, 2nd to last would be -2, etc), which was the inner list, and then I indexed *that* for it's 1st element. \n",
"\n",
"**Try it out:** In the cell below, get it to output the \"s\" in \"a string\" in the 0th element of the list. (It works the same way). "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionaries\n",
"Dictionaries are like lists, but rather than indexing them by element number as we were doing above, we index them by a special \"key\" that we assign to each \"value\". For example:"
"Dictionaries are inherently unordered - the order I define things within the dictionary doesn't matter, only that I know the key associated with each value. For some applications, this has advantages over a list. Here, if I were storing the ages of students in a class and was only interested in things like the average and median age, a list would be fine. But if I needed to know *who* was *which* age throughout my analysis, a list would require me to impose that the order went \"sam, sarah, kim, mukund,\" and to remember that order when indexing, and if the order in the list changes, keep track of those too. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Strings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We've already seen strings used - It lets you store things like words (or, later on, filepaths) in your code that otherwise don't have meaning as far as Python is concerned. Strings are the most forgiving data type of all; you can stick literally anything inside. If you have a data file with tons of different types of data in it, Python will often just read in everything as strings and let you work out how to convert the proper things into ints, floats, etc. Strings are iterable - they can be indexed like lists, character by character. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tuples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We don't use them to often, so just check out this example and follow aloong!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"example_list = [1,2,5]\n",
"example_tuple = (1,2,5)\n",
"example_list[0] = 2\n",
"print(example_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So I've successfully defined it as a tuple and list, and changed the 0th entry of the list. But what if I try on the tuple?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"example_tuple[0] = 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Thus, I get an error. So basically, if you make a list-type item and want to make sure it can never be adjusted in your code, you can make it a tuple. \n",
"\n",
"For more examples of the basic manipulations you can make to these basic data types, check out Chapter 2 of the textbook, which lays some of them out!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 3: Bringing it together (kind of)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try a single, longer example that (tries to) bring in the things we've seen above. You'll find that part 2 of this bootcamp covers things like iterating/for loops and conditionals, which drastically increase what you can do with Python. Nevertheless, here's some practice with the basic operations. \n",
"\n",
"Let's say you're the teacher of your school's introductory Quantum Mechanics class. You've just graded their first midterm, and are shocked, (*shocked*) to see so many low scores (You thought the midterm was totally reasonable!) \n",
"\n",
"Before you post their individual scores, which might give some students a heart attack, you decide to calculate the distribution statistics of the exam first, so that each student can compare their score to the average, etc. \n",
"Your first step to analyzing these numbers should be to put them in a list (call it \"scores\"). Do that in a cell below (you can copy and paste from above, just add the list syntax). "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scores = #your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, the first thing we need to do is calculate the average score. Later on, we'll see that there are external functions you can import into Python that will just do this for you, but for now let's calculate it manually (it's easy enough, right?). \n",
"\n",
"Above, we saw use of the sum() function - as it turns out, you can run the sum() function on a list (so long as it only contains numbers) and it will tell you the sum. The only other thing you'll need to calculate the average is the len() function, which returns the number of elements in a list/array. Using those two, define a variable below called \"average_score\" and calculate it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"average_score = #your code here\n",
"average_score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Great, so we now know what the average score on the test was. Let's figure out what that is in percent. In the cell below, calculate the percentage value of the average score by dividing it by the number of points on the test, and mulitplying that by 100 in the same line. Then, run the cell - you'll see a nice sentence output that lists the percentage, take a look at the line I wrote that does this and see if you can glean how it worked."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"avg_score_percent = #your code here\n",
"\n",
"shortened = str(avg_score_percent) #turn it into a string\n",
"statement = \"The average score on the test was a {}%\".format(str_version[0:5]) #use indexing on the string to drop the everything past the second decimal\n",
"print(statement)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Okay, so the other thing students are always interested in is the standard deviation from the mean - this basically will tell them whether they get an A, B, C, D, or F on the test assuming you curve. Let's be nice, and assume we are going to curve the average to a flat B (85%). The formula for a standard deviation is \n",
"where $\\mu$ is the average and N is the total number of scores."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We already know how to get N, and we know what $\\mu$ is as well. So to calculate this, we need to know how to calculate the quantity on the top of the fraction. This is actually kind of tricky with the methods we have on hand, so I'm going to introduce a new concept: Numpy (numerical python) arrays. I'm going to get into these in detail in part 2 of the bootcamp, but for now, see the example below for elucidation on why we're about to use them:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"arr_version = np.array(scores)\n",
"print(scores-1)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Okay, so I can't subtract an integer from a list. What if I try the array version?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(arr_version-1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you look, you should see that each of those scores is the original score with one subtracted off it. Your spidey senses should be tingling then for how we can leverage this functionality to calculate our STD. In the cell below, fill in the variable I'm calling \"top_frac\" to calculate this quantity:\n",
"$$\n",
"\\sum_{i=1}^N (x_i - \\mu)^2\n",
"$$\n",
"\n",
"Notice here that you don't have to actually calculate it one by one - if we first compute a single array that represents each score with the mean subtracted off and then that value squared, then we finish off top_frac just by summing up that array as we've done before. Feel free to use my variable \"arr_version\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"top_frac = #your code here\n",
"print(top_frac)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With that done, we can easily apply the formula to get the final STD - **Hint:** the function np.sqrt() will be useful here."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"STD_scores = #your code here\n",
"print(STD_scores)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alright! If you've done everything correctly, you should have found that the average score is a 61/120, with a stadard deviation of 28. Let's, for fun, make a helpful plot to show the students their scores. Don't worry about how the plotting stuff works just yet, we'll dive into it more in part 2, but see if you can figure out what each part of the command is doing."
"Nice! It looks like our formula for standard deviation successfully describes the original distribution of scores pretty well. Now, how to get them to do better on midterm 2...."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Wrap up"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I hope this super-basic introduction has given you a glimpse at some of the basic functionality of Python. Of course, Python is way more powerful than what has been shown here. I call this Part 1 because once you know the basic data types, how to define variables, and do some simple math on them, we are going to need to jump into new concepts — for loops and conditional statements, as well as invoke new libraries (like numpy and matplotlib) to do make further progress. If you're ready to do that, head on over to Part 2! "
"To submit this assignment, first make sure that all the cells have been run and are showing the outputs that you expect. Then you can print this file to PDF. You must combine this PDF with a PDF from Part 2 of the bootcamp before submitting it to gradescope. \n",
"\n",
"Here's a handy website that makes merging PDFs easy: \n",