A function, different to a mathematical one, is just a piece of code that only runs when it’s called and returns a value if desired. Modules We can just import our files from other files. Module...
# Raising forced exceptions x = 10 if x > 5: raise Exception('x should not exceed 5. The value of x was: {}'.format(x)) # assert import sys assert ('linux' in sys.platform), "This code runs...
Answers to all of these questions will be available at solutions 24 hours before the next lecture. 1 Make a function factorial(x) that will return x! (the factorial of x) >>> def factor...
Input Here we’ll make use of the input() function (gasps). Example 1 >>> input() boo 'boo' Here you might notice that input() acted like the echo command in bash. To be honest, this i...
1 We first need to figure out how to add letter from the start of a string to the end. For our first case let’s try taking the first letter of 'string' and sticking it to the end. Slice the strin...
Answers to all of these questions will be available at solutions 24 hours before the next lecture. 1 Notice anything peculiar about “ikbal” and “kbali?” When we take the first letter of ikbal and...
I have said we’d go into lists et al. in a later lecture and here we are. Iteration as its name is about iterating. You have an object with multiple (or just one) elements and you cycle over them (...
Simple part The rules of the game OK let’s first think about how we can go about doing this. We have two separate arguments so we need to turn them into writing one-by-one. We...
You should be able to do the easy part with your current knowledge while advanced will need you to do some research on your own (It’s related to next week’s subject). Answers to both of these quest...