class: big, middle # Engineering 1020: Introduction to Programming .title[ .lecture[Lecture \5\:] .title[Variables] ] .footer[[/lecture/5/](/lecture/5/)] --- # Previously: ## Expressions -- _Values_ and _operations_ that _evaluate_ to a _value_ --- # Values ### Literals * integer literals (e.g., `42`, `1_000_000`) * _floating-point_ (real-valued) literals (e.g., `3.14`, `1e6`) * _imaginary_ literals (e.g., `3.14j`) * _string_ literals (e.g., `'hello'`) * _Boolean_ (logical) literals (e.g., `True`) --- # Operations ### Arithmetic operators: `+`, `-`, `*`, `/`, `//`, `%`, `**` ### Function calls ### Logical operators: `and`, `or`, `not`, `^` (XOR) ### Comparators: `<`, `<=`, `>`, `>=`, `==`, `!=` --- # Operator precedence .floatleft[ | Operator | Descr. | |---------------|------------------| | `()` | Parens | `x(args...)` | Call | `**` | Expon. | `+x`, `-x`, `~x` | Unary | `*`, `/`, `//`, `%` | Mult. | `+`, `-` | Add. ] .floatright[ | Operator | Descr. | |---------------|------------------| | `<`, `<=`, `>`, `>=`, `!=`, `==` | Comp. | `not` | $ \neg $ | `and` | $ \wedge $ | `or` | $ \vee $ ] ??? A more complete order of operations (including operators that we will only get to later in the course) can be found here: | Operator | Description | |---------------|------------------| | `()` | Parentheses | `x[i]`, `x[i:j]`, `x(args...)`, `x.attribute` | Subscription, slicing, call, attribute reference | `**` | Exponentiation | `+x`, `-x`, `~x` | Unary positive, negative, bitwise not | `*`, `@`, `/`, `//`, `%` | Multiplication | `+`, `-` | Addition and subtraction | `<<`, `>>` | Shifts | `&` | Bitwise and | `^` | Bitwise xor | | | Bitwise or | `in`, `not in`, `<`, `<=`, `>`, `>=`, `!=`, `==` | Comparisons and membership tests | `not` | Boolean not | `and` | Boolean and | `or` | Boolean or | `if - else` | Conditional expression | `=` | Assignment The absolute full details can be seen at https://docs.python.org/3/reference/expressions.html#operator-precedence, but that includes some operators that we won't even get to in this course. --- class: big, middle # and now: -- .title[ .title[ ## Variables! ] ] -- .title[ .subtitle[ ### ... in mathematics and in programming, being in some ways similar and in others different ] ] --- # Variables -- ### We previously _used_ variables: -- ```python >>> from math import * >>> pi 3.141592653589793 >>> 2j * pi 6.283185307179586j ``` -- ```python angle*6*E*I/(x*(3*L**2-3*L*x+x**2)) (angle*(6*E*I))/((3*L**2-3*L*x+x**2)*x) (6*E*I*angle)/(x*((3*(L**2))-(3*L*x)+(x**2))) (angle*6*E*I)/(3*L*L*x)-(3*L*x*x)+(x*x*x) ``` --- # Making variables ### But where do variables _come_ from? -- ### First: what _are_ variables? -- * in mathematics -- * in programming --- # Mathematical variables ??? You should already have some familiarity with the concept of variables from mathematics. In math, we describe variables using "let" statements, e.g., $\textrm{let } x = \frac{2 \pi}{3}$. In this usage, **the name $x$ is a placeholder for the value $\frac{2\pi}{3}$**, i.e., wherever you see the variable $x$ you can substitute in the value $\frac{2\pi}{3}$ instead. If you wanted to express a changing value of $x$, you might use names like $x\prime$ or $x\prime\prime$, which are clearly related but are in fact **different names**: $x$ is not the same variable as $x \prime$. -- $$ \textrm{let } \Theta = \frac{\pi}{2} $$ -- * $\Theta$ is a _placeholder_ * $\Theta$ can be _substituted_ for $\frac{\pi}{2}$: -- $ \frac{\pi}{2} + \sin{\pi} $ -- $\Rightarrow$ $ \Theta + \sin{2\Theta} $ -- * Statement of truth: $x = 4$ is the same as $4 = x$ --- # Computer programming ??? In computer programming, variables are a related but **slightly different** concept. Variables in programming languages, like in mathematics are names that can be used to refer to values. Unlike mathematics, however, programming variables do not refer directly to values but rather than **places in memory that hold values**. For example, the image to the right depicts three integer values (42, 17 and 54) being held at three different locations in memory. It is worth noting a couple of things about these variables: 1. we refer to each one by a **name**, 2. we talk about each one in terms of a **type** (integer vs real vs ...), 3. each has a specific **size** in memory (which depends on the type) and 4. each has a defined **place** in memory. -- ### Variable: _place in memory_ that holds a _value_ -- ### A variable has: -- * a name -- * a stored value -- * which has a _type_ ??? Show the `type()` function -- * which can **change!** --- # Creating variables ### First (real) use of Python _statements_ -- * .footnote[ A Python file (or "script") is a series of statements that can be run one after the other. ] -- ```python n = 42 ``` ### This defines: -- * the _name_ of the variable -- (`n`) -- * the initial _value_ stored in the variable --- # _Initial_ value? -- ### Mathematically invalid: $$ \textrm{let } n = 1 $$ $$ \textrm{let } n = 2 $$ -- ### Programming variables can change value: ```python n = 1 n = 2 ``` --- # Exercises 1. In a Python interpreter, define four variables: * one containing an _integer_ * one containing a _floating-point_ number * one containing a _string_ * one containing a _Boolean_ value 2. Change their values; check their types with `type()` --- # Variables and types ### Strictly speaking: -- * **variables** don't have types -- , **values** do -- * when someone says, "the type of a variable", their more precise meaning is... -- the _type of its current value_ --- # Types... so far... ### Give two examples for each of: .centered[ | Type | Used for | |------------|----------| | `bool` | Boolean (true/false) values | `int` | "Whole" things | `float` | Real numbers | `complex` | Complex numbers | `str` | Names, arbitrary-length text ] --- # Type conversions ### We've see `type()` (which does what, again?) -- ### How about converting values to _different_ types? -- _Why_ would we want to do this? -- _How_ would we do this? ??? ### Demonstrate: * `type()` * `int()` * `round()` * `float()` * `str()` --- # Example ```python name = input('What is your name? ') quest = input('What is your quest? ') v = input('What is the average airspeed velocity of an unladen swallow? ') ``` -- How long will it take the swallow to fly 792 m? ??? Show error from `792 / v` without type conversion -- ## Type conversions are very useful! --- # Monotony and tedium ### Speaking of useful... -- * re-writing the same statements over and over is tedious -- * programming's supposed to make life _less_ tedious! -- * enter the _script_ mode of programming ??? A Python script is named this way because **Demo** how to create and run a script with IDLE and Thonny. --- # Python scripts ```python # Copyright (c) 2020 Jonathan Anderson # Permission is granted to copy and modify this code for any purpose. # # This is an example of a simple Python script for questing. # First, gather some basic information from the user. name = input('What is your name? ') quest = input('What is your quest? ') v = input('What is the average airspeed of an unladen swallow? ') # Greet the brave dame or knight. print(f'Greetings, brave {name}!') # Convert speed into a floating-point number and calculate quest time. v = float(v) print(f'Your quest for {quest} will be aided when the swallow flies 792 m.') print(f'This should take {792 / v} s.') ``` ??? What can we see in this file (which is available as [python.py](python.py))? * comments * copyright * type conversion * variables --- # What we just saw -- ### Comments \# Helpful descriptive text -- for **people**, not the computer! -- \# Everything from `#` to the end of the line -- ### Copyright -- * writing code is a _creative act_ -- * more like an English assignment than Math! --- # What's next? ### Write a Python script! * take some input, compute something, produce output -- * good preparation for exercise 2 --- class: big, middle (here endeth the lesson)