{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Numbers and Variables\n", "\n", "In this lecture, we'll introduce basic numerical operations in Python. We'll discuss: \n", "\n", "- Python's representations of numbers\n", "- Operations on numbers\n", "- Identity and equality of variables. \n", "\n", "### Reading\n", "\n", "- The Python Tutorial, 3.1-3.3: [An Informal Introduction to Python](https://docs.python.org/3.7/tutorial/introduction.html)\n", "- [Download the lecture notes](https://philchodrow.github.io/PIC16A/content/basics/numbers.ipynb). \n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python as a Calculator\n", "\n", "Because Python is an interactive language, it's a very practical substitute for a calculator. Let's try a few simple computations. \n", "\n", "The simplest approach is to type in the formula for a computation you would like to do. Here are a few examples using integers: " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "33" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "27+6" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3*(2+3)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "36/12 # (division converts integers into floating point numbers, i.e. decimals)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.9166666666666665" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "35/12" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "37 % 12 # remainder after division" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**3 # python uses x**y to denote \"x to the power y\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most of the same operations work with floating point numbers. However, surprises are possible. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "32.7" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "27.3 + 5.4" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14.515932837559118" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.2**2.3" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8000000000000007" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "37.4 % 12.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wait. Uh, what? \n", "\n", "Recall that `%` refers to the remainder after division. In this case, 12.2 \"goes into\" 37.4 three times. Mathematically, the remainder is `37.4 - 3*(12.2) = 37.4 - 36.6 = 0.8`. \n", "\n", "Just like in C++, Python's implementation of floating point numbers (i.e. decimals) has *finite precision.* That means that most numbers cannot be represented exactly. the result above reflects this fact: Python cannot represent `0.8` exactly, so it uses a close approximation in floating point representation. \n", "\n", "As you might expect, operations like taking the square root of `-1` yield complex numbers. " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6.123233995736766e-17+1j)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(-1)**(0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables\n", "\n", "When writing code, it's almost always best to give names to important numbers. You can then use those names in subsequent computations. These names are called *variables.* " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "32.7" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 27.3\n", "y = 5.4\n", "\n", "x + y\n", "# same as writing 27.3 + 5.4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This code block creates variables `x` and `y` with values 27.3 and 5.4 respectively, and then prints their sum. \n", "\n", "You can also assign names to the outputs of computations: " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "32.7" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = x + y\n", "z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Identity and Equality of Variables\n", "\n", "When are two variables \"the same\"? It's easy to check the `id` of a variable in Python -- this indicates the location in memory to which the name of the variable points. We can also use the `is` binary operator to check whether two variables point to the same memory location. The `is` operator should not be confused with `==`. The `==` operator checks whether two variables have the same value, regardless of whether they refer to the same object. \n", "\n", "To clarify the difference, consider the following scenarios: \n", "\n", "1. You and I have each laid claim to the same stack of four cookies. Then, `your_stack` and `my_stack` are both variables with value 4. Thus, `your_stack == my_stack` (they have the same value). Because each variable refers to the *same stack of cookies*, it also holds that `your_stack is my_stack`. \n", "2. On the other hand, suppose that we *each* have a stack of four cookies. Then, `your_stack` and `my_stack` are again variables of value 4, and `your_stack == my_stack`. However, these two variables now refer to different stacks of cookies, and so it is **not** the that case `your_stack is my_stack`. \n", "\n", "Variables whose values are immutable (cannot be changed) often share the same memory. Integers, floats, and strings are examples of immutable types in Python. " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 4\n", "y = 4\n", "\n", "x == y" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4504444688, 4504444688)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(x), id(y)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x is y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On the other hand, mutable variables like lists will generally not share memory (we haven't formally introduced lists yet -- all you need to know for now is that they are data structures can be altered). " ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "a = [1 , 2]\n", "b = [1 , 2]\n", "c = a" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4537059912, 4538546184)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a), id(b)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(False, True)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b, a == b" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4537059912" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(c)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is c" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7.3 64-bit", "language": "python", "name": "python37364bitff68f03e0729439b934cf550182ecbd9" }, "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }