# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] papermill={"duration": 0.026469, "end_time": "2020-08-19T20:24:05.593760", "exception": false, "start_time": "2020-08-19T20:24:05.567291", "status": "completed"} tags=[]
# # <a id='PF'>Program Flow in Python</a>
#   <div>
#     <img src="https://image.flaticon.com/icons/svg/762/762620.svg" width="250"/>
#   </div>

# + [markdown] papermill={"duration": 0.021143, "end_time": "2020-08-19T20:24:05.637003", "exception": false, "start_time": "2020-08-19T20:24:05.615860", "status": "completed"} tags=[]
# ## <a id='main'>Table of Contents</a>
# - [What is a computer Program](#def)
# - [Functions](#F)
#     * [Why Functions?](#WF)
#     * [Syntax](#S)
# - [Conditional & Logic](#C&L)
# - [Iteration](#I)
#     * [Aside (Recursion)](#AR)
# - [Puting it all together](#PIAT)
# - [More about Functions](#MAF)
# - [Some topics we haven't discussed, but have used](#last)

# + [markdown] papermill={"duration": 0.021202, "end_time": "2020-08-19T20:24:05.679996", "exception": false, "start_time": "2020-08-19T20:24:05.658794", "status": "completed"} tags=[]
# # <a id='def'>What is a computer Program</a>
# [Go back to the main page](#main)
# <p>
#     At its simplest a program is a list of instructions that a computer carries out in order. A program could be long and complicated, but it is built of simple parts. Let's look at some simple operations in python, and think about what the computer does for each one.
#     </p>

# + papermill={"duration": 0.033942, "end_time": "2020-08-19T20:24:05.735685", "exception": false, "start_time": "2020-08-19T20:24:05.701743", "status": "completed"} tags=[]
1 + 1

# + papermill={"duration": 0.032474, "end_time": "2020-08-19T20:24:05.790313", "exception": false, "start_time": "2020-08-19T20:24:05.757839", "status": "completed"} tags=[]
2 * 4.4

# + papermill={"duration": 0.032613, "end_time": "2020-08-19T20:24:05.844923", "exception": false, "start_time": "2020-08-19T20:24:05.812310", "status": "completed"} tags=[]
1 + 1
2 * 4.4

# + [markdown] papermill={"duration": 0.021982, "end_time": "2020-08-19T20:24:05.889853", "exception": false, "start_time": "2020-08-19T20:24:05.867871", "status": "completed"} tags=[]
# In the first cell we compute `1 + 1`, and Python returns the results `2`. We can think of this as a very sort program. Similarly, in the second cell we compute `2 * 4.4`, and Python returns the results `8.8`.<br>
#
# However, In the third cell, when we combine these to statements as squential lines, we only see Python return `8.8`.<br>
#
# This is because, Python can only return one result at the end of the cell, so the first line is evaluated, but we never se the result. One way can report intermdiate results is using `print`.

# + papermill={"duration": 0.033214, "end_time": "2020-08-19T20:24:05.945561", "exception": false, "start_time": "2020-08-19T20:24:05.912347", "status": "completed"} tags=[]
print(1 + 1)
print(2 * 4.4)

# + [markdown] papermill={"duration": 0.022023, "end_time": "2020-08-19T20:24:05.990046", "exception": false, "start_time": "2020-08-19T20:24:05.968023", "status": "completed"} tags=[]
# we can also includes **comments** in our lines of code which computer will not execute.These lines can be used to add notes and explanations for the code. We use `#` to start a comment in our code. 

# + papermill={"duration": 0.032471, "end_time": "2020-08-19T20:24:06.045002", "exception": false, "start_time": "2020-08-19T20:24:06.012531", "status": "completed"} tags=[]
# this is a comment and it will not be executed
# print 1 + 1
print(f"1 + 1 is {1+1}")
# print 2 * 4.4
print(f"2 * 4.4 is {2*4.4}")
# print 'completed'
print('completed')

# + [markdown] papermill={"duration": 0.022604, "end_time": "2020-08-19T20:24:06.090217", "exception": false, "start_time": "2020-08-19T20:24:06.067613", "status": "completed"} tags=[]
# While writing a code often we not only want to print intermediate results, but *store* them for future use in the code. We can store a result in computer's memory by assigning it to a **variable**.

# + papermill={"duration": 0.032938, "end_time": "2020-08-19T20:24:06.146074", "exception": false, "start_time": "2020-08-19T20:24:06.113136", "status": "completed"} tags=[]
first = 1 + 1
final = first * 4.4

print(final)

# + [markdown] papermill={"duration": 0.023289, "end_time": "2020-08-19T20:24:06.193361", "exception": false, "start_time": "2020-08-19T20:24:06.170072", "status": "completed"} tags=[]
# Here we are able to use the result of the first calculation (stored in `first`) in our second calculation. We store second calculation in `final`, which ww print in third expression at the end of the cell.<br>
#
# Variables help us to keep track of the information we need to successfully excute a program. Variables can be used to store a variety of information.

# + papermill={"duration": 0.034565, "end_time": "2020-08-19T20:24:06.250921", "exception": false, "start_time": "2020-08-19T20:24:06.216356", "status": "completed"} tags=[]
my_name = 'Vishwas'
my_age = 22
my_favourite_number = 34
has_pet = False

print(f"My name is {my_name}")
print(f"My age is {my_age}")
print(f"My favourite number is {my_favourite_number}")
print(f"Do I have a Pet: {has_pet}")

