<jupyter_start><jupyter_text>Scientific Programming I <NAME>  Thanks to <NAME> and <NAME> for ideas.  1. Individual thingsThe most basic component of any programming language are "things", also called variables orin some languages such as Python, objects.The most common basic "things" in Python are integers, floats, strings, booleans, andsome special objects of various types. We'll meet many of these as we go through the lesson. __TIP:__ To run the code in a cell quickly, press Ctrl+Enter. To run the code in a cell and then proceed to the next cell, press Ctrl+Shift. The Help menu above lists more shortcuts under the Keyboard Shortcuts menu item. You can also use the buttons under the menu to delete cells (use the Cut button), create new ones, move cells, etc.<jupyter_code># A thing
2
# Use print to show multiple things in the same cell
print(2)
print('hello')  # Both single and double quotes are fine
# Things can be stored as variables
a = 2
b = 'hello'
c = True  # This is case sensitive
print(a, b, c)
# We can use type to determine what kind of thing we have
print(type(a))
print(type(b))
print(type(b))<jupyter_output><class 'int'>
<class 'str'>
<class 'str'><jupyter_text>2. Commands that operate on thingsWhile things are great, things by themselves aren't that much use to us. Right away, we'd like to start doing stuff, that is peforming operations, with various things. There are three common means of performing an operation on a thing.  2.1 Use an operatorAll of the basic math operators work like you think they should for numbers. They can alsodo some useful operations on other things, like strings. There are also boolean operators thatcompare quantities and give back a `bool` variable as a result.<jupyter_code># Standard math operators work as expected on numbers
a = 2
b = 3
print(a + b)
print(a * b)
print(a ** b)  # You don't want ^
# There are also operators for strings
print('hello' + 'world')
print('hello' * 3)
#print('hello' / 3)
# Boolean operators compare two things
a = (1 > 3)
b = (3 == 3)
print(a)
print(b)
print(a or b)
print(a and b)<jupyter_output>False
True
True
False<jupyter_text>2.2 Use a functionThese will be very familiar to anyone who has programmed in any language, and work like youwould expect.<jupyter_code># There are thousands of functions that operate on things
print(type(3))  # We've already met print and type
print(type('hello'))
print(len('hello'))
print(round(3.3))<jupyter_output><class 'int'>
<class 'str'>
5
3<jupyter_text>__TIP:__ To find out what a function does, you can type it's name and then a question mark toget a pop up help window. The window will describe the function and also tell you what input parameters it takes. Parameters in square brackets are optional.<jupyter_code>#round?
round(3.14159, 2)<jupyter_output><empty_output><jupyter_text>__TIP:__ Many useful functions are not built in to core Python, but are found in external scientific packages. These need to be imported into your Python notebook (or program) before they can be used. In addition to functions, these external packages also often include new types of things, such as numeric arrays and data tables. We'll meet several of the most important scientific packages in the next lesson. When these packages are imported into Python, we usually refer to them as modules.<jupyter_code># Import the random module and use a function from it
# After importing a module, use a '.' to look inside of it
import random as rnd
rnd.random()<jupyter_output><empty_output><jupyter_text>2.3 Use a methodBefore we get any farther into the Python language, we have to say a word about "objects". Wewill not be teaching object oriented programming in this workshop, but you will encounter objects throughout Python (in fact, even seemingly simple things like ints and strings are actually objects in Python).In the simplest terms, you can think of an object as a small bundled "thing" that contains within itself both data and functions that operate on that data. For example, strings in Python are objects that contain a set of characters and also various functions that operate on the set of characters. When bundled in an object, these functions are called "methods". The reason to bundle a function up as a method, instead of making it free floating, is that it makes it clear what kind of variable the function can operate on.Instead of the "normal" `function(arguments)` syntax, methods are called using thesyntax `variable.method(arguments)`. Think of the '.' as short for "look inside".<jupyter_code># A string is an object
a = 'hello, world'
# Strings have many methods
print(a.capitalize())
print(a.replace('l', 'X'))<jupyter_output>Hello, world
heXXo, worXd<jupyter_text>EXERCISE 1 - Introducing logistic growthThroughout this lesson, we will successively build towards a program that will calculate the logistic growth of a population of bacteria in a petri dish (or bears in the woods, if you prefer). Logistic growth produces a classic S shaped curve in which a population initially grows very fast, then slows down over time until it reaches a steady state value known as the carrying capacity.For example, when there are only few bears in the woods and lost of food, they reproduce very fast. As the woods get full of bears and food gets harder to find, growth slows until the number of bears just balances with the amount of food (and hunters) in the woods, at which point the population stops growing.A commonly used discrete time equation for logistic population growth is$$ n_{t+1} = n_{t} + r n_{t} (1 - n_{t} / K) $$where $n_t$ is the population size at time $t$, $r$ is the maximum net per capita growth rate, and $K$ is the carrying capacity of the dish/woods.To get started, write Python expressions that do the following:1. Create variables for `r`, `K`, and `n0` (the population at time $t = 0$), setting these equal to 0.3, 100, and 10, respectively.1. Create the variable `n1` and calculate it's value. Do the same for `n2`.1. Check the type of `n2` - what is it?__Bonus__1. Figure out how to test whether `n2` is an integer (a mathematical integer, not necessarilywhether it is an integer type) (HINT: look at the methods of `n2` by typing `n2.` and pressingtab.)1. Modify your calculations for `n1` and `n2` so that these values are rounded to the nearestinteger. When you do so, you might notice that your answer to Bonus 1 above stops working --- why?<jupyter_code>r = 0.3
K = 100
n0 = 10

