{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "format: html\n", "execute: \n", " freeze: auto\n", " cache: true\n", "toc: true\n", "---\n", "\n", "# Variables and Numbers \n", "\n", "[Jupyter Notebook](https://lancejnelson.github.io/PH135/jupyter/variables.ipynb)\n", "\n", "## Variables\n", "\n", "When performing mathematical operations, it is often desirable to store the values in *variables* for later use instead of manually typing them back in each time you need to use them. This will reduce effort because small changes to variables can automatically propagate through your calculations. \n", "\n", "Attaching a value to a variable is called *assignment* and is performed using the equal sign (=), as demonstrated in the cell below:" ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| eval: true\n", "#| echo: true\n", "\n", "a = 5.0\n", "b = 3\n", "c = a + b" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Variable naming convention\n", "\n", "There are some rules for allowed variable names in Python. They are as follows:\n", "\n", "1. Variable names must begin with a letter or an underscore (`_`)\n", "2. Variables names must only contain letters, numbers, and underscores.\n", "3. Variable names cannot contain spaces.\n", "4. Variables names cannot be a word reserved by Python for something else. These words are:\n", "\n", "| | Python | reserved |words | | \n", "|-----|-----|-----|------|--------|\n", "|and | as |assert | break | class |\n", "|continue | def |del | elif | else |\n", "|except | False |finally | for | from |\n", "|global | if |import | in | is |\n", "|lambda | None |nonlocal | not | or |\n", "|pass | raise |return | True | try |\n", "|why | with |yield | | |\n", "\n", "\n", "The cell below contains some allowed variable names and some that are not allowed.\n", "\n", "\n", ">**_To Do:_**\n", ">\n", ">1. Determine which variable names are allowed and which are not in the cell below.\n", ">2. What does Python do if you try to define a variable using a name that is not allowed?" ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| echo: true\n", "#| eval: false\n", "my1variable = 3\n", "1stvariables = 2\n", "a big constant = 3\n", "a_big_constant = 1e8" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also a good practice to make variable names meaningful. For example, in the cell below we calculate $E = mc^2$ using two choices for variable assignments. In one case, it is easy to determine what the calculation is and in the other it isn't." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Good Variable Names\n", "mass_kg = 1.6\n", "light_speed = 3.0e8\n", "energy = mass_kg * light_speed**2\n", "\n", "\n", "# Poor Variable Names\n", "a = 1.6\n", "b = 3.0e8\n", "c = a * b**2\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numbers: Integers and Floats\n", "\n", "There are two types of numbers in Python - floats and integers. *Floats*, short for \"floating point numbers,\" are values with decimals in them. They may be either whole or non-whole numbers such as 3.0 or 1.2, but there is always a decimal point. *Integers* are whole numbers with no decimal point such as 2 or 53.\n", "\n", "Mathematical operations that only use integers *and* evaluate to a whole number will generate an integers (except for division). All other situations will generate a float. See the example cell below.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "a = 24\n", "b = 6\n", "d = 0.3\n", "e = a + b # Produces an integer.\n", "f = a + d # Produces a float\n", "g = a * b # Produces a ???\n", "h = a / b # Produces a ???" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">**_To Do:_**\n", ">\n", ">1. For each of the mathematical operations above guess what type of number the result will be.\n", ">2. Use `print` statements to verify your guesses and formulate a general rule that you can rely on. (Tip, the `type()` function will tell you what kind of number a variable is.)" ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Integers and floats can be inter-converted to each other using the `int()` and `float()` functions." ] }, { "cell_type": "code", "metadata": {}, "source": [ "int(3.0)\n", "float(4)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The distinction between floats and ints is often a minor detail. Occasionally, a function will require that an argument be a float or an int but usually you won't have to worry about which one you use.\n", "\n", "Below you will find some other common mathematical operations that can be performed on numerical variables. \n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| echo: true\n", "#| eval: false\n", "\n", "a = 20\n", "b = 10\n", "c = a + b \n", "d = a/b \n", "r = a//b\n", "r = a % b\n", "e = a * b\n", "f = c**4" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">**To Do:**\n", ">\n", ">1. Use print statements to investigate what each operation does. \n", ">2. Guess what type of number you expect the result to produce (int or float) and then check yourself?\n", ">3. Add comments next to each line (Use `#` to start a comment) explaining that operation. " ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Augmented Assignment\n", "\n", "Augmented assignment is a shortened way to make a simple modification to a variable. For example, if we want to increase the value of a variable by 10, one way to do it would be like this." ] }, { "cell_type": "code", "metadata": {}, "source": [ "a = 5\n", "a = a + 10" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is certainly not difficult, but it does involve typing the variable twice which becomes cumbersome as your variable name gets longer. Alternatively, we can accomplish the same thing with the `+=` operator." ] }, { "cell_type": "code", "metadata": {}, "source": [ "a = 5\n", "a += 10" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Augmented assignment can be used with addition, subtraction, multiplication, and division as shown in the code cell below.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "a = 7\n", "a += 3\n", "a -= 1\n", "a *= 4\n", "a /= 3" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">**_To Do:_**\n", ">\n", ">1. Predict what the final result of `a` will be in the code cell above.\n", ">2. Add an appropriately-place `print` statement to see if you were correct.\n", ">3. If you were wrong, pow-wow with your neighbor until you understand." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compound Assignment\n", "\n", "At the beginning of a program or calculation, it is often necessary to define a set of variables. Each variable may get it's own line of code, but if there are a lot of variables, this can begin to clutter your code a little. An alternative is to assign multiple variables on a single line. In the code below, we assign the atomic mass of the first three elements." ] }, { "cell_type": "code", "metadata": {}, "source": [ "H, He, Li = 1.01, 4.00, 5.39" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">**_To Do:_**\n", ">\n", ">1. Use print statements to verify that each variable was assigned it's own value.\n", ">2. Add assignments for the atomic masses of the next three elements on the periodic table." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Large numbers\n", "\n", "Sometimes you find yourself working with large numbers in your calculation. Maybe your calculation involves the use of ten billion, which has 10 zeros in it. It can be difficult to look at all of those zeros with no commas to help break it up. In those cases, you can use an underscore (`_`) in place of the comma, as shown below. " ] }, { "cell_type": "code", "metadata": {}, "source": [ "myLargeNumber = 10000000000 # This is tough to look at.\n", "myLargeNumber = 10_000_000_000 # This is easy to read\n", "\n", "myLargeFloat = 5000000.6 # This is tough to read\n", "myLargeFloat = 5_000_000.6 # This is easy to read" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Very Large Numbers\n", "If your number is very large or very small ( $20-30$ zeros), you would probably rather not have to type all of the zeros at all, even if you can break it up with the underscores. For example, the Boltzmann constant, which comes up in thermodynamics, has a value equal to \n", "\n", "$$ 1.38 \\times 10^{-23}$$ \n", "\n", "We can avoid typing all those zeros by using scientific notation when defining the variable. (see example below) This is super handy for very large and very small numbers. (Numbers of both variety show up frequently in physics!)" ] }, { "cell_type": "code", "metadata": {}, "source": [ "kB = 1.38e-23" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python functions\n", "\n", "In addition to basic mathematical functions, python contains several mathematical *functions*. As in mathematics, a function has a name (e.g. *f*) and the arguments are places inside of the parenthesis after the name. The *argument* is any value or piece of information fed into the function. In the case below, *f* requires a single argument *x*.\n", "$$f(x)$$\n", "\n", "In the cell below, you will find several useful Python functions." ] }, { "cell_type": "code", "metadata": {}, "source": [ "abs(-5.5)\n", "float(2)\n", "int(5.6)\n", "print(1.26e-6)\n", "round(-5.51)\n", "str(3.2)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition to Python's native collection of mathematical functions, there is also a `math` module with more mathematical functions. Think of a module as an add-on or tool pack for Python just like a library. The `math` module comes with every installation of python and can be *imported* (i.e. activated) using the `import math` command. After the module has been imported, any function in the module is called using `math.function()` where `function` is the name of the function. Below is a list of commonly-used functions inside the `math module`. Carefully look at each of them and guess what they mean." ] }, { "cell_type": "code", "metadata": {}, "source": [ "import math\n", "math.sqrt(4)\n", "math.ceil(4.3)\n", "math.cos(1.5)\n", "math.sin(1.5)\n", "math.tan(3.14)\n", "math.asin(1)\n", "math.acos(1/2)\n", "math.atan(2)\n", "math.degrees(6.28)\n", "math.e\n", "math.exp(5)\n", "math.factorial(4)\n", "math.log(200)\n", "math.log10(1000)\n", "math.radians(360)\n", "math.pi\n", "math.pow(2,8)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">**_To Do:_**\n", ">\n", ">1. Use print statements to figure out what each function in the code cell above does. Pay special attention to trigonometric function. Do these functions expect the argument to be in radians or degrees? \n", ">2. Add comments to remind yourself for later." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are other ways to import functions from modules. If you only want to use a single function inside the module, you can selectively import it using `from`, as shown below." ] }, { "cell_type": "code", "metadata": {}, "source": [ "from math import radians\n", "radians(4)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Errors In Python \n", "When you are learning to write code, you will inevitably run into errors. Seeing a block of red text can be intimidating at first, but it's important to remember that errors are not failures; they are helpful signs. The error message is simply Python's way of telling you that it couldn't understand your instructions and often gives you a very specific clue about what went wrong.\n", "\n", "Learning to read these messages is a fundamental skill that will turn you into a more effective programmer. Let's look at some of the most common errors you will encounter on your journey.\n", "\n", "\n", "\n", "\n", "### Syntax Error\n", "\n", "A `SyntaxError` is the most common type of error for beginners. It means you have broken a grammar rule of the Python language. Just like a sentence in English needs correct punctuation to make sense, a line of Python code needs correct syntax.\n", "\n", "\\textbf{Common Cause}: Missing punctuation, like a colon :, a parenthesis ), or a quotation mark \".\n", "\n", "In the example below, we forget to close the parentheses for the print function." ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| error: true\n", "\n", "# We are trying to print a message, but we forgot the closing parenthesis.\n", "print(\"This line has a syntax error\"" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indentation Error\n", "Python is unique because it uses whitespace (specifically, the spaces at the beginning of a line) to group code together. An `IndentationError` means that the spacing in your code is not correct or consistent.\n", "\n", "\\textbf{Common Cause}: Forgetting to indent code that belongs inside a loop, function, or if statement.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| error: true\n", "\n", "# The line below should be indented, because it belongs inside the for loop.\n", "for i in range(5):\n", "print(i)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also get an `IndentationError` if there are multiple lines indented but their indentation levels are not consistent, as shown below.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| error: true\n", "\n", "# Indentation error because the lines in the loop are not all indented\n", "# to the same position\n", "for i in range(10):\n", " print(i)\n", " print(i**2)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### NameError\n", "A `NameError` occurs when you try to use a variable or function that Python doesn't recognize.\n", "\n", "\\textbf{Common Cause}: A typo in a variable name or trying to use a variable before you have assigned it a value." ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| error: true\n", "\n", "# We create a variable called 'initial_velocity'.\n", "initial_velocity = 20\n", "\n", "# But then we make a typo when trying to print it. Python doesn't know what 'initial_velocite' is.\n", "print(initial_velocite)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since Python executes code sequentially: the first line gets executed first, then the second line, etc., you can get a `NameError` if you try to use a variable before it is defined, as shown below." ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| error: true\n", "dx = 5.22\n", "\n", "v = dx/dt # A NameError occurs at this line because dt is not yet defined.\n", "\n", "dt = 0.4\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### TypeError\n", "A `TypeError` means you are trying to do an operation with a data type that doesn't support it. It's like trying to add a word to a number—it just doesn't make sense.\n", "\n", "\\textbf{Common Cause}: Trying to combine incompatible types, like adding a string and an integer.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| error: true\n", "\n", "# You can't directly add a number (an integer) to a word (a string).\n", "# Python doesn't know if you mean to do math or combine text.\n", "message = \"The answer is: \" + 5" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### IndexError\n", "An `IndexError` happens when you try to access an item in a list using an index that is out of bounds.\n", "\n", "\\textbf{Common Cause}: Forgetting that list indices start at 0. For a list of 3 items, the only valid indices are 0, 1, and 2." ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| error: true\n", "\n", "# This list has three items.\n", "planets = [\"Mercury\", \"Venus\", \"Earth\"]\n", "\n", "# The valid indices are 0, 1, and 2.\n", "# By asking for index 3, we are asking for a fourth item that doesn't exist.\n", "print(planets[3])" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### FileNotFoundError\n", "This is a very common error when you start working with data. It simply means that you are trying to open a file that Python cannot find at the location you specified.\n", "\n", "\\textbf{Common Cause}: A typo in the filename or the file not being in the same folder as your script." ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| error: true\n", "\n", "# Python will look in the current folder for a file with this name.\n", "# If it can't find it, it will raise this error.\n", "with open(\"data_that_does_not_exist.csv\") as f:\n", " print(f.read())" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises\n", "\n", "1. Most (or all) of you have use Pythagorean's theorem to calculate the hypotenuse of a right triangle. The theorem states that the lengths of a right triangle are related like this $a^2 + b^2 = c^2$, where $a$ and $b$ are the lengths of the sides forming the right angle and $c$ is the length of the hypotenuse. There is a Python function called `hypot` (found in a library called `math`) that will perform this calculation for your. Calculate the distance from the point $(-48,56)$ to the point $(23,81)$ using \n", " 1. Pythogorean's theorem\n", " 2. The Python function `hypot`. To learn how to use the hypot function, visit the following [documentation page](https://docs.python.org/3/library/math.html) and search for the explanation for the `hypot` function. Learning to read and understand online documentation is a skill that you you should develop.\n", " \n", "Print out the result and check your answer with your neighbor's answer." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Equations involing quadratic polynomials,like the one shown below, appear in science frequently. \n", "\n", "$$ 5x^2 + 2x - 1 = 0$$\n", "\n", "You may remember that the solution to this equation is given by the quadratic formula\n", "$$ x = {- b \\pm \\sqrt{b^2 - 4 a c} \\over 2a}$$\n", "Solve the quadratic equation given above and print off the results. (There are two answers.) Then check with a classmate to verify that your answers match." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. (Solar Mass) The mass of the sun can be calculated using the following formula:$$ M_\\text{sun} = {4 \\pi^2 (1 \\text{ AU})^3\\over G (1 \\text{ yr})^2}$$ \n", "The unit AU is called an astronomical unit of length and is defined to be the average distance between the sun and earth. $$ 1 \\text{ AU} = 1.58\\times 10^{-5} \\text{ light years}$$\n", "where $1 \\text{ lightyear} = 9.5 \\times 10^{15}$ m. The constant $G$ is called the gravitational constant and has the value: $$ G = 6.674 \\times 10^{-11} \\text{ m}^3 \\text{ kg}^{-1}\\text{ s}^{-1}$$ Calculate the mass of the sun (in kg) and display your result using a print statement. You should find a value of $M_\\text{Sun} = 2.01 \\times 10^{30}$ kg. " ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. (Projectile Motion) The range of a projectile is given by the equation:$$ d = {2 v^2 \\cos \\theta \\sin \\theta \\over g}$$\n", "or the equivalent expression:$$ d = {v^2 \\sin 2 \\theta \\over g}$$\n", "where $g = 9.8$ m/s$^2$, $\\theta$ is the launch angle, and $v$ is the launch speed.\n", "\n", " 1. Using a launch angle of 60$^\\circ$ and a launch speed of $40$ m/s, verify that both expressions above give the same result for the range.\n", " 2. Now pick one of the equations above and use trial and error to determine the angle that gives maximum range.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "v = 40\n", "g = 9.8\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. (Rydberg's constant) Rydberg's constant ($R_\\infty$) is a physical constant in Rydberg's formula which was used to predict the spectrum of light emitted by a heavy atom. Rydberg's constant is given by: $$R_\\infty = {m_e e^4 \\over 8 \\epsilon_0^2 h^3 c}$$ \n", "where \n", " - $m_e = 9.109 \\times 10^{-31}$ kg is the mass of an electron.\n", " - $e = 1.602 \\times 10^{-19}$ C is the charge of an electron/proton.\n", " - $\\epsilon_0 = 8.854 \\times 10^{-12}$ C V$^{-1}$ m$^{-1}$ is the electrical constant.\n", " - $h = 6.626 \\times 10^{-34}$ J Hz is the Planck constant.\n", " - $c = 3 \\times 10^8$ m/s is the speed of light.\n", "\n", " These constants show up all over in physics. In the cell below write some code that assigns the constants to variables and then use the variables to calculate Rydberg's constant. Use a print statement to display the result. The result should be: $R_\\infty = 10961656.2162$ (in m$^{-1}$)\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. In Einstein's special theory of relativity, the momentum of an object with mass $m$ (in kg) and velocity $v$ (in m/s) is given by:$$ p = m v \\gamma$$ where $$\\gamma = {1\\over \\sqrt{1 - {v^2\\over c^2}}}$$ with $c = 3 \\times 10^8$ m/s\n", "\n", " 1. Calculate the momentum of an object with mass $m = 0.14$ kg and speed $v = 50$ m/s. Then compare to the classical expression for momentum: $p = mv$. \n", " 2. Now calculate the momentum of an object with mass $m = 0.14$ kg and whose speed is ${1 \\over 4}$ the speed of light. Repeat the comparison to the classical value. \n", " 3. Repeat for the following speeds: ${1\\over 2}$ the speed of light, ${3\\over 4}$ the speed of light, and ${7\\over 8}$ the speed of light. Repeat the comparison to the classical value." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] } ], "metadata": { "kernelspec": { "name": "python3", "language": "python", "display_name": "Python 3 (ipykernel)", "path": "/Users/legoses/environments/lammps_env/share/jupyter/kernels/python3" } }, "nbformat": 4, "nbformat_minor": 4 }