# + [markdown] papermill={"duration": 0.022648, "end_time": "2020-08-19T20:24:06.296920", "exception": false, "start_time": "2020-08-19T20:24:06.274272", "status": "completed"} tags=[]
# Since variables are used to store information, it's good idea to give them discriptive names. This increase code readibility, which helps in fixing mistakes, or colloboration ith others

# + papermill={"duration": 0.042126, "end_time": "2020-08-19T20:24:06.362408", "exception": false, "start_time": "2020-08-19T20:24:06.320282", "status": "completed"} tags=[]
print(type(my_name))
print(type(my_age))
print(type(my_favourite_number))
print(type(has_pet))


# + [markdown] papermill={"duration": 0.023087, "end_time": "2020-08-19T20:24:06.409086", "exception": false, "start_time": "2020-08-19T20:24:06.385999", "status": "completed"} tags=[]
# * A **String** is a sequence of characters.
# * An **integer** is same as mathematics (i.e. "negative & positive whole numbers"
# * A **float** or **floating point number** are the decimal numbers (i.e. "real numbers" in mathematics). It is called float because the decimal point is alloed to "float" through the digits, allowing us to rpresent both big numbers (eg. 112334.112) and small numbers (eg. 0.000045).
# * A **bool** or **boolean** refers to a variable that is either True or False.
#
# These are some of the few data we will encounter whiach are mainly used during programing.
#
# In Python, we can assign any type of data to a variable without declaring what type the variable will be in advance. 

# + [markdown] papermill={"duration": 0.022645, "end_time": "2020-08-19T20:24:06.455138", "exception": false, "start_time": "2020-08-19T20:24:06.432493", "status": "completed"} tags=[]
# # <a id='F'>Functions</a>
# [Go back to the main page](#main)

# + [markdown] papermill={"duration": 0.022815, "end_time": "2020-08-19T20:24:06.501090", "exception": false, "start_time": "2020-08-19T20:24:06.478275", "status": "completed"} tags=[]
#
# Here is a mathematical function defined by 
# $$ f(x) = 3x - 5 $$
# we give function an input and gets an output
# $$ f(6)-> 13 $$
# if we have a variable 
# $$ y = 3 $$
# and we evaluate function on y we get same result 
# $$ f(y)-> 4 $$

# + [markdown] papermill={"duration": 0.022942, "end_time": "2020-08-19T20:24:06.547231", "exception": false, "start_time": "2020-08-19T20:24:06.524289", "status": "completed"} tags=[]
# Many programs react to user input. Function allows us to dfine a task a computer will carry out based on a given input. A simply Pyhton function look like this:

# + papermill={"duration": 0.03246, "end_time": "2020-08-19T20:24:06.605803", "exception": false, "start_time": "2020-08-19T20:24:06.573343", "status": "completed"} tags=[]
def cube(number):
    return number**3


# + [markdown] papermill={"duration": 0.022928, "end_time": "2020-08-19T20:24:06.652424", "exception": false, "start_time": "2020-08-19T20:24:06.629496", "status": "completed"} tags=[]
# We define function using the `def`. Then comes the name of the function, hich in this case is `cube`. we then enclose the function input in paranthesis, in this case `number`. We use `:` to indicate the Python the starting of the body of the function
#
# In this case th body of the function is vary simple, we return the cube of `number` (we use `**` for exponents in Python). The keyword `return` signals that the function will generate some output. Not every function will have a `return` statement. A `return` statement ends a function

# + papermill={"duration": 0.033387, "end_time": "2020-08-19T20:24:06.709450", "exception": false, "start_time": "2020-08-19T20:24:06.676063", "status": "completed"} tags=[]
# we can store the function output in variables
cube_6 = cube(6)

print(cube_6)

my_input = 3.4
# we can also use variables as function input
print(cube(my_input))

# + [markdown] papermill={"duration": 0.023006, "end_time": "2020-08-19T20:24:06.755715", "exception": false, "start_time": "2020-08-19T20:24:06.732709", "status": "completed"} tags=[]
# We can pass different input to the `cube` function, including variables. When we passed a float to `cube`, float is returned. When we passed an integer, `cube` returned integer. In both cases the input was interpretd by the function as the  argument `number`
#
# Not all possible inputs are valid.

# + papermill={"duration": 0.069757, "end_time": "2020-08-19T20:24:06.849083", "exception": false, "start_time": "2020-08-19T20:24:06.779326", "status": "completed"} tags=[]
print(cube('apple'))

# + [markdown] papermill={"duration": 0.02336, "end_time": "2020-08-19T20:24:06.896435", "exception": false, "start_time": "2020-08-19T20:24:06.873075", "status": "completed"} tags=[]
# We ran into an error because `apple` is a string, not a number. This is because exponent doesn't work on `string`. W should be careful t omake sure that the input for a function makes sense for the function's purpose

# + [markdown] papermill={"duration": 0.023131, "end_time": "2020-08-19T20:24:06.945659", "exception": false, "start_time": "2020-08-19T20:24:06.922528", "status": "completed"} tags=[]
# We can test the function using assertion weather the oupput is equal to the given value

# + papermill={"duration": 0.043852, "end_time": "2020-08-19T20:24:07.013005", "exception": false, "start_time": "2020-08-19T20:24:06.969153", "status": "completed"} tags=[]
assert cube(3) == 23

