Hey Guys and Gals, I am glad you found your way to this starter notebook!
I will walk you through the basics so that you can read and write your own code. I do recommend you check out the video tutorials by Sentex, especially his tutorial on the basics."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What you are looking at right now is called a jupyter notebook. These are great because they allow you to write code and words in different boxes, called cells, and run them whenever you would like. In the top left are your buttons. From left to right they are: save, insert new cell, cut cell, copy cell, paste cell, move cell up, move cell down, run the cell, interrupt the running code, restart (think start over), and run all cells. The most important are the add new cell button and the run button since you will want to be able to run and play around with your own code. There are also hotkeys for many of these commands, for example, you can run a cell by pressing shift+enter.
Now lets get started!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Printing, variables, functions
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before you dive into a new programming language, the common first task is to print \"Hello World!\". To do this we will begin by using the print() function built into python, which will write out whatever is placed inside of the parenthesis. Here is an example that you can run!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Hello World!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will notice that I had to place the words in \" \". This is because python assumes that words are variables, which are ways you can reference values. The print function will print out a string, which is just a group of characters, of whatever is given to it. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Besides just using strings, you can make your own variables and print those. First lets take a look at some different variables. The variable name can be nearly anything you want, so long as it is all connected together and does not start with a number. It is also case-sensitive."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"number = 5 \n",
"decimal = 0.2\n",
"letter = 'a'\n",
"sentence = 'This is a sentence.'\n",
"boolean = True\n",
"Whatever_I_Like = \"Something\"\n",
"Whatever_i_Like = \"Something else\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(number)\n",
"print(decimal)\n",
"print(letter)\n",
"print(sentence)\n",
"print(boolean)\n",
"print(Whatever_I_Like)\n",
"print(Whatever_i_Like)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also print multiple things at once by seperating each part with a comma."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(sentence, \"This is also a sentence.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python accepts basic math and solves this before running the print function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Number =\", number)\n",
"print(\"Number + 1 =\", number+1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following shows some different math operations and comments next to the code. The comments do not run any code but they can make it easy to understand what is going on. Python makes anything in the same line after # a comment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"six = number + 1 \n",
"print(six) \n",
"print(six/2) # 6/2 = 3\n",
"print(six*2) # 6*2 = 12\n",
"print(six**2) # 6^2 = 36"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We also have other data types which help for combining things together. Lists are made by putting square brackets [ ] around the values and seperating them with commas. Then you can get specific values out by asking for the index of the value you want, starting with the first element as number 0."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# this is a list\n",
"my_list = ['first', 'second', 'third']\n",
"\n",
"# here we can get the values from the list by asking for the value by index, starting from 0\n",
"# or the whole list together without an index\n",
"\n",
"print(my_list[0])\n",
"print(my_list[1])\n",
"print(my_list[2])\n",
"# so it goes [0] -> 'one', [1] -> 'two', [2] -> 'three'\n",
"\n",
"print(my_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also see that Python does not care if there are empty lines in the code, but it is nicer to look at if there are some blank lines to seperate different ideas."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
If statments, for loops, while loops
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All of these things may not seem very useful, since you still have to type everything you want it to do. Now we will look at how to use if statements, for loops, and while loops, which solve this problem."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
If statements
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If statements let you check if something is true, and do one thing or another depending on if it is true or false. For example, what if you wanted to know if a variable was larger than 5?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_variable = 6\n",
"\n",
"if my_variable > 5:\n",
" print(my_variable,\"is greater than 5\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may notice the syntax, which is the way Python expects code to be written, is a little hard to understand at first. Python uses colons to know when an if/for/while statement is occurring, and it includes all of the code which is tabbed (or 4 spaces) over."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# or you can check if a value is True or False and do something in either case\n",
"boolean = True # True and False are defined in Python, but they are only capitalized\n",
"\n",
"if boolean is False:\n",
" print(boolean)\n",
"else: # notice that else is not tabbed over, it is another statement\n",
" print(\"the boolean is not False\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also expand beyond just if and else, by using elif. elif comes after if but before else and only occurs if the if statements above it did not occur and the condition is also true. Here is an example."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_num = 5\n",
"\n",
"if my_num < 3:\n",
" print(\"My number is less than 3!\")\n",
"elif my_num < 4:\n",
" print(\"My number is less than 4!\")\n",
"elif my_num < 6:\n",
" print(\"My number is less than 6!\")\n",
"else:\n",
" print(\"My number is greater than or equal to 6!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
For loops
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now lets take a look at for loops. These allow you to take a group of things and use them one at a time, which is called iterating. For example here we can take a list of numbers and print them out one at a time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# example of a for loop\n",
"counting_up = [0, 1, 2]\n",
"\n",
"for index in counting_up: # the syntax is for *whatever_you_want_to call_your_variable* in *a_list_or_iterable*\n",
" print(index)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is one of the most powerful parts of python, because for loops can iterate through anything, not just numbers that count up or down."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"new_list = ['a', 'b', 'c', 'd']\n",
"\n",
"for letter in new_list:\n",
" print(letter)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# we can combine the two examples as well\n",
"# recall how indexing works with lists!\n",
"\n",
"for index in counting_up: # [0, 1, 2]\n",
" print(new_list[index]) # this prints the letter for each of the indexes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That is one of the most common ways to use for loops, since we generally want to get the index so that we can use it for multiple lists. We can also only use some indices very easily."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# or only show some values\n",
"two_indices = [0, 2]\n",
"\n",
"for i in two_indices:\n",
" print(new_list[i])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Combining if statements and for loops is a powerful method to write code one time and use it on all of your data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"even_nums = [0, 2, 4, 6, 8, 10]\n",
"\n",
"for number in even_nums:\n",
" if number > 5:\n",
" print(number)\n",
" else:\n",
" print(\"the number\", number, \"is below 5\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now what if you just wanted to do something 100 times, or 1,000,000 times? Python provides the functions range() and len() which help you solve situations that you don't want to explicitely state every value you are looping over. The range() function automatically makes a list which counts up the value you give it, but does not include the top value. This makes it a nice shorthand for writing for loops that execute a number of times."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"short_hand = range(5) # this is similar to [0, 1, 2, 3, 4]\n",
"print(short_hand) # note that it is actually a true iterator, so it doesnt solve for the values until you ask for them"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# to make a shorter for loop\n",
"for i in range(5):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find out how many values are in a list using len() function, which is the length of the list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(len(short_hand))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Combining this with the range function gives a very nice way to work with function and lists."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_list = ['a', 'b', 'c']\n",
"\n",
"for index in range(len(my_list)): # counts from 0 up to the length of the list\n",
" print(my_list[index])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
While loops
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What if you wanted your code to just keep running until something happened? This could be difficult to do with for loops, but while loops can solve the problem. These will keep running until the while condition is False."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test = 0\n",
"\n",
"while test < 7: # keeps running until test is greater than or equal to 7\n",
" print(test, \"is less than 7\")\n",
" test = test + 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"you could also use while loops to change all of the values up to a point, even if you don't know how many values that is."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"even_nums = [0, 2, 4, 6, 8, 10]\n",
"test = 0\n",
"\n",
"while even_nums[test] < 5:\n",
" even_nums[test] = even_nums[test] + 1 # make the first values add one until you find a value equal or greater than 5\n",
" test = test + 1 # dont forget to increase this or you get stuck in a loop forever!\n",
" \n",
"print(even_nums)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you get caught in a loop forever, go to Kernel in the top and click on Interrupt. This will throw an error so that the code stops running."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"At this point you nearly have the tools you need to understand the basics of python. The final topic we will cover will be defining your own functions. This will allow you to write your functions which can do whatever you want them to do, and reuse that function as much as you like."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Functions
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The basic syntax is shown below, which starts with the def keyword, and provides the name of the arguments inside of the brackets. Then when you call the function, you can see that it runs the code for the given input, which is called x in this function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# this function will double the input given to it and then print it.\n",
"def my_func(x):\n",
" print(2*x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# now that the function is defined, lets use it like we do the print function\n",
"my_func(5) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Functions can do a lot more than just printing values. Commonly they are used to calculate something and return it to be used later. Here is an example."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# this function returns 2 times the input\n",
"def doubler(x):\n",
" x = 2*x\n",
" return x # calling return means that instead of printing the value, the function will give the result back"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# now I can use this function with other variables.\n",
"my_var = 3\n",
"double_my_var = doubler(my_var)\n",
"print(double_my_var)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will see that this did not change the value of my_var, which is important to remember when you make functions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(double_my_var)\n",
"print(\"my_var is still\", my_var)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, we can always replace the variables if we want to as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# We can replace it by saving the new value\n",
"\n",
"my_var = doubler(my_var) # now it is 6\n",
"\n",
"my_var = doubler(my_var) # now it is 12\n",
"\n",
"print(my_var)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You saw that the variable my_var did not change even though we passed it into the function and changed it in there. This is called variable scope and is a big topic. To keep it simple we will just need to know that functions do not change variables outside of their function and any values you want to change, must be returned from the function. You can also make more complex functions, which call other functions or return multiple values."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# take two inputs and return two outputs\n",
"def complex_func(a, b):\n",
" y = doubler(a)\n",
" z = b**2 # this is how python does exponential, so this is b squared\n",
" return y,z\n",
"\n",
"my_var = 5\n",
"\n",
"# retrieve both output values into two variables\n",
"my_var2, my_var_squared = complex_func(my_var, my_var)\n",
"\n",
"print(my_var2, my_var_squared)\n",
"\n",
"# if we didnt need the values we can just execute it all in a print statement\n",
"print(complex_func(my_var, my_var))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# functions can also have no inputs\n",
"\n",
"def no_input_function():\n",
" print(\"This function took no inputs\")\n",
" \n",
"no_input_function()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" This wraps up the content for the first tutorial. Now you have some understanding of the syntax for Python, and how to use it to create variables and lists, write if statements, for and while loops, and make your own functions. In the next tutorial of mine we will dive right into data analysis tools, since those are useful no matter what profession you are in!
Each tutorial will also end with a short practice section to see if you understand what was done."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Practice
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try to complete the following blocks, the answers are available at the end of the notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# make a variable which starts at 3, adds one to it, then prints out 1/2 of this value\n",
"my_var = ??\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# make a function which takes two inputs and returns the two multiplied together\n",
"def func(x, y):\n",
" ??\n",
" \n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# write a for loop that runs three times and replaces var1 with the product of var1 and var2 each time. Then print var1\n",
"var1 = 6\n",
"var2 = 13\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# make a while loop which will divide var1 by 2 until it is less than 1/2 and then print out what var1 is\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# make a list with 5 values in it and use a for loop to add 1 to each value, then print the whole list at once.\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \n",
" \n",
" \n",
" \n",
" \n",
" You can delete this cell when you want to compare your answers, or move the answer cells up next to your to compare\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Answers
"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# make a variable which starts at 3 and adds one to it, then prints out 1/2 of this value\n",
"\n",
"my_var = 3\n",
"my_var = my_var + 1\n",
"print(my_var/2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# make a function which takes two inputs and returns the two multiplied together\n",
"\n",
"def func(x, y):\n",
" return x*y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# write a for loop that goes three times and replaces var1 with the product of var1 and var2 each time. Then print var1\n",
"\n",
"var1 = 6\n",
"var2 = 13\n",
"\n",
"for i in range(3):\n",
" var1 = func(var1, var2)\n",
"print(var1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# make a while loop which will divide var1 by 2 until it is less than 1/2 and then print out what var1 is\n",
"\n",
"while var1 > 0.5:\n",
" var1 = var1/2\n",
" \n",
"print(var1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# make a list with 5 values in it and use a for loop to add 1 to each value, then print the whole list at once.\n",
"\n",
"# any values are fine to start with\n",
"my_list = [1, 2, 3, 1, -17]\n",
"\n",
"for item in range(len(my_list)):\n",
" my_list[item] = my_list[item] + 1\n",
" \n",
"print(my_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}