{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# [Numbers](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `int`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_int = 6\n", "print(f\"value: {my_int}, type: {type(my_int)}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `float`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_float = float(my_int)\n", "print(f\"value: {my_float}, type: {type(my_float)}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that division of `int`s produces `float`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(1 / 1)\n", "print(6 / 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Be aware of the binary floating-point pitfalls (see [Decimal](#decimal) for workaround):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val = 0.1 + 0.1 + 0.1\n", "print(val == 0.3)\n", "print(val)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Floor division `//`, modulus `%`, power `**`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "7 // 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "7 % 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2**3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## [`decimal.Decimal`](https://docs.python.org/3/library/decimal.html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from decimal import Decimal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from_float = Decimal(0.1)\n", "from_str = Decimal(\"0.1\")\n", "print(f\"from float: {from_float}\\nfrom string: {from_str}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_decimal = Decimal(\"0.1\")\n", "sum_of_decimals = my_decimal + my_decimal + my_decimal\n", "print(sum_of_decimals == Decimal(\"0.3\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operator precedence in calculations\n", "Mathematical operator precedence applies. Use brackets if you want to change the execution order:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(1 + 2**2 * 3 / 6) # 1 + 4 * 3 / 6 == 1 + 12 / 6 == 1 + 2\n", "print((1 + 2**2) * 3 / 6)" ] } ], "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.11.0" } }, "nbformat": 4, "nbformat_minor": 2 }