# + [markdown] papermill={"duration": 0.023167, "end_time": "2020-08-19T20:24:07.060262", "exception": false, "start_time": "2020-08-19T20:24:07.037095", "status": "completed"} tags=[]
# ## <a id='WF'>Why Functions?</a>
# [Go back to the main page](#main)

# + [markdown] papermill={"duration": 0.02434, "end_time": "2020-08-19T20:24:07.109099", "exception": false, "start_time": "2020-08-19T20:24:07.084759", "status": "completed"} tags=[]
# On other use of function apart from handling the user input is performing an action multiple times on different input. If we want to perform square a bunch of numbers, in particular the number between 1 to 9, I can do this pretty easily (later we will learn about iteration which will make this even easier!)

# + papermill={"duration": 0.036885, "end_time": "2020-08-19T20:24:07.169779", "exception": false, "start_time": "2020-08-19T20:24:07.132894", "status": "completed"} tags=[]
1**2
2**2
3**2
4**2
5**2
6**2
7**2
8**2
9**2

# + papermill={"duration": 0.035947, "end_time": "2020-08-19T20:24:07.229543", "exception": false, "start_time": "2020-08-19T20:24:07.193596", "status": "completed"} tags=[]
print(1**2)
print(2**2)
print(3**2)
print(4**2)
print(5**2)
print(6**2)
print(7**2)
print(8**2)
print(9**2)


# + [markdown] papermill={"duration": 0.02367, "end_time": "2020-08-19T20:24:07.277747", "exception": false, "start_time": "2020-08-19T20:24:07.254077", "status": "completed"} tags=[]
# What if we want to go back and add `3` to all the results? Clearly we can see the problem here. Lets instead define a function to do our work

# + papermill={"duration": 0.032701, "end_time": "2020-08-19T20:24:07.334322", "exception": false, "start_time": "2020-08-19T20:24:07.301621", "status": "completed"} tags=[]
def my_func(x):
    print(x**2 + 3)


# + [markdown] papermill={"duration": 0.02386, "end_time": "2020-08-19T20:24:07.383888", "exception": false, "start_time": "2020-08-19T20:24:07.360028", "status": "completed"} tags=[]
# Now we can just call the function on every element. If  want to change the output, we just need to change the function

# + papermill={"duration": 0.08653, "end_time": "2020-08-19T20:24:07.494800", "exception": false, "start_time": "2020-08-19T20:24:07.408270", "status": "completed"} tags=[]
my_func(1)
my_func(2)
my_func(3)
my_func(4)
my_func(5)
my_func(6)
my_func(7)
my_func(8)
my_func(9)


# + [markdown] papermill={"duration": 0.023876, "end_time": "2020-08-19T20:24:07.545862", "exception": false, "start_time": "2020-08-19T20:24:07.521986", "status": "completed"} tags=[]
# Splitting out the work into function is often a way to make code more modular and understandable. It also helps ensure your cod is correct. If write a function and test it correct, we kno it will be correct every time we use it. if we don't break out code into a function, it is very easy to make typos or other errors which will cause our progrmas to break 

# + [markdown] papermill={"duration": 0.023969, "end_time": "2020-08-19T20:24:07.594096", "exception": false, "start_time": "2020-08-19T20:24:07.570127", "status": "completed"} tags=[]
# ## <a id='S'>Syntax</a>
# [Go back to the main page](#main)

# + [markdown] papermill={"duration": 0.026088, "end_time": "2020-08-19T20:24:07.644513", "exception": false, "start_time": "2020-08-19T20:24:07.618425", "status": "completed"} tags=[]
# As our instructions to the computer become more complicated, we will need to organize them in a way the computer understands. We've already seen an example of `cube` function. There is a specific order to the words and specific symbols we had to use to let Python know which part of the function we the defination and hich part is the body, or which part is the name of the function and which part is th argument. We call the rules for organizing code the programming language's **Syntax**
#
# Python' syntax is very streamlined so that code is readable and intutive. Python uses White space to organize code. For Example:

# + papermill={"duration": 0.036516, "end_time": "2020-08-19T20:24:07.706834", "exception": false, "start_time": "2020-08-19T20:24:07.670318", "status": "completed"} tags=[]
def ex_1():
    print('ex_1 is running')
    print('returning value "a"')
    return 'a'

ex_1()


# + papermill={"duration": 0.034837, "end_time": "2020-08-19T20:24:07.766558", "exception": false, "start_time": "2020-08-19T20:24:07.731721", "status": "completed"} tags=[]
def ex_2():
    print('ex_2 is running')
    print('exiting without returing a value')
    
ex_2()


# + [markdown] papermill={"duration": 0.024294, "end_time": "2020-08-19T20:24:07.815616", "exception": false, "start_time": "2020-08-19T20:24:07.791322", "status": "completed"} tags=[]
# The function `ex_1` ends with a return statement, but `ex_2` has no return statement. So how does Python know where `ex_2` ends? We use indentation to indicate which lines are part of th function and which aren't. The indented lines are all grouped together undr the function defination.

# + [markdown] papermill={"duration": 0.024481, "end_time": "2020-08-19T20:24:07.865007", "exception": false, "start_time": "2020-08-19T20:24:07.840526", "status": "completed"} tags=[]
# # <a id='C&L'>Conditional & Logic</a>
# [Go back to the main page](#main)