n1 = n0 + r*n0*(1 - n0/K) # round()
n2 = n1 + r*n1*(1 - n1/K) # round()

print(n0, n1, n2)
print(type(n2))

print('n2 is an integer: ', n2.is_integer())<jupyter_output>10 12.7 16.02613
<class 'float'>
n2 is an integer:  False<jupyter_text>3. Collections of thingsOnce the number of variables that you are interested in starts getting large, working with them all individually starts to get unwieldy. To help stay organized, we can use collections of things.There are many types of collections in Python, including lists, tuples, dictionaries, and numpy arrays. Here we'll look just at the most flexible and simplest container, the list. Lists are declared using square brackets []. You can get individual elements out of a list using the syntax `a[idx]`.<jupyter_code># Lists are created with square bracket syntax
a = ['hi', 'hello', 'yo']
print(a)
print(type(a))
# Lists (and all collections) are also indexed with square brackets
# NOTE: The first index is zero, not one
print(a[0])
print(a[1])
# Lists can be sliced by putting a colon between indexes
# NOTE: The end value is not inclusive
print(a[0:2])

# Think of this behavior as counting the "walls" between items
# You can leave off the start or end if desired
print(a[:2])
print(a[2:])
print(a[:])
print(a[:-1])
# Lists are objects, like everything else, and have methods such as append
a.append('hiya')
print(a)

a.append([1,2])
print(a)

a.pop()
print(a)<jupyter_output>['hi', 'hello', 'yo', 'hiya']
['hi', 'hello', 'yo', 'hiya', [1, 2]]
['hi', 'hello', 'yo', 'hiya']<jupyter_text>EXERCISE 2 - Storing population size in a listCopy your code from Exercise 1 into the box below, and do the following:1. Modify your code so that the values of `n0`, `n1`, and `n2` are stored in a list and not asseparate individual variables. HINT: You can start off by declaring an empty list using the syntax`n = []`, and then append each new calculated value of `nt` to the list.1. Get the first and last values in the list, calculate their ratio, and print out "Grew by a factor of "followed by the result.__Bonus__1. Extract the last value in two different ways: first, by using the index forthe last item in the list, and second, presuming that you do not know how long the list is.<jupyter_code>######################################
# This code deletes our old variables
try: del n0, n1, n2, r, K
except: pass
######################################

r = 0.3
K = 100
n = []
n.append(10)  # Append n0 in the first location

n.append(round(n[0] + r*n[0]*(1 - n[0]/K)))  # Append n1
n.append(round(n[1] + r*n[1]*(1 - n[1]/K)))  # Append n2

print(n)

