#!/usr/bin/env python # coding: utf-8 # # Python for Data Science Workshop - Spring 2018 # May 17, 2018 # # Instructor: Fernando Rodriguez # # ## Session 4: Running Jupyter Notebooks, Iteration # #### Keyboard shortcuts for navigating Jupyter Notebooks # In[4]: from IPython.display import Image Image(filename='images/s4_jupytershortcuts.png') # In[5]: # Quick Review of Functions # creating a function that multiplies a variable with a single value by 50 x = 50 y = 10 def mult50(var): multSol = var * 50 print "the solution is:", multSol # In[8]: mult50(x) # #### Updating Variables # # Assignment statements may update themselves # # In[10]: x = 1 x = x + 1 print x # Before you can update a variable you have to initialize it (e.g., assign it) # In[13]: # trying to update without initializing variable y = y + 1 # #### Print vs. Return # # Print and Return are statements, not functions. # # Print shows the user a string representing the output. It can help us understand how a program works, and is helpul for debugging. # # Return gives us back a value and can be used to either store in a variable, or used as an argument which is passed into another function. # In[19]: def printingFun(): print "I printed" # In[20]: printingFun() # In[17]: def returnFun(): print "I printed again" return "I returned" # In[18]: returnFun() # In[21]: f1 = printingFun() f2 = returnFun() # Now let's see what is inside # In[22]: print f1 # In[23]: print f2 # #### While Statement # # The while statment repeatedly executes code as long as a given condition is true. # # A while statement is also called a loop. The while statement loops on itself until the condition you state is no longer true. # In[24]: # coutdown example n = 10 while n > 0: print n n = n - 1 print "blastoff" # In[25]: Image(filename='images/s4_whilestatement.png') # You have to be careful with while statements because they can go on until infinity if there is no condition to prevent it from stopping. # When something like this occurs you can interupt the kernell. In the top menu of Juptyer notebooks and select `kernel -> Interrupt kernel. # # or press i twice. # # ii # #### Break Statements # # If you know ahead of time that your loop may go on infinitely, you can add a break statement to jump out of the loop. # In[1]: n = 10 while n > 0: print n n = n + 1 if n > 100: break # ### For-Loops and Lists # We use a for-loop to go through a list of items or through the contents of a file. # # Constructing a loop involves: # # 1. Initializing one or more variables before the loop starts (this means you know ahead of time what variables you need in order to store the information you'll gather when you use the for-loop) # # 2. Peforming some computation or changing each item in the loop body # # 3. Looking at the resulting variables when the loop ends # # for and in are used together in for-loops. # # We will create a list, which is a sequence of numbers, characters, or a mix of both. # Our list will consist of numbers only. # # We can use lists to perform a for-loop, which will modify the character in some way. # # In[29]: count = 0 sampleList = [2, 3, 5, 10, 100] for eachnum in sampleList: count = count + eachnum print count # We can also add the list directly in the for-loop. We get the same results. # In[72]: count = 0 for eachnum in [2, 3, 5, 10, 100]: count = count + eachnum print count # ## Class Excercise # # 1. Create a variable with a value of 1000 # # 2. Make a for-loop that subtracts the value in x using the following list of numbers [100, 50, 25, 10] # # 3. Your for-loop should print each subtraction result # In[65]: x = 1000 sampleList = [2, 3, 5, 10, 100] for eachnum in sampleList: x = x - eachnum print x # #### Making this into a function # # Note that we need two arguments in the function: # # varList: the variable containing the list of numbers # # singleVar: contains a single value, which will get subtracted from the list of numbers in varList. # In[2]: y = [100, 50, 25, 10] x = 1000 def subfunc(varList, singleVar): for eachnum in varList: singleVar = singleVar - eachnum print singleVar # In[3]: subfunc(y, x)