# + [markdown] papermill={"duration": 0.024206, "end_time": "2020-08-19T20:24:07.913860", "exception": false, "start_time": "2020-08-19T20:24:07.889654", "status": "completed"} tags=[]
# We'll often want the computer only to take an action under certain situations. For Example, we might want a game to print the message 'High Score!', but only if the player's high score is higher than the previous high score. We can write this as a formal logical statement: if the player's score is higher than the previous high scor thn print 'High Score!'
#
# The syntax for exprssing this logic in python is vry similar. Let's define a function that accepts the player's score and the previous high score as arguments. If the player's score is higher, then it will print 'High score!'. Finally, it will return the new high score (whichever one that is).

# + papermill={"duration": 0.034061, "end_time": "2020-08-19T20:24:07.972976", "exception": false, "start_time": "2020-08-19T20:24:07.938915", "status": "completed"} tags=[]
def test_high_score(player_score, high_score):
    if player_score > high_score:
        print('High score!')
        high_score = player_score
        
    return high_score


# + papermill={"duration": 0.036696, "end_time": "2020-08-19T20:24:08.034780", "exception": false, "start_time": "2020-08-19T20:24:07.998084", "status": "completed"} tags=[]
print(test_high_score(83, 98))

# + papermill={"duration": 0.036748, "end_time": "2020-08-19T20:24:08.097649", "exception": false, "start_time": "2020-08-19T20:24:08.060901", "status": "completed"} tags=[]
print(test_high_score(95, 93))


# + [markdown] papermill={"duration": 0.024286, "end_time": "2020-08-19T20:24:08.147065", "exception": false, "start_time": "2020-08-19T20:24:08.122779", "status": "completed"} tags=[]
# With if statements we use a similar syntax as we used for organizing functions. With functions we had a `def` statement ending with `:`, and an indented body. Similarly for a conditional, we have an if statement ending with `:`, and an indented body.
#
# Conditional statements are used to control program flow. We can visualize our example, `test_high_score`, in a decision tree
#
# ![programflow_highscore.png](https://raw.githubusercontent.com/VishwasSaini32/Data-Science/master/Python/images/programflow_highscore.png)
#
# We can nest `if` statements to make more complicatd trees

# + papermill={"duration": 0.036854, "end_time": "2020-08-19T20:24:08.208728", "exception": false, "start_time": "2020-08-19T20:24:08.171874", "status": "completed"} tags=[]
def nested_example(x):
    if x < 50:
        if x % 2 == 0:
            return 'branch_a'
        else:
            return 'branch_b'
    else:
        return 'branch_c'
    
print(nested_example(42))
print(nested_example(51))
print(nested_example(37))

# + [markdown] papermill={"duration": 0.025145, "end_time": "2020-08-19T20:24:08.259034", "exception": false, "start_time": "2020-08-19T20:24:08.233889", "status": "completed"} tags=[]
# In this example, we have an `if` statement nested under another `if` statement. As we change the input, we end up on different branches of the tree.
#
# ![programflow_nestedlogic.png](https://raw.githubusercontent.com/VishwasSaini32/Data-Science/master/Python/images/programflow_nestedlogic.png)
#
# The statement that follows the `if` is called the **condition**. Th condition can be either true or false. If the condition is true, then we execute the statements under the `if`.If the condition is false, then we execute the statements under the `else` (or if there is no `else`, then we do nothing).
#
# Conditions themselves are instructions that Python can interpret.

# + papermill={"duration": 0.035799, "end_time": "2020-08-19T20:24:08.319788", "exception": false, "start_time": "2020-08-19T20:24:08.283989", "status": "completed"} tags=[]
print(50 > 10)
print(2 + 2 == 4)
print(-3 > 2)

# + [markdown] papermill={"duration": 0.032345, "end_time": "2020-08-19T20:24:08.381481", "exception": false, "start_time": "2020-08-19T20:24:08.349136", "status": "completed"} tags=[]
# Conditions are evaluated as booleans, which are `True` or `False`. We can combine conditions by asking of condition A _and_ condition B are true. We could also ask if condition A *or* condition B are true. Let's consider whether such statements are true overall based on the possible values of condition A and condition B.
#
# |Condition A|Condition B|Condition A and Condition B|Condition A or Condition B|
# |:---------:|:---------:|:-------------------------:|:------------------------:|
# |True|True|True|True|
# |True|False|False|True|
# |False|True|False|True|
# |False|False|False|False|

# + papermill={"duration": 0.039522, "end_time": "2020-08-19T20:24:08.447009", "exception": false, "start_time": "2020-08-19T20:24:08.407487", "status": "completed"} tags=[]
print(True and True)
print(True and False)
print(False and True)
print(False and False)

# + papermill={"duration": 0.040305, "end_time": "2020-08-19T20:24:08.516982", "exception": false, "start_time": "2020-08-19T20:24:08.476677", "status": "completed"} tags=[]
print(True or True)
print(True or False)
print(False or True)
print(False or False)

# + papermill={"duration": 0.038375, "end_time": "2020-08-19T20:24:08.586000", "exception": false, "start_time": "2020-08-19T20:24:08.547625", "status": "completed"} tags=[]
x = 5
y = 3

print(x > 4 and y > 2)
print(x > 7 and y > 2)
print(x > 7 or y > 2)

# + [markdown] papermill={"duration": 0.025639, "end_time": "2020-08-19T20:24:08.644132", "exception": false, "start_time": "2020-08-19T20:24:08.618493", "status": "completed"} tags=[]
# The keywords `or` and `and` are called **logical operations** (in the same sense that we call `+`, `-`, `*`, etc. arithmetic operations). The last logical operation is `not`: `not True` is `False`, `not False` is `True`

