{ "cells": [ { "cell_type": "markdown", "id": "e6040bd4-7d0d-43f3-b73f-51b4bc5d3504", "metadata": {}, "source": [ "We use Python as a calculator." ] }, { "cell_type": "markdown", "id": "9313a7fa-8474-4a43-b0c9-c3cd351e94fe", "metadata": {}, "source": [ "# 1. Integers and Floats" ] }, { "cell_type": "markdown", "id": "1ed8c2c3-a191-49db-a134-0d53bcb50900", "metadata": {}, "source": [ "The integers in Python can grow arbitrarily long. The exponentiation in Python is executed with `**` not as `^`." ] }, { "cell_type": "code", "execution_count": 1, "id": "2103cd61-c821-4a0a-91c1-35cfcd22f1d7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1267650600228229401496703205376" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2**10)**10" ] }, { "cell_type": "code", "execution_count": 2, "id": "b76a268c-f8f1-41c4-be6b-016e6275d159", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.2676506002282294e+30" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2.0**10)**10" ] }, { "cell_type": "markdown", "id": "9ec3f1db-6b20-49c0-a85c-c22ca77fe41e", "metadata": {}, "source": [ "Observe the scientific notation, `e+30` stands for `10**30`." ] }, { "cell_type": "markdown", "id": "675b8820-2eea-4bfa-b299-d06e2eb4fd33", "metadata": {}, "source": [ "Observe: `2` is not the same as `2.0`. The `2` is the integer number two, while `2.0` is the number two as a floating-point number." ] }, { "cell_type": "code", "execution_count": 3, "id": "c92c923f-9338-432c-a99f-21dc8c05b26f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(2)" ] }, { "cell_type": "code", "execution_count": 4, "id": "e729928f-0259-4ce6-9681-b4f21bf19be8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(2.0)" ] }, { "cell_type": "markdown", "id": "03f6db3b-1359-4079-8836-52b2eff94b2f", "metadata": {}, "source": [ "The machine precision `eps` is *the smallest positive number that can be added to 1.0 and results in a number different from 1.0*." ] }, { "cell_type": "code", "execution_count": 5, "id": "743fb64c-1591-4565-a058-13fb147f4da5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0000000000000002" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1.0 + 2**(-52)" ] }, { "cell_type": "code", "execution_count": 6, "id": "073ad3d6-f12b-4f4c-b874-136defa0f01a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1.0 + 2**(-53)" ] }, { "cell_type": "markdown", "id": "5e0cfff8-8f8e-471e-a1e0-87910c200fac", "metadata": {}, "source": [ "The machine precision for the type `float` in Python is therefore `2**(-52)`." ] }, { "cell_type": "markdown", "id": "e16823f8-6db5-4627-bca1-60b629d016dc", "metadata": {}, "source": [ "## 2.1 Integer Division" ] }, { "cell_type": "markdown", "id": "57b485e8-0bba-41c5-b2d6-14d7725ca64d", "metadata": {}, "source": [ "Python does by default exact integer arithmetic, but not rational arithmetic." ] }, { "cell_type": "code", "execution_count": 7, "id": "777df523-7f1a-4746-9a34-96db05df521f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.3333333333333333" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1/3" ] }, { "cell_type": "code", "execution_count": 8, "id": "52f2f9ae-bd1d-4b1a-810a-209bb6f69f38", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1//3" ] }, { "cell_type": "markdown", "id": "c82edfaa-ea2c-45a8-8d01-8c97b978d802", "metadata": {}, "source": [ "Mathematically, both answers are wrong! From a computer science perspective they are valid if we understand the logic of the typing. The operator `/` on integers return a machine float. The operator `//` return the quotient of the integer division." ] }, { "cell_type": "markdown", "id": "51bc3aa2-44db-49d8-b939-1b6440bdf71b", "metadata": {}, "source": [ "# 2. Fractions" ] }, { "cell_type": "markdown", "id": "5cde6d14-c041-4ded-9def-c4b4a63d1617", "metadata": {}, "source": [ "Rational arithmetic is available via the `fractions` module, which must be imported." ] }, { "cell_type": "code", "execution_count": 9, "id": "1dfa8a23-55c1-4b6f-b616-86d60693d607", "metadata": {}, "outputs": [], "source": [ "from fractions import Fraction" ] }, { "cell_type": "code", "execution_count": 10, "id": "fd7c6a8a-d90d-4fdb-9873-b1ea6025d6f4", "metadata": {}, "outputs": [], "source": [ "onethird = Fraction(1, 3)" ] }, { "cell_type": "markdown", "id": "c1ea6ccc-31e8-4acb-b13e-845515b9a2af", "metadata": {}, "source": [ "A fraction has a numerator and demoninator, which we select as follows." ] }, { "cell_type": "code", "execution_count": 11, "id": "322cd729-a885-4e1a-8bbf-8ba558874674", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "onethird.numerator" ] }, { "cell_type": "code", "execution_count": 12, "id": "4bc0fac2-dc3b-4ec8-b0a1-6d3e30254e10", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "onethird.denominator" ] }, { "cell_type": "code", "execution_count": 13, "id": "4e550c7a-45f9-4016-a0dc-c5ebbf7c475b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Fraction(11, 15)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Fraction(1, 3) + Fraction(2, 5)" ] }, { "cell_type": "markdown", "id": "452ac8c0-8692-4d91-9768-417fd3b6ec79", "metadata": {}, "source": [ "The `math` module also exports the greatest divisor as the function `gcd()`." ] }, { "cell_type": "code", "execution_count": 14, "id": "62d949cb-ccf5-4176-9cd7-463895e51be3", "metadata": {}, "outputs": [], "source": [ "from math import gcd" ] }, { "cell_type": "code", "execution_count": 15, "id": "a123e2a4-fbd2-4bbc-b163-945d1c7c6df5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gcd(18, 12)" ] }, { "cell_type": "markdown", "id": "02e8f854-7af0-4561-b56c-c7414bef5ae2", "metadata": {}, "source": [ "# 3. ASCII Codes and Strings" ] }, { "cell_type": "markdown", "id": "b271b95d-6d1b-4018-81f0-800402f060f6", "metadata": {}, "source": [ "The function `ord()` returns the ASCII code of a character." ] }, { "cell_type": "code", "execution_count": 16, "id": "026e8dbf-d1e7-4ad6-85f7-3614a3f0afe0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "97" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ord('a')" ] }, { "cell_type": "code", "execution_count": 17, "id": "3b32e6d6-598c-4418-b041-f2fa8eec2695", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "98" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ord('b')" ] }, { "cell_type": "markdown", "id": "a1dace9f-394a-475e-a3b4-d3ffc03c2302", "metadata": {}, "source": [ "The function `chr()` returns the character with the given ASCII code." ] }, { "cell_type": "code", "execution_count": 18, "id": "c857a3fb-028a-4e41-952b-c64867dd774e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chr(98)" ] }, { "cell_type": "markdown", "id": "53783708-b235-41c2-965f-418fd9a81ff4", "metadata": {}, "source": [ "The quotes around `b` indicate that `'b'` is a string." ] }, { "cell_type": "markdown", "id": "603cd702-d37e-485c-8e25-157cc80aa8b1", "metadata": {}, "source": [ "Every number has a string representation, which can be computed with the `%` operator." ] }, { "cell_type": "markdown", "id": "425d9167-8e7b-4d7a-88c4-5234e3aa7f51", "metadata": {}, "source": [ "For example, the string representation of `math.pi` with 5 decimals after the `.` in scientific notation is computed as follows." ] }, { "cell_type": "code", "execution_count": 19, "id": "c64f0e55-d593-48da-a16e-2015821c7399", "metadata": {}, "outputs": [], "source": [ "from math import pi" ] }, { "cell_type": "code", "execution_count": 20, "id": "4d154282-4b9d-4099-b223-d2126fbcc368", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.14159e+00'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'%.5e' % pi" ] }, { "cell_type": "markdown", "id": "8418876c-21a4-4d70-8244-5b030ffe42e5", "metadata": {}, "source": [ "Observe the double use of `%`:\n", "\n", "1. once at the start of the format string; and\n", "\n", "2. once as the conversion operator before `pi`." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" } }, "nbformat": 4, "nbformat_minor": 5 }