<jupyter_start><jupyter_text>This is text shown in the notebook with markdown<jupyter_code># This is a comment, no markdown here!
x = 1<jupyter_output><empty_output><jupyter_text>x is a variable. It can change its value by assignment.<jupyter_code>print(x)
x = x + 1
x<jupyter_output><empty_output><jupyter_text>You can have as many variables as you want...<jupyter_code>y = 7
z = x * y<jupyter_output><empty_output><jupyter_text>Output of last statement is shown in console, if there is no assignment<jupyter_code>z = z
z

y<jupyter_output><empty_output><jupyter_text>Print() can be used to show contents of variables. Works always, but messy. Better use logging (later in class?).<jupyter_code>print(z)

print(y)<jupyter_output><empty_output><jupyter_text>If statementAn if statement assesses a boolean statement (TRUE or FALSE). If statement is TRUE, first command block is executed.IF statement is FALSE, second command block is executed.<jupyter_code>if z > 3:
    print("z is larger than 3")
else:
    print("z is smaller than or equal to 3")<jupyter_output><empty_output><jupyter_text>Correct indentation in Python is syntax, i.e. obligatory<jupyter_code>if z > 3:
print("z is larger than 3")<jupyter_output><empty_output><jupyter_text>Exercise 1Create a variable named `infected_03_17` and `infected_03_21`.Assign the values `1332` and `2785`, respectively.Assign `4` to the variable `diff_days`.Calculate the average daily discrete growth rate given by $\frac{\textrm{infected_03_21}}{\textrm{infected_03_17}}^{\frac{1}{\textrm{diff_days}}} - 1$ and assign it to the variable `growth_rate`. Note: power function is written as $x^2$ = x ** 2 in Python code.Display the `growth_rate`.  FunctionsA function is used to put code into one container that can be reused. You will write some functions, but you will use even more functions.Most functions you are going to use are part of packages, i.e. containers of functions (more on that later!). For now you have to know that if you want to use a function, you first have to import the package. The package has to be available on your computer, so sometimes it has to be installed with conda first. Many important packages come in the conda standard installation already. Calculate the square root of a number:<jupyter_code>import math

math.sqrt(7)<jupyter_output><empty_output><jupyter_text>Of course, you can also pass variables to functions:<jupyter_code>x = 7

math.sqrt(7)<jupyter_output><empty_output><jupyter_text>For the moment, we do not write our own functions, but only use them. This will also be important for your home work, where you use functions to encrypt a text.  Exercise 2Calculate the logarithm of `infected_03_17` and `infected_03_21` with the Python library `math`. Use Google to find the correct function name in python. Or guess ;-)Display the result.  Exercise 3 The code below assigns a random value in the range $[0, 50]$ to $x$. Add code to the cell that prints "Above 25" if the value of $x$ is above $25$ and "Below 25" if the value of $x$ is below $25$. Use an if statement for that purpose.<jupyter_code>import random

x = random.randrange(0, 50)<jupyter_output><empty_output><jupyter_text>Exercise 4 The following code should calculate the doubling time of an exponential process from the growth_rate calculated before. However, something is wrong. Find the error!<jupyter_code>doubling_time = log(2) / log(1 + growth_rate)

doubling_time<jupyter_output><empty_output><jupyter_text>Writing your own functionsSomewhere up here, we have calculated the growth rate of a process for two timesteps. It would be handy to reuse this code without having to retype the formula each time. This can be accompolished with your own user-defined function.<jupyter_code>def growth_rate(value_t1, value_t2, time_diff):
    result = (value_t2 / value_t1) ** (1 / time_diff) - 1
    
    return(result)
growth_rate
growth_rate(100, 200, 1)
t1 = 100
t2 = 200
diff = 1

growth_rate(t1, t2, diff)
value_t1 = 100
value_t2 = 200
time_diff = 1

growth_rate(value_t1, value_t2, time_diff)<jupyter_output><empty_output><jupyter_text>Exercise 5Write a function to determine the doubling time of an exponential process, given the growth rate.  Exercise 6This function should calculate the relative growth rate of a process given two discrete variables. The formula is given by $\textrm{relative_growth} = \frac{\textrm{value_t2} - \textrm{value_t1}}{\textrm{value_t2}}$. However, the function does not work. Find the problem!<jupyter_code>def relative_growth(value_t1, value_t2):
    result = (x_t2 - x_t1) / x_t1
    return result
relative_growth(2, 1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08159311087190528, 'fraction_numerical': 0.024757804090419805, 'mean_word_length': 5.516129032258065, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '126812', 'license': 'MIT', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1411, 'n_words': 572, 'path': 'lecture02-conda-python/my-first-notebook.ipynb', 'repo_name': 'WaterboyG/lecture-scientific-computing', 'size': '4645'}