<jupyter_start><jupyter_text>Data Analysis with Jupyter Notebooks. Tutorial 2 — Simple Calculations<NAME>, University of Bath.  Contents- [Simple Calculations](simple_calculations)- [Comments and Markdown cells](comments)- [Mathematical Functions and Modules](functions_and_modules)- [Variables](variables)  Simple calculationsOne of the simplest forms of &ldquo;code&rdquo; that can be run in code cells is mathematical expressions:>```python1+2+3+4``` >```python4*5/2``` >```python2**4 - 2``` `**` is the &ldquo;power&rdquo; operator. This code calculates $2^4 - 2$. Perform the following calculations in the three cells below:      $1+1+2+3+5+8$       $1*2*3*4*5*6$      $2^{10}$ Running the cell below will check your results for these three calculations.  If any of these give errors, you can go back to edit your code, and re-run to check your changes.<jupyter_code># This cell tests your answers from the three previous code cells.
# You do not need to edit it
assert ___ == 20
assert __ == 720
assert _ == 1024<jupyter_output><empty_output><jupyter_text>Mathematical functions and modulesIn mathematics, a **function** converts one number to another number; $y=f(x)$.In programming, a **function** is more general than this, and converts an input into an output. For example, if we want to calculate a square root, we can use the `sqrt()` function.>```pythonsqrt(4)``` This has given us an error:  NameError: name 'sqrt' is not defined. Python has a *lot* of built in commands (functions). Solving any particular problem will only require a small subset of these. To keep Python code efficient, only a minimal set of the available tools are available "out of the box". Other commands (such as mathematical functions) are collected in **modules** that we can load, to make these available in our notebook.`sqrt` lives in the `math` module. We can load it like this:>```pythonfrom math import sqrtsqrt(4)``` Edit the last code cell to import the math function and re-run it.  Or we can import the entire math module:>```pythonimport mathmath.sqrt(4)``` Edit the previous code cell again to calculate $\sqrt 4$ by importing the entire math module. You can think of `math.sqrt()` as instructing the computer to &ldquo;use the `sqrt()` function provided inside the  `math` module&rdquo;. The `math` module contains a [large set of common mathematical functions](https://docs.python.org/2/library/math.html), and the constants $\pi$ and $\mathrm{e}$ (natural logarithm).>```pythonfrom math import pi, sin, e, log```The `log` function calculates the natural logarithm, i.e. $\ln x$. >```pythonpi``` >```pythonsin( pi/2 )``` >```pythone  natural logarithm``` >```pythonlog(e)  the natural logarithm of e``` >```pythone**2``` Calculate the following:    $\cos(2\pi)$  $\ln(2\mathrm{e})-\ln(2)$$\log_{10}(10)$          The math function for $\log_{10}$ is log10().<jupyter_code># This cell tests your answers from the three previous code cells.
# You do not need to edit it
from math import fabs
assert fabs( ___ )- 1.0 < 1e-10
assert fabs( __ ) - 1.0 < 1e-10
assert fabs( _ )  - 1.0 < 1e-10<jupyter_output><empty_output><jupyter_text>VariablesMany of the code examples we have already seen produce some information. When each code cell is executed, if that code **returns** a result, this is printed directly underneath the corresponding code cell, next to Out[ ]:.>```python72/4``` >```pythonprint("Nothing is returned here!")``` print() is another function, like `math.sqrt()` or `math.log()`. Instead of performing a mathematical calculation, print() just prints whatever is inside the brackets. In this example, this is exactly what happened. The text is printed to the screen, but the print() function does not return a value.print() can print more than one variable if these are separated by commas:>```pythonprint("72/4 =",72/4)``` Import statements are another example of code that does not return anything.>```pythonfrom math import sqrt``` If you run an code cell and nothing appears underneath, the code ran okay (and hopefully did what you expected). Any output under a cell will either be the **returned** result, or an error. One aspect that makes programmatic data analysis useful comes from the ability to write complex procedures with many steps, that are then performed *identically* every time the code is run against new data sets. To build up more sophisticated data analysis workflows, we often want to keep a result of one step, to use in a later step. Storing results in computer memory is called **assigning** **variables**. A variable is just a name; a sequence of letters and numbers; that labels the stored result. Then, to access the value stored in the variable, we can use the label (the variable name) to refer to the original result.>```python calculate 2 + 32 + 3``` >```python calculate 2 + 3 and store the result in the variable `my_result`my_result = 2 + 3``` Notice there is no return value printed to Out[ ]:.  Instead a variable `my_result` is created, and the value returned by the calculation is stored here.  Variable names can be nearly anything, as long as that name is not already used for some part of Python (e.g. `print`). Two limitations are they cannot begin with a number (but can contain numbers), and they cannot contain spaces. Underscores are commonly used instead of spaces to keep the code readable.>```python1st_result = 3 + 4``` >```pythonthis result = 5 + 6``` Fix the two previous code cells to use first_result and this_result so that they will run without errors. To check the value stored in a variable we can just type the variable name, which returns the stored value.>```pythonmy_result``` Note that we only get the **last** value returned if we have multiple lines of code.>```pythonmy_resultthis_result``` We can get round this by using the `print` function to print out the value stored in one or more variables>```pythonprint( my_result )print( this_result )``` Variables can be used to store raw numbers, and can then be used for calculations.>```pythonthe_number_six = 6my_result + the_number_six``` Any code that uses variables may itself return a further result, which can be assigned to a new variable, and used later (and so on).>```pythonyet_another_variable = my_result + the_number_sixprint( yet_another_variable )``` If you refer to a variable that has not yet been created you will get an error.>```pythonprint( bananas )``` Edit the code cell above so to print "bananas" (the quotes are part of the new code), and re-run the cell. Create three variables, $x$, $y$, and $z$, and use them to store the numbers $5,6,7$.  Using these variables, calculate:  $5+6+7$,  and  $(5+6)\times7$.<jupyter_code># create your variables and store the numbers 5, 6, 7
◽◽◽
◽◽◽
◽◽◽
# use the variables to calculate 5+6+7
◽◽◽
# use the variables to calculate (5+6)×7
◽◽◽
# This cell tests your answers from the three previous code cells.
# You do not need to edit it
assert (z,y,x) == (7,6,5)
assert __ == 18
assert _ == 77<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10119726339794755, 'fraction_numerical': 0.016961231470923602, 'mean_word_length': 4.881810561609388, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 40, 'https://': 1, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '281333', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2047, 'n_tokens_neox': 1882, 'n_words': 998, 'path': 'Tutorial_2.ipynb', 'repo_name': 'pythoninchemistry/chem_data_analysis_jupyter', 'size': '7028'}