{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Expressions\n", "\n", "**Expressions** are the base unit in Python (and indeed, in most programming languages). You can think of these as similar to \"terms\" in a typical algebraic equation, though we'll see shortly how that analogy doesn't go very far.\n", "\n", "Python always tries to calculate **expressions** to the most literal value possible. Consider putting `2 + 4` into a calculator. In this example, we don't really care about the equation `2 + 4`, but rather _the result_. Likewise, Python cares about the result, and tries to do whatever work is required to get there -- in this case, addition.\n", "\n", "Expressions are usually composed of other expressions; this allows us to create equations and formulas more interesting than just the two-term `2 + 4`. In fact, we even consider the numbers themsevles expressions: `2` and `4` are each expressions which are composed together with the `+` operation to form the full expression." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 + 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python follows all of the expected order of operations in mathematics (\"PEMDAS\"), and evaluates inside-out, left-to-right. As such, the following two expressions result in different evaluations:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 * 4 + 5 * 6" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "162" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 * (4 + 5) * 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given the following, how many expressions are there?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22.22222222222222" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(72 - 32) / 9 * 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(72 - 32) / (9 * 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(72 - 32)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "72 - 32" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "32" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "32" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "72" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "72" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "40 / 45" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.13.2" } }, "nbformat": 4, "nbformat_minor": 2 }