from IPython.display import Image
Image(filename='images/s4_jupytershortcuts.png')
# 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
mult50(x)
the solution is: 2500
Assignment statements may update themselves
x = 1
x = x + 1
print x
2
Before you can update a variable you have to initialize it (e.g., assign it)
# trying to update without initializing variable
y = y + 1
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.
def printingFun():
print "I printed"
printingFun()
I printed
def returnFun():
print "I printed again"
return "I returned"
returnFun()
I printed again
'I returned'
f1 = printingFun()
f2 = returnFun()
I printed I printed again
Now let's see what is inside
print f1
None
print f2
I returned
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.
# coutdown example
n = 10
while n > 0:
print n
n = n - 1
print "blastoff"
10 9 8 7 6 5 4 3 2 1 blastoff
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
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.
n = 10
while n > 0:
print n
n = n + 1
if n > 100:
break
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
We use a for-loop to go through a list of items or through the contents of a file.
Constructing a loop involves:
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)
Peforming some computation or changing each item in the loop body
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.
count = 0
sampleList = [2, 3, 5, 10, 100]
for eachnum in sampleList:
count = count + eachnum
print count
2 5 10 20 120
We can also add the list directly in the for-loop. We get the same results.
count = 0
for eachnum in [2, 3, 5, 10, 100]:
count = count + eachnum
print count
2 5 10 20 120
Create a variable with a value of 1000
Make a for-loop that subtracts the value in x using the following list of numbers [100, 50, 25, 10]
Your for-loop should print each subtraction result
x = 1000
sampleList = [2, 3, 5, 10, 100]
for eachnum in sampleList:
x = x - eachnum
print x
998 995 990 980 880
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.
y = [100, 50, 25, 10]
x = 1000
def subfunc(varList, singleVar):
for eachnum in varList:
singleVar = singleVar - eachnum
print singleVar
subfunc(y, x)
900 850 825 815