print("Grew by a factor of ", n[2]/n[0])  # or n[-1]/n[0]<jupyter_output>[10, 13, 16]
Grew by a factor of  1.6<jupyter_text>4. Repeating yourselfSo far, everything that we've done could, in principle, be done by hand calculation. In this section and the next, we really start to take advantage of the power of programming languages to do things for us automatically.We start here with ways to repeat yourself. The two most common ways of doing this are known as for loops and while loops. For loops in Python are useful when you want to cycle over all of the items in a collection, such as a list, and while loops are useful when you want to cycle for an indefinite amount of time until some condition is met.<jupyter_code># A basic for loop - pay attention to the white space!
wordlist = ['hi', 'hello', 'bye']
for word in wordlist:
    print(word + '!')
# Sum all of the values in a collection using a for loop
numlist = [1, 4, 77, 3]
total = 0

for num in numlist:
    total = total + num
    
print("Sum is", total)
# Sometimes we want to loop over the indexes of a collection, not just the items
print(wordlist)

length = len(wordlist)
print(length)

for i in range(length):
    print(i, wordlist[i])
# While loops are useful when you don't know how many steps you will need,
# and want to stop once a certain condition is met.
stp = 0
prd = 1
while prd < 100:
    stp = stp + 1
    prd = prd * 2
    print(stp, prd)
    
print('Reached a product of', prd, 'at step number', stp)<jupyter_output>1 2
2 4
3 8
4 16
5 32
6 64
7 128
Reached a product of 128 at step number 7<jupyter_text>__TIP:__ Once we start really generating useful and large collections of data, it becomes unwieldy to inspect our results manually. The code below shows how to make a very simple plot. We'll do more plotting later.<jupyter_code># Jupyter notebook command to load plotting functions
%pylab inline

# Make some data and plot it
y = [2,7,1,10]
plot(y)<jupyter_output>Populating the interactive namespace from numpy and matplotlib<jupyter_text>EXERCISE 3 - Using loops to repeat calculationsLet's get smart about calculating our population size. Copy your code from Exercise 2 into the box below, and do the following:1. Write a for loop to calculate and store the values of `nt` for 100 time steps. HINT: Use the `range` function to pick the number of time steps. For each step, append the new population value to a list called `n`. What indexing strategy can you use to get the last value of the list each time around the for loop?1. Plot the population sizes in the list `n`.1. Play around with the values of `r` and `K` and see how it changes the plot. What happens if you set `r` to 1.9 or 3, or values in between?__Bonus__1. Modify your code to use a while loop that will stop your calculation after the population size exceeds 80. HINT: Start a step counter `i` at 1, and check that the population size is less than 80 each time around the loop. Increment the step counter within the loop so that you have a record of what step the calculation stopped at.<jupyter_code>r = 0.3
K = 100
n0 = 10

n = []
n.append(n0)

for i in range(100):
    n.append(round(n[-1] + r*n[-1]*(1 - n[-1]/K)))
    
plot(n)
r = 0.3
K = 100
n = []
n.append(10)

i = 1
while n[-1] < 80:
    n.append(round(n[i-1] + r*n[i-1]*(1 - n[i-1]/K)))
    print(i, n[i])
    i = i + 1
    
print('Ended with a population size of', n[i-1], 'at step', i-1)
print('Population sizes to this step were', n[0:i])<jupyter_output>1 13
2 16
3 20
4 25
5 31
6 37
7 44
8 51
9 58
10 65
11 72
12 78
13 83
Ended with a population size of 83 at step 13
Population sizes to this step were [10, 13, 16, 20, 25, 31, 37, 44, 51, 58, 65, 72, 78, 83]<jupyter_text>5. Making choicesOften we want to check if a condition is True and take one action if it is, and another action if the condition is False. We can achieve this in Python with an if statement.__TIP:__ You can use any expression that returns a boolean value (True or False) in an if statement. Common boolean operators are ==, !=, , >=.<jupyter_code># A simple if statement
x = 3
if x > 0:
    print('x is positive')
elif x < 0:
    print('x is negative')
else:
    print('x is zero')
# If statements can also use boolean variables
x = -1
test = (x > 0)
if test:
    print('Test was true')<jupyter_output><empty_output><jupyter_text>EXERCISE 4 - Adding some randomness to the modelLet's introduce some element of randomness into our population growth model. We'll model a simple "catastrophe" process, in which a catastrophe happens in 10% of the time steps that reduces the population back down to the initial size. Copy your code from Exercise 4 into the box below, and do the following:1. Inside your for loop, add a variable called `cata`, for catastrophe, that will be True if a catastrophe has occurred, and False if it hasn't. A simple way to do this is to generate a pseudorandom number between 0 and 1 using `rnd.random()`. Check whether this number is less than 0.1 - this check will be True 10% of the time.1. Using your boolean variable `cata`, add an if statement to your for loop that checks whether `cata` is true in each time step. If it is true, set the population back to the initial size. Otherwise, perform the usual logistic growth calculation.1. Plot your results. Run the cell again to see a different random population growth path.__Bonus__1. Count the number of time steps in your list `n` in which the population was above 50.<jupyter_code>r = 0.3