# + papermill={"duration": 0.036574, "end_time": "2020-08-19T20:24:08.707239", "exception": false, "start_time": "2020-08-19T20:24:08.670665", "status": "completed"} tags=[]
print(not True)
print(not False)

# + papermill={"duration": 0.040688, "end_time": "2020-08-19T20:24:08.773821", "exception": false, "start_time": "2020-08-19T20:24:08.733133", "status": "completed"} tags=[]
x = 10
y = 8

print(x > 7 or y < 7)
print(not x > 7 or y < 7)
print(not x > 7 or not y < 7)
print(not (x > 7 or y < 7))

# + [markdown] papermill={"duration": 0.025829, "end_time": "2020-08-19T20:24:08.825826", "exception": false, "start_time": "2020-08-19T20:24:08.799997", "status": "completed"} tags=[]
# # <a id='I'>Iteration</a>
# [Go back to the main page](#main)

# + [markdown] papermill={"duration": 0.027227, "end_time": "2020-08-19T20:24:08.878961", "exception": false, "start_time": "2020-08-19T20:24:08.851734", "status": "completed"} tags=[]
# Conditions are very useful because theey allows our programs to make dcisions based on some information. These decision control the flow of the program (i.e. which statments get executed). We have on other major tool for controlling program flow, which is repetition. In programming, we will use repetitive loops to execute the same code many times. This is called `iteration`. The most basic kind of interation is the `while` loop. A `while` loop will keep executing so long as the condition after the `while` is `True` 

# + papermill={"duration": 0.036468, "end_time": "2020-08-19T20:24:08.941362", "exception": false, "start_time": "2020-08-19T20:24:08.904894", "status": "completed"} tags=[]
x = 0
while x < 5:
    print(x)
    x = x + 1


# + [markdown] papermill={"duration": 0.028852, "end_time": "2020-08-19T20:24:09.001403", "exception": false, "start_time": "2020-08-19T20:24:08.972551", "status": "completed"} tags=[]
# We will often use interation to prform a task a certain numbers of times, but we might also use it to carry out a process to a certain stage of completition.
#
# As an example of these different cases, we'll considr the Fibonacci sequence.The Fibonacci numbers are the sequence of numbers $\{F_n\}_{n=1}^\infty$ defined by the linear recurrence equation
#
# $$ \begin{equation}
# F_n=F_{n-1}+F_{n-2}
# \end{equation}$$
#
# with $F_1=F_2=1$. As a result of the above definition, it is conventional to define $F_0=0$.
#
# The Fibonacci numbers for $n=1, 2, ...$ are $1, 1, 2, 3, 5, 8, 13, 21, ...$ (OEIS [A000045](https://oeis.org/A000045)).
#
# ![Fibonacci numbers](https://mathworld.wolfram.com/images/gifs/FoxTrot20051011.jpg)
#
# The above cartoon (Amend 2005) shows an unconventional sports application of the Fibonacci numbers (left two panels). (The right panel instead applies the [Perrin sequence](https://mathworld.wolfram.com/PerrinSequence.html)).
#
# Below we define two functions to compute part of the Fibonacci sequence; the first function computes the first `n` terms, while the second function computes all the terms less than an upper limit, `x`.

# + papermill={"duration": 0.040606, "end_time": "2020-08-19T20:24:09.069932", "exception": false, "start_time": "2020-08-19T20:24:09.029326", "status": "completed"} tags=[]
def first_n_fibonacci(n):
    prev_num = 0
    curr_num = 1
    count = 2
    
    print(prev_num)
    print(curr_num)
    
    while count <= n:
        next_num = curr_num + prev_num
        print(next_num)
        prev_num = curr_num
        curr_num = next_num
        count += 1

def below_x_fibonacci(x):
    prev_num = 0
    curr_num = 1
    
    if curr_num < x:
        print(prev_num)
        print(curr_num)
    elif prev_num < x:
        print(prev_num)
        
    while curr_num + prev_num < x:
        next_num = curr_num + prev_num
        print(next_num)
        prev_num = curr_num
        curr_num = next_num


# + papermill={"duration": 0.035489, "end_time": "2020-08-19T20:24:09.132747", "exception": false, "start_time": "2020-08-19T20:24:09.097258", "status": "completed"} tags=[]
m = 7
print(f'First {m} Fibonacci numbers')
first_n_fibonacci(m)

# + papermill={"duration": 0.036101, "end_time": "2020-08-19T20:24:09.195077", "exception": false, "start_time": "2020-08-19T20:24:09.158976", "status": "completed"} tags=[]
y = 40
print(f'Fibonacci numbers below {y}')
below_x_fibonacci(y)

# + [markdown] papermill={"duration": 0.025974, "end_time": "2020-08-19T20:24:09.247904", "exception": false, "start_time": "2020-08-19T20:24:09.221930", "status": "completed"} tags=[]
# Sometimes we will want our program to take a repeated action, but we won't know how many repetitions we will have to do, or it might be difficult to know ahead of time when the program should stop. For example, we could write a program that prints out cooking instructions. We don't know in advance how many instructions there will be in the recipe (some meals take a long time to cook and have many steps, while others are short and simple to make). We also don't know what the last instruction might be, so it would be difficult to write a conditional telling the program when to stop. How are we going to solve the problem? Let's look at an example.
#
# Instructions for making bread:  
# 1) Dissolve salt in water  
# 2) Mix yeast into water  
# 3) Mix water with flour to form dough  
# 4) Knead dough  
# 5) Let dough rise  
# 6) Shape dough  
# 7) Bake  
#
# The recipe has an ordered `list` of instructions. In Python we can use a list of strings to represent the instructions.

