<jupyter_start><jupyter_text>02. MathOne important use of python is to do math which would normally be too difficult, or too repetitive to do yourself. Python allows you to do many of the normal mathematical operations including:<jupyter_code>addition = 4.2 + 3.9
print(f"Addition: {addition}")

subtraction = 4.2 - 3.9
print(f"Subtraction: {subtraction}")

multiplication = 4.2 * 3.9
print(f"Multiplication: {multiplication}")

division = 4.2 / 3.9
print(f"Division: {str(division)}")

integer_division = 4.2 // 3.9
print(f"Integer division: {integer_division}")

remainder = 4.2 % 3.9
print(f"Remainder: {remainder}")

power = 4.2 ** 3.9
print(f"Power: {power}")<jupyter_output>Addition: 8.1
Subtraction: 0.30000000000000027
Multiplication: 16.38
Division: 1.076923076923077
Integer division: 1.0
Remainder: 0.30000000000000027
Power: 269.57041830455506<jupyter_text>Did any of those results surprise you? What about 4.2-3.9? We know that is equal to 0.3, but python tells us it's 0.30000000000000027!What you're seeing here is a phenomenon known as `floating point error`. Normally when we think about number, we think in `base 10`. The `base` you use tells you how high you count before changing the next digit. So in `base 10` we count 01, 02, 03, 04, 05, 06, 07, 08, 09 and then 10. We then count 11, 12, 13, 14, 15, 16, 17, 18, 19, and then since we've counted another 10, the next digit changes to a 2 (20).Computers don't use `base 10`, they use `base 2` or binary:| Base 10 (Decimal) | Base 2 (Binary) ||-------------------|-----------------||        0          |        0        ||        1          |        1        ||        2          |       10        ||        3          |       11        ||        4          |      100        ||        5          |      101        |And so on. The reason we get `floating point error` is because there are some numbers which can't be stored correctly in binary. Consider what happens when you divide 10 by 3. The answer is 0.333333... (the 3's go on forever). A computer wouldn't be able to store this number exactly because you can't store an infinite amount of information. Your computer could use all its storage space and still not be able to exactly store this number. When we write 4.2-3.9, python converts those numbers into binary and then subtracts them and then converts it back into decimal to show to us. It is converting back and forth between binary and decimal which produces these errors.  Advanced MathematicsYou can do a lot more with python than just those simple operations. To start you need to add extra functionalility to the base python. This is done by `importing` a package. You can learn more in the [Modules](10.%20Modules.ipynb) section.<jupyter_code>import math<jupyter_output><empty_output><jupyter_text>Now that we've imported this, we can start doing some more interesting mathematics including:<jupyter_code>pi = math.pi
print(f"Pi: {pi}")

factorial = math.factorial(5)
print(f"Factorial: {factorial}")

exponential = math.exp(5)
print(f"Exponential: {exponential}")

logarithm = math.log(5)
print(f"Logarithm: {logarithm}")

log_base_10 = math.log10(5)
print(f"Log base 10: {log_base_10}")

square_root = math.sqrt(5)
print(f"Square root {square_root}")

sin = math.sin(pi)
print(f"Sin: {sin}")

cos = math.cos(pi)
print(f"Cos: {cos}")

tan = math.tan(pi)
print(f"Tan: {tan}")<jupyter_output>Pi: 3.141592653589793
Factorial: 120
Exponential: 148.4131591025766
Logarithm: 1.6094379124341003
Log base 10: 0.6989700043360189
Square root 2.23606797749979
Sin: 1.2246467991473532e-16
Cos: -1.0
Tan: -1.2246467991473532e-16<jupyter_text>Once again, look at the answers and see if any look strange. Grab a calculator and sheck if you get the answer you expect.Sin and Tan of $\pi$ should be equal to 0, but we see that it's equal to $+$(or$-$)$1.22*10^{-16}$. This is those annoying floating point errors popping up again. There are many other things you can do with the `math` package. A list of the most commonly used functions is given [here](https://www.programiz.com/python-programming/modules/math). In the following python cell go through the list and try out a few of the functions. Remember to use a function you need to prepend the name with `math.function_name`<jupyter_code># Try out different math functions here!<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10396496081143385, 'fraction_numerical': 0.08483171968649147, 'mean_word_length': 3.7472647702407, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '249836', 'license': 'MIT', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1336, 'n_words': 554, 'path': 'python/03. Math.ipynb', 'repo_name': 'jlibermann/panoptes-tutorials', 'size': '4338'}