K = 100
n0 = 10

n = []
n.append(n0)

for i in range(100):
    cata = (rnd.random() < 0.1)  # Random catastrophe 10% of time
    if cata:
        n.append(n0)
    else:
        n.append(round(n[-1] + r*n[-1]*(1 - n[-1]/K)))
    
plot(n)
above = 0
for pop in n:
    if pop > 50:
        above += 1
    
print('Population was above 50 in', above, 'out of 100 steps')<jupyter_output>Population was above 50 in 41 out of 100 steps<jupyter_text>6. Creating chunks with functions and modulesOne way to write a program is to simply string together commands, like the ones above, in one long file, and then to run that file to generate your results. This may work, but it can be cognitively difficult to follow the logic of programs written in this style. Also, it does not allow you to reuse your code easily - for example, what if we wanted to run our logistic growth model for several different choices of initial parameters?The most important ways to "chunk" code into more manageable pieces is to create functions and then to gather these functions into your own modules, and eventually packages. Below we will discuss how to create functions and modules. A third common type of "chunk" in Python is classes, but we will not be covering object-oriented programming in this workshop.<jupyter_code># It's very easy to write your own functions
def multiply(x, y):
    return x*y
# Once a function is "run" and saved in memory, it's available just like any other function
print(type(multiply))
print(multiply(4, 3))
# It's useful to include docstrings to describe what your function does
def say_hello(time, people):
    return 'Good ' + time + ', ' + people
say_hello('afternoon', 'friends')
# All arguments must be present, or the function will return an error
#say_hello('afternoon')
# Keyword arguments can be used to make some arguments optional by giving them a default value
# All mandatory arguments must come first, in order
def say_hello(time, people='friends'):
    return 'Good ' + time + ', ' + people
say_hello('afternoon')
say_hello('afternoon', 'students')<jupyter_output><empty_output><jupyter_text>EXERCISE 6 - Creating a logistic growth functionFinally, let's turn our logistic growth model into a function that we can use over and over again. Copy your code from Exercise 5 into the box below, and do the following:1. Turn your code into a function called `logistic_growth` that takes four arguments: `r`, `K`, `n0`, and `p` (the probability of a catastrophe). Make `p` a keyword argument with a default value of 0.1. Have your function return the `n` list of population sizes.1. In a subsequent cell, call your function with different values of the parameters to make sure it works. Store the returned value of `n` and make a plot from it.__Bonus__1. Refactor your function by pulling out the line that actually performs the calculation of the new population given the old population. Make this line another function called `grow_one_step` that takes in the old population, `r`, and `K`, and returns the new population. Have your `logistic_growth` function use the `grow_one_step` function to calculate the new population in each time step.<jupyter_code>def logistic_growth(r, K, n0, p=0.1):

    n = []
    n.append(n0)

    for i in range(100):
        cata = (rnd.random() < p)  # Random catastrophe 10% of time
        if cata:
            n.append(n0)
        else:
            n.append(round(n[-1] + r*n[-1]*(1 - n[-1]/K)))
            #n.append(grow_one_step(n[-1], r, K))
    
    return n

def grow_one_step(nold, r, K):
    return round(nold + r*nold*(1 - nold/K))
a1 = logistic_growth(0.3, 100, 10)
a2 = logistic_growth(0.3, 100, 10)
a3 = logistic_growth(0.3, 100, 10)

plot(a1)
plot(a2)
plot(a3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08611916616512556, 'fraction_numerical': 0.023472123433823933, 'mean_word_length': 4.279607163489312, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '260945', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 5633, 'n_tokens_neox': 5257, 'n_words': 2799, 'path': 'lessons/python/python1-master.ipynb', 'repo_name': 'BIDS/2017-01-12-ucb', 'size': '18293'}