# + papermill={"duration": 0.035609, "end_time": "2020-08-19T20:24:09.309956", "exception": false, "start_time": "2020-08-19T20:24:09.274347", "status": "completed"} tags=[]
bread_recipe = ['Dissolve salt in water', 'Mix yeast into water', 'Mix water with flour to form dough', 
                'Knead dough', 'Let dough rise', 'Shape dough', 'Bake']

# + [markdown] papermill={"duration": 0.027662, "end_time": "2020-08-19T20:24:09.364134", "exception": false, "start_time": "2020-08-19T20:24:09.336472", "status": "completed"} tags=[]
# We could store different recipes in different lists.

# + papermill={"duration": 0.040152, "end_time": "2020-08-19T20:24:09.438190", "exception": false, "start_time": "2020-08-19T20:24:09.398038", "status": "completed"} tags=[]
soup_recipe = ['Dissolve salt in water', 'Boil  water', 'Add bones to boiling water', 'Chop onions', 
               'Chop garlic', 'Chop carrot', 'Chop celery', 'Remove bones from water', 
               'Add vegetables to boiling water', 'Add meat to boiling water']

beans_recipe = ['Soak beans in water', 'Dissolve salt in water', 'Heat water and beans to boil', 
                'Drain beans when done cooking']


# + [markdown] papermill={"duration": 0.029869, "end_time": "2020-08-19T20:24:09.500200", "exception": false, "start_time": "2020-08-19T20:24:09.470331", "status": "completed"} tags=[]
# Each of these lists has different instructions, and they are not all the same length. The beans recipe has four stps while the soup rcipe has ten. It ould b hard to write a `while` loop to print out each step. It is much easier to do it using a `for` loop.
#
# A `for` loop does an action for each item in a `list` (or more precisely, in an **iterable**)

# + papermill={"duration": 0.036407, "end_time": "2020-08-19T20:24:09.566232", "exception": false, "start_time": "2020-08-19T20:24:09.529825", "status": "completed"} tags=[]
def print_recipe(instructions):
    count = 1
    for step in instructions:
        print(f'{count}. {step}')
        count += 1


# + papermill={"duration": 0.036518, "end_time": "2020-08-19T20:24:09.629376", "exception": false, "start_time": "2020-08-19T20:24:09.592858", "status": "completed"} tags=[]
print_recipe(soup_recipe)

# + papermill={"duration": 0.036688, "end_time": "2020-08-19T20:24:09.692653", "exception": false, "start_time": "2020-08-19T20:24:09.655965", "status": "completed"} tags=[]
print_recipe(bread_recipe)

# + papermill={"duration": 0.036286, "end_time": "2020-08-19T20:24:09.756542", "exception": false, "start_time": "2020-08-19T20:24:09.720256", "status": "completed"} tags=[]
print_recipe(beans_recipe)


# + [markdown] papermill={"duration": 0.026649, "end_time": "2020-08-19T20:24:09.810455", "exception": false, "start_time": "2020-08-19T20:24:09.783806", "status": "completed"} tags=[]
# We can also use a `for` loop to repeat a task a certain number of times, like printing out the first `n` numbers in the Fibonacci sequence. Compare these two Fibonacci functions

# + papermill={"duration": 0.04283, "end_time": "2020-08-19T20:24:09.880247", "exception": false, "start_time": "2020-08-19T20:24:09.837417", "status": "completed"} tags=[]
def first_n_fibonacci_while(n):
    prev_num = 0
    curr_num = 1
    count = 2
    
    print(prev_num)
    print(curr_num)
    
    while count <= n:
        next_num = curr_num + prev_num
        print(next_num)
        prev_num = curr_num
        curr_num = next_num
        count += 1

def first_n_fibonacci_for(n):
    prev_num = 0
    curr_num = 1
        
    print(prev_num)
    print(curr_num)
    
    for count in range(2, n+1):
        next_num = curr_num + prev_num
        print(next_num)
        prev_num = curr_num
        curr_num = next_num


# + papermill={"duration": 0.037681, "end_time": "2020-08-19T20:24:09.944736", "exception": false, "start_time": "2020-08-19T20:24:09.907055", "status": "completed"} tags=[]
first_n_fibonacci_while(7)

# + papermill={"duration": 0.037675, "end_time": "2020-08-19T20:24:10.009509", "exception": false, "start_time": "2020-08-19T20:24:09.971834", "status": "completed"} tags=[]
first_n_fibonacci_for(7)


# + [markdown] papermill={"duration": 0.027435, "end_time": "2020-08-19T20:24:10.064888", "exception": false, "start_time": "2020-08-19T20:24:10.037453", "status": "completed"} tags=[]
# ## <a id='AR'>Aside (Recursion)</a>
# [Go back to the main page](#main)
#
# Another way to get something like interation is called *recursion*
# which is when ew define a function in terms of itself. Lets write the Fibonacci sequence recursively. This will be slightly different that it will only calculate the nth Fibonacci number

