{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python as a calculator\n", "\n", "```{admonition} Interactive page\n", ":class: warning, dropdown\n", "This is an interactive book page. Press the launch button at the top right side.\n", "```\n", "\n", "Python has a set of **mathematical functions** that are built directly into the language. You can, therefore, use Python as a calculator." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "remove-output" ] }, "outputs": [], "source": [ "1+1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculations also work with variables:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "remove-output" ] }, "outputs": [], "source": [ "a = 5\n", "print(a+1)" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "grade_id": "cell-74f5aea3a72d2f71", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "```{exercise}\n", "Discover what the following Python operators do by performing some math with them in the code cell below: `*`, `-`, `/`, `**`, `//`, `%`. Use `print` to investigate the outputs.\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{admonition} Integer division (//) and modulus (%) operators\n", ":class:note\n", "\n", "The **integer division** operator `//` performs division and truncates the decimal part to return the largest **integer** less than or equal to the result.\n", "\n", "The **modulus** operator `%` gives the **remainder** when one number is divided by another.\n", "\n", "For example, if we divide 13 by 3, the result is 4.333. Therefore, the largest integer less than the resulting 4.333 is 4 and the remainder is 1. If you think in the opposite direction, you could \"construct\" number 13 as 3*4 + 1. Therefore, in Python the result of `13 // 3` would be 4, and the result of `13 % 3` would be 1.\n", "```" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "grade_id": "cell-8cd6e8f83013a575", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "Another handy built-in function is `abs()` for calculating absolute values:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "remove-output" ] }, "outputs": [], "source": [ "print(abs(10))\n", "print(abs(-10))\n", "print(abs(1j))\n", "print(abs(1+1j))" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "grade_id": "cell-ec6f5beb19edae73", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "You can find the full list of built-in mathematical commands on the [Python documentation webpage](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)." ] } ], "metadata": { "jupytext": { "formats": "ipynb,md" }, "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.4" } }, "nbformat": 4, "nbformat_minor": 4 }