Read through this on functions carefully. This is a very important concept in programming that will really save you a lot of time.
Skim through this on modules
Read the first answer on the modulo operator here and the syntax in python.
Here we will dive a bit more into loops and their power. Just a quick refersher on the syntax:
for x in range(0,101):
print(x) # will print all numbers from 0 -> 100 (including 100)
x = 0
while True: # will run forever
print(x)
x = x + 1
While loops have a similar syntax to if statements, but they will run again and again while the condition is true. So if we wanted to make a while loop work like a for loop it would be simple:
x = 0
while x < 101:
print(x)
x = x + 1
If we look at this, it will work exactly like the for loop above, the loop runs while x is less than 101, on each iteration x is incremented, just like the for loop, and it prints it. Try a couple out in the interpreter.
Just like if statements, loops can also be nested:
for x in range(1,11):
print(x)
for x in range(1,11):
for y in range(1,11):
print(str(x) + " * " + str(y) + " = " + str(x*y))
You don't even have to have static nested loops, you can make them a bit more dynamic:
for x in range(1,11):
for y in range(x,11): #start from x and go to 10 (inclusive)
print(str(x) + " * " + str(y) + " = " + str(x*y))
If you'd like to break out of a loop you would you the "break" keyword:
for x in range(1,10000000):
print(x)
if x == 10:
break
# or you can use it in a while loop
x = 10
while True:
print(x)
x = x + 1
if x > 25:
break
Try these out and play around with it to get a better feel.
The last thing we will do is iterable collections
if we have a list we can loop through it as well:
listOfItems = [1,2,3,4, "I like coding", "coding is fun", "yay"]
for item in listOfItems:
print(item)
As an exercise try to create a list of lists and iterate through every element and print them all.
Functions in programming allow you to write a piece of code once and use it many times. I'm sure you have seen functions before in your math classes, such as sin,cos,tan, ln, log, etc. Even if you havn't dont worry, the concept isn't hard.
Lets create a simple function, one that calculates the number that is bigger, given two numbers.
We would take two numbers in and will return to the user the larger number.
def largerNumber(firstNumber, secondNumber):
if firstNumber > secondNumber:
return firstNumber
elif secondNumber > firstNumber:
return secondNumber
else: #they are equal
return firstNumber #does not matter since they are equal
In python you use the "def" keyword to let the intrepreter know that you are "def"-ining a function, you then name it (we named it largerNumber), then you open the parenthesis to allow arguements for the functions, the rest we have seen before.
To use it, just call it like other functions we have seen:
print(largerNumber(5,10)) # will print 10
Let's rewrite some of our previous code in a function so that we can re-use it. Let's print the numbers from 1 through the users input:
def print_from_one_to_the_number(n):
for x in range(1,n+1):
print(x)
print_from_one_to_the_number(20)
print_from_one_to_the_number(30)
print_from_one_to_the_number(40)
Here we define a function that takes an argument that we use in our for loop, so that our loop is dynamic. Now every time we want to print out the numbers from 1 -> n, just use this one line call.
Now lets do something a bit more useful, lets start computing, lets sum the numbers from 1 -> 100
currentSum = 0
for x in range(1,101):
currentSum = currentSum + x # on each iteration x will change to the next number, we add it to the current sum
print(currentSum)
Now if we functionize it, we can sum the numbers from 1 -> n
def sum_from_one_to_n(n):
currentSum = 0
for x in range(1,n+1):
currentSum = currentSum + x
return currentSum
Now if we want to sum from a -> b:
def sum_from_a_to_b(a,b):
currentSum = 0
for x in range(a,b+1):
currentSum = currentSum + x
return currentSum
Now that was easy, we know have a function that will sum any amount of numbers, starting from anyplace.
As an exercise, try and write a function that sums all the squares (x*x) from a -> b
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
It will be useful to understand the modulo operator, if statements, and looping.
For this homework we will write a program for the caesar cipher.
In the cipher, you will increment each letter by fixed amount (with wrapping so that 'z' + 1 = 'a').
For example if the increment is 5 then 'abcde' becomes 'fghij', if the increment is 3 then 'wxyz' becomes 'zabc'
In this assignment you will print out all the possible values that our encripted value could be.
For example if our encripted code is 'a' then that code could have come from the entire alphabet.
'ab' could have come from 'ab','bc','cd','de', etc.
For a slightly harder problem, try implemt an algorithm that does not have to check every possibility.
For example, if our encripted code is 'z lzm hr z gtlzm' then since we are dealing with the english language, we know that there are only a limited amount of one letter words, decreasing our possibilities to only handful.