# + papermill={"duration": 0.045412, "end_time": "2020-08-19T20:24:10.138325", "exception": false, "start_time": "2020-08-19T20:24:10.092913", "status": "completed"} tags=[]
def fibonacci_recursive(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)


# + papermill={"duration": 0.038242, "end_time": "2020-08-19T20:24:10.204100", "exception": false, "start_time": "2020-08-19T20:24:10.165858", "status": "completed"} tags=[]
fibonacci_recursive(4)


# + [markdown] papermill={"duration": 0.027858, "end_time": "2020-08-19T20:24:10.259797", "exception": false, "start_time": "2020-08-19T20:24:10.231939", "status": "completed"} tags=[]
# Here we make use of the fact that a Fibonacci number $F_n$ can be defined in terms of $F_{N-1}$ and $F_{N-2}$ with some base cases $F_0=0$ and $F_1=1$.  We will not be using recursion in this course, but it is an interesting and useful programming construct.

# + [markdown] papermill={"duration": 0.026896, "end_time": "2020-08-19T20:24:10.314091", "exception": false, "start_time": "2020-08-19T20:24:10.287195", "status": "completed"} tags=[]
# # <a id='PIAT'>Puting it all together</a>
# [Go back to the main page](#main)
#
# We've learned two of the major components of programs: **variable** and **functions**. We've also learned two of the major components of program control: **conditionals** (`if` statements) and **itrations** (`for` and `while` loops). We can use thse ideas and tools to write code to prform complex tasks. Let's look at an example, involving all of these ideas put together.
#
# Below we write a function that points out all the prime numbers up to some number `n`. We will use iteration to check if each number is prime. We will use a conditional to print out numbers only if they are prime. We will also break up the task ito small pieces so our code is easy to read and understandable. This means we will use (or call) helper functions inside of our solution.

# + papermill={"duration": 0.048521, "end_time": "2020-08-19T20:24:10.390282", "exception": false, "start_time": "2020-08-19T20:24:10.341761", "status": "completed"} tags=[]
def is_prime(number):
    if number <= 1:
        return False
    
    for factor in range(2, number):
        if number % factor == 0:
            return False
    
    return True

def print_prime(n):
    for number in range(1, n):
        if is_prime(number):
            print(f'{number} is prime')


# + papermill={"duration": 0.041441, "end_time": "2020-08-19T20:24:10.462761", "exception": false, "start_time": "2020-08-19T20:24:10.421320", "status": "completed"} tags=[]
print_prime(10)

# + [markdown] papermill={"duration": 0.027291, "end_time": "2020-08-19T20:24:10.518143", "exception": false, "start_time": "2020-08-19T20:24:10.490852", "status": "completed"} tags=[]
# The other application of the functions might be to do something many times (not necessarily in an iteration). One specific and natural way to understand this is to have a list elements and apply a function of the `list`. Lets take a list of first 20 numbers and find which one are prime. We will do this and save the result in a `list`. Lists have an `append` method which allows us to add an element to the end of the list.

# + papermill={"duration": 0.041438, "end_time": "2020-08-19T20:24:10.587321", "exception": false, "start_time": "2020-08-19T20:24:10.545883", "status": "completed"} tags=[]
list_of_numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
prime_list = []
for number in list_of_numbers:
    if is_prime(number):
        prime_list.append(number)
prime_list

# + [markdown] papermill={"duration": 0.046334, "end_time": "2020-08-19T20:24:10.661487", "exception": false, "start_time": "2020-08-19T20:24:10.615153", "status": "completed"} tags=[]
# Python provides a nice construct to apply a function to every element of a list, called a `list comprehension`, here is an example of one:

# + papermill={"duration": 0.038221, "end_time": "2020-08-19T20:24:10.727959", "exception": false, "start_time": "2020-08-19T20:24:10.689738", "status": "completed"} tags=[]
[number for number in list_of_numbers if is_prime(number)]


# + [markdown] papermill={"duration": 0.027839, "end_time": "2020-08-19T20:24:10.784182", "exception": false, "start_time": "2020-08-19T20:24:10.756343", "status": "completed"} tags=[]
# Note that this is a simple bit of code that is very undrstandable. We don't ned to care **how** the `is_prime` computation os occuring, only that its occuring for every element of `list_of_numbers`. This means that we can view our program at a high level without caring about the small details (which hopefully we have already designed well and tested)

# + [markdown] papermill={"duration": 0.028006, "end_time": "2020-08-19T20:24:10.840622", "exception": false, "start_time": "2020-08-19T20:24:10.812616", "status": "completed"} tags=[]
# # <a id='MAF'>More about Functions</a>
# [Go back to the main page](#main)
#
# Notice that the `ex_1` and `ex_2` has no input, but other functions like `test_high_score` had multiple variables as input. Remember that a function argument is just a placeholder for a name and will b bound to hatever is passed into the function. For Example

# + papermill={"duration": 0.037334, "end_time": "2020-08-19T20:24:10.905951", "exception": false, "start_time": "2020-08-19T20:24:10.868617", "status": "completed"} tags=[]
def print_this(a):
    print(f'inside print_this: {a}')

a = 5
print_this(2)
print(f'a = {a}')


# + [markdown] papermill={"duration": 0.027804, "end_time": "2020-08-19T20:24:10.961928", "exception": false, "start_time": "2020-08-19T20:24:10.934124", "status": "completed"} tags=[]
# Notice that even though `print_this` was printing the variable `a` inside the function and there was a variable `a` defined outside of the function, the `print` function inside `print_this` still printed what was passed in.  However, I can also 

