{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables\n", "\n", "Variables are like boxes: you can store anything in them and then put a label on it. The label doesn't need to match the contents, but _it's really helpful if it does_." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We store a value into a variable with the `=` (single equals) operator. This creates a _statement_ that has an effect (to store a value in the \"box\" with the specified label)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "x = 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once a variable exists, we can use it as an expression (thing that evaluates to a value):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But the variable must exist first! Referencing a label for a box that hasn't been created yet generates an error:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'y' is not defined", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43my\u001b[49m\n", "\u001b[31mNameError\u001b[39m: name 'y' is not defined" ] } ], "source": [ "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variables will always hold the same value until they are re-assigned. Moreover, the expression on the right-hand side of the `=` is _fully evaluated_ at assignment time." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "x = 2 + 2 # stores 4, i.e. the result of the `2 + 2` expression" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do a number of things in the meantime, including using `x`..." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "y = x\n", "z = 65\n", "n = z + 5.4 / x\n", "m = 11.2 * x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... but x will remain unchanged." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11.555555555555555" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x * 2 + 32 / 9" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "cannot assign to literal here. Maybe you meant '==' instead of '='? (3901524068.py, line 1)", "output_type": "error", "traceback": [ " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[10]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31m22 = x\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m cannot assign to literal here. Maybe you meant '==' instead of '='?\n" ] } ], "source": [ "22 = x\n", "22 = 4" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 22\n", "x" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "pi = 3.14" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "avogadro = 6 \n", "avogadro" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "47" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = 25\n", "x_and_y = x + y\n", "x_and_y" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "123" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x123 = 123\n", "x123" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.88" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x/y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Go to this code in PythonTutor. What's the output?\n", "\n", "https://dub.sh/DacUWD3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Aside: `print`\n", "\n", "In the Python Interpreter (or the last line of code in a notebook cell), the value of expressions is automatically shown. Often, we need to be more explicit about when values are shown in our terminal or on screen. This is where `print` comes in." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Hello World!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"Hello World!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is especially helpful when seeing intermediate values in a short program:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "9\n", "9\n" ] } ], "source": [ "x = 2 + 4\n", "print(x)\n", "y = 3 + x\n", "print(y)\n", "x = y\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "x = 2 + 4\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n" ] } ], "source": [ "print(\"Hello\")" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "x = print(42) " ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Boolean Expressions\n", "\n", "So if we have variables that can have arbitrary values, then we might at some point need a way to determine a variable's stored value without changing it. This is where _boolean expressions_ come in." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 2\n", "x < 5" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x > 5" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "22 >= 22" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 22 == 22" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not x < 5 or x == 2" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False or False" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False and True" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n" ] } ], "source": [ "# What's printed out here?\n", "temp = 72\n", "is_liquid = temp > 32 and temp < 212\n", "print(is_liquid)\n", "temp = 300\n", "print(is_liquid)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings\n", "\n", "Strings are a type of value that can hold arbitrary letters, symbols, numbers, etc. They are denoted by paired single (`'`) or double (`\"`) quotes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name = \"Alessia\"\n", "name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "thing_one = \"\"\n", "thing_two = 0\n", "thing_three = print(\"Hello\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "thing_one == thing_two == thing_three" ] } ], "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 }