<jupyter_start><jupyter_text>NumbersThe main [numeric types](https://docs.python.org/3/library/stdtypes.htmlnumeric-types-int-float-complex) in Python are integers, floating point numbers and complex numbers. The syntax for arithmetic operators are: addition `+`, subtraction `-`, multiplication `*`, division `/` and exponentiation `**`.  IntegersAdd integers:<jupyter_code>8 + 12<jupyter_output><empty_output><jupyter_text>Subtract integers:<jupyter_code>2019 - 21<jupyter_output><empty_output><jupyter_text>Multiply integers:<jupyter_code>45 * 11<jupyter_output><empty_output><jupyter_text>Divide integers (and notice that division of integers *always* returns a float):<jupyter_code>100 / 4<jupyter_output><empty_output><jupyter_text>Compute powers of integers:<jupyter_code>2**10<jupyter_output><empty_output><jupyter_text>Use the built-in function `type()` to verify the type of a Python object:<jupyter_code>type(42)<jupyter_output><empty_output><jupyter_text>Floating Point NumbersA floating point number (or float) is a real number written in decimal form. Python stores floats and integers in different ways and if we combine integers and floats using arithmetic operations the result is always a float.Approximate $\sqrt{2} \,$:<jupyter_code>2**0.5<jupyter_output><empty_output><jupyter_text>Approximate $2 \pi$:<jupyter_code>2 * 3.14159<jupyter_output><empty_output><jupyter_text>Use scientific notation to create $0.00001$:<jupyter_code>1e-5<jupyter_output><empty_output><jupyter_text>Again, use the `type()` function to verify the type of a number:<jupyter_code>type(42)
type(42.0)<jupyter_output><empty_output><jupyter_text>Complex NumbersUse the built-in function `complex()` to create a complex number in Python or use the letter `j` for $j = \sqrt{-1}$. The built-in function `complex()` takes 2 parameters defining the real and imaginary part of the complex number.Create the complex number $1 + j$:<jupyter_code>complex(1,1)<jupyter_output><empty_output><jupyter_text>Add complex numbers:<jupyter_code>(1 + 2j) + (2 - 3j)<jupyter_output><empty_output><jupyter_text>Multiply complex numbers:<jupyter_code>(2 - 1j) * (5 + 2j)<jupyter_output><empty_output><jupyter_text>Use the `type()` function to verify the type of a number:<jupyter_code>type(2 - 7j)<jupyter_output><empty_output><jupyter_text><!-- Complex MethodsThe complex datatype has a few methods. For example, we can access the real and imaginary parts of a complex number:```pythonz.real``````nohighlight1.0``````pythonz.imag``````nohighlight1.0``````pythonprint(z)``````nohighlight(1+1j)```The conjugate of a complex number $z = a + b\sqrt{-1}$ is $\overline{z} = a - b\sqrt{-1}$.```pythonz.conjugate()``````nohighlight(1-1j)```The modulus of a complex number $z = a + b\sqrt{-1}$ is $|z| = \sqrt{a^2 + b^2}$ which is computed by the builtin function `abs` (which is the absolute value when applied to integers and floats).```pythonprint(z)abs(z)``````nohighlight(1+1j)1.4142135623730951``````python(1**2 + 1**2)**(0.5)``````nohighlight1.4142135623730951```-->  Arithmetic OperatorsThe syntax for arithmetic operators in Python are:| Operator | Description  || :---: | :---: || `+` | addition || `-` | subtraction || `*` | multiplication || `/` | division || `**` | exponentiation || `%` | remainder (or modulo) || `//` | integer division |Notice that division of integers always returns a float:<jupyter_code>4 / 3<jupyter_output><empty_output><jupyter_text>Even if the mathematical result is an integer:<jupyter_code>4 / 2<jupyter_output><empty_output><jupyter_text>Use parentheses to group combinations of arithmetic operations:<jupyter_code>5 * (4 + 3) - 2<jupyter_output><empty_output><jupyter_text>An integer power of an integer is again an integer:<jupyter_code>2**4<jupyter_output><empty_output><jupyter_text>An exponent involving a float is a float:<jupyter_code>9**0.5<jupyter_output><empty_output><jupyter_text>The remainder operator computes the remainder of division of integers:<jupyter_code>11 % 4<jupyter_output><empty_output><jupyter_text>Integer division is:<jupyter_code>11 // 4<jupyter_output><empty_output><jupyter_text>Examples Taylor ApproximationThe [Taylor series](https://en.wikipedia.org/wiki/Taylor_series) of the exponential function $e^x$ is given by$$e^x = \sum_{k=0}^{\infty} \frac{x^k}{k!}$$Compute the Taylor polynomial of degree 5 evaluated at $x = 1$ to find an approximation of $e$$$e \approx \frac{1}{0!} + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + \frac{1}{4!} + \frac{1}{5!}$$<jupyter_code>1 + 1 + 1/2 + 1/(3*2) + 1/(4*3*2) + 1/(5*4*3*2)<jupyter_output><empty_output><jupyter_text>Ramanujan's $\pi$ Formula[Srinivasa Ramanujan](https://en.wikipedia.org/wiki/Srinivasa_RamanujanMathematical_achievements) discovered the following beautiful (and very rapidly converging) series representation of $\pi$$$\frac{1}{\pi} = \frac{2 \sqrt{2}}{99^2} \sum_{k = 0}^{\infty} \frac{(4k)!}{k!^4} \frac{1103 + 26390k}{396^{4k}}$$Let's find an approximation of $\pi$ by computing the *reciprocal* of the sum of the first 3 terms of the series:$$\pi \approx \frac{99^2}{2 \sqrt{2}} \frac{1}{\left( 1103 + 4! \frac{1103 + 26390}{396^{4}} + \frac{8!}{2^4} \frac{1103 + 26390(2)}{396^{8}} \right)}$$<jupyter_code>99**2 / (2 * 2**0.5) / (1103 + 4*3*2 * (26390 + 1103) / 396**4
                       + 8*7*6*5*4*3*2 / 2**4 * (26390*2 + 1103) / 396**8)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1597918989223337, 'fraction_numerical': 0.05239687848383501, 'mean_word_length': 6.846938775510204, 'pattern_counts': {'":': 0, '<': 94, '<?xml version=': 0, '>': 94, 'https://': 3, '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': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '488748', 'license': 'CC0-1.0', 'n_tokens_mistral': 2108, 'n_tokens_neox': 1877, 'n_words': 436, 'path': 'computing-primer/python/numbers.ipynb', 'repo_name': 'osterEWU/mathemathemical_computing_with_Python', 'size': '5382'}