# + papermill={"duration": 0.041618, "end_time": "2020-08-19T20:24:11.032403", "exception": false, "start_time": "2020-08-19T20:24:10.990785", "status": "completed"} tags=[]
def print_it():
    print(f'inside print_it: {a}')
    
a = 5
print_it()
print('a = ', a)


# + [markdown] papermill={"duration": 0.028384, "end_time": "2020-08-19T20:24:11.089722", "exception": false, "start_time": "2020-08-19T20:24:11.061338", "status": "completed"} tags=[]
# Here there is no variable passed into the function so python uses th variable from the outer scope. Be careful with this second paradigm as it can be dangerous. The danger lies in th fact that the output of the function depends upon th overall state of th program (namely th value of `a`) as opposed to `print_this` hich depends only on the input of the function. Functions like `print_this` ar much asier to reason about, test, and use, they should be prferred in many contexts.
#
# That said, there is a very powerful technique called `function closure` which we can make use of this ability. Lets say we want a function which will raise a number to some exponent, but we don't know which exponent ahead of runtime.

# + papermill={"duration": 0.037438, "end_time": "2020-08-19T20:24:11.156868", "exception": false, "start_time": "2020-08-19T20:24:11.119430", "status": "completed"} tags=[]
def some_exponent(exponent):
    def func(x):
        return x**exponent
    return func


# + papermill={"duration": 0.039847, "end_time": "2020-08-19T20:24:11.225638", "exception": false, "start_time": "2020-08-19T20:24:11.185791", "status": "completed"} tags=[]
some_exponent(2)(2), some_exponent(3)(2)


# + [markdown] papermill={"duration": 0.028116, "end_time": "2020-08-19T20:24:11.282744", "exception": false, "start_time": "2020-08-19T20:24:11.254628", "status": "completed"} tags=[]
# Now that we understand how normal arguments work, lets look at a few conveniences Python provides for making functions easier to create. The first is default arguments. Let's suppose we have a function which had a bunch of arguments, but most of them had sane defaults, for example:

# + papermill={"duration": 0.040928, "end_time": "2020-08-19T20:24:11.352255", "exception": false, "start_time": "2020-08-19T20:24:11.311327", "status": "completed"} tags=[]
def print_todo(watch_tv, read, eat, sleep):
    print('I need to:')
    if watch_tv:
        print('  watch_tv')
    if read:
        print('  read')
    if eat:
        print('  eat')
    if sleep:
        print('  sleep')
print_todo(True, True, True, True)


# + [markdown] papermill={"duration": 0.028741, "end_time": "2020-08-19T20:24:11.411411", "exception": false, "start_time": "2020-08-19T20:24:11.382670", "status": "completed"} tags=[]
# I know that I almost always need to eat and sleep, so I can use a default argument for these instead.  This means I don't need to define the value of `eat` and `sleep` unless they are different than the default.

# + papermill={"duration": 0.041034, "end_time": "2020-08-19T20:24:11.480985", "exception": false, "start_time": "2020-08-19T20:24:11.439951", "status": "completed"} tags=[]
def print_todo_default(watch_tv, read, eat=True, sleep=True):
    print('I need to:')
    if watch_tv:
        print('  watch_tv')
    if read:
        print('  read')
    if eat:
        print('  eat')
    if sleep:
        print('  sleep')
print_todo_default(True, True)


# + [markdown] papermill={"duration": 0.028264, "end_time": "2020-08-19T20:24:11.538041", "exception": false, "start_time": "2020-08-19T20:24:11.509777", "status": "completed"} tags=[]
# These default arguments can allow us to create complex function with many inputs while also maintaining ease of use by setting sane defaults.
#
# Another thing we might want to do is take a variable list of arguments, lets write a similar `todo` function as before, but this time we will allow it to pass in any number of argumnts. Hre we will make use of the `*args` syntax. This `*` tells python to gather th rest of the arguments into the tuple `args`.

# + papermill={"duration": 0.039693, "end_time": "2020-08-19T20:24:11.606517", "exception": false, "start_time": "2020-08-19T20:24:11.566824", "status": "completed"} tags=[]
def print_todo_args(*args):
    print('I need to:')
    for arg in args:
        print(f'{arg}')
        
print_todo_args('watch_tv', 'read', 'eat', 'sleep')

# + [markdown] papermill={"duration": 0.028419, "end_time": "2020-08-19T20:24:11.663932", "exception": false, "start_time": "2020-08-19T20:24:11.635513", "status": "completed"} tags=[]
# This sort of syntax can be very useful in large programs where abstract functions may use all a variety of different functions with different arguments.

# + [markdown] papermill={"duration": 0.028394, "end_time": "2020-08-19T20:24:11.721485", "exception": false, "start_time": "2020-08-19T20:24:11.693091", "status": "completed"} tags=[]
# # <a id='last'>Some topics we haven't discussed, but have used</a>
# [Go back to the main page](#main)
# - [String formatting](https://docs.python.org/3/reference/lexical_analysis.html#f-strings)
# - Exceptions (e.g. `TypeError`)

# + papermill={"duration": 0.028703, "end_time": "2020-08-19T20:24:11.779107", "exception": false, "start_time": "2020-08-19T20:24:11.750404", "status": "completed"} tags=[]


StarcoderdataJupyterScriptsDedupFiltered