class: big, middle # Engineering 1020: Introduction to Programming .title[ .lecture[Lecture 1:] .title[Expressions] ] .footer[[/lecture/1/](/lecture/1/)] --- # Last time ### Introduction to Introduction to Programming <img src="../images/computer.png" alt="Computer structure" width="400" class="floatright"/> ### Knowledge and Computation --- # A simple algorithm e.g., find the sum of the set $S = \{4, 7, 2, 11, 5, 8, 1\}$ ### Mathematically: $$\sum\_{i=1}^n S\_{1..n}$$ ### Algorithmically? (do this as an exercise) ??? ### Suggested methodology: * work out an example or two * break it into steps * explain the steps to someone else (pretend they're a computer) --- # Software > Description of instructions for a computer #### You express meaning using a _programming language_ .floatleft[ **Python:** ```python y = x / 2 if (2 * y == x): print(x, 'is even') ``` ] .floatleft[ **C++:** ```cpp int y = x / 2; if (2 * y == x) cout << x << " is even\n"; ``` ] **Also:** Assembly, C, C#, Go, Java, Matlab, Perl, R, Rust, Scala, SPIN... #### Your code is translated into _machine instructions_ ??? What is software? ### Softer than hardware? ### There's also "firmware", but... Software, whether written in C++, Java, Python or another programming language, is a way of **describing algorithms to a computer**. Consider the following implementations of the algorithm from above that checks whether or not a number is even. You should note that these two code snippets look a bit different, as the details of the two programming languages are different, but the **fundamentals of algorithms are the same**. In both the Python and C++ examples, a program has to translate the programmer-readable _source code_ into computer-readable _machine code_ for the CPU to execute. This process will happen mostly transparently to us as we work with Python via [online Python environments]({{% relref "/resources/tools/online" %}}) or [_integrated development environments_ (IDEs)]({{% relref "/resources/tools/ides" %}}). I have provided information about getting started with Python tools on the [tools page]({{< relref "/resources/tools" >}}). --- # Programming languages -- ### vs natural languages * like natural languages * unlike natural languages ??? Like natural languages, a medium for expressing semantics Unlike natural languages, highly constrained (more like math). Allows succinct yet powerful constructions. -- ### Syntax and semantics -- * _syntax_: rules of **well-formed** language -- * _semantics_: the **meaning** of it all --- # Write some software ## Yes, right now! -- 1. Think about a problem -- , e.g., what is $ 1 + 2 \times 3 - 4$? -- 2. Compute an answer -- 2. Check your answer with Python Type `1 + 2 * 3 - 4` into [pythonmorsels.com/repl](https://www.pythonmorsels.com/repl), then press Enter --- # What did you just do? -- ### Wrote an _expression_ -- ### Expression was _evaluated_ -- ## What is an expression? ### Algebra: -- values, operators that **evaluate** -- to a **value** -- ### Programming: -- the same! -- (even operator precedence) --- # Exercise 0 ### Submit a Python expression that evaluates to 42 .floatleft[ ### Not: ```python Python 3.9.1 >>> x = 21 >>> y = 21 >>> x + y 42 ``` ] .floatleft[ ### Or: ```python x = 21 y = 21 print(x + y) ``` ] .floatleft[ ### Just: ```python 21 + 21 ``` ] .floatleft[ ### Or even: ```python 42 ``` ] .floatright[ #### See: ["Resources" > "Tools"](../../resources/tools) ] --- # Expressions ### **Values** and **operations** that **evaluate** to a **value** -- ### Let's consider each of these words in turn --- layout: true .footnote[ Expression: _**values** and **operations** that **evaluate** to a **value**_ ] --- # Values -- ### For now, just one kind: -- ## A _literal_ value --- # Literals ### Literally mean what they literally say -- * `42` : an integer literal -- * `3.14` : a real-number (_floating-point_) literal -- * `'hello'` : a _string_ literal -- * `True` : a logical (_Boolean_) literal --- # Integer literals -- ### `1`, `2`, `42`... (ok, so like math!) -- ### `1_000_000` (ok, so a bit like math...) ??? You can use underscores in the middle of a Python integer literal to help group numbers and keep things clear. These underscores can go **anywhere**: they're not tied to thousands, so be careful! (e.g., `1_00_000` looks a lot like `1_000_000`, but its meaning is quite different) -- ### `0b10`, `0o10`, `0x10` (what!?) ??? We'll come back to what these different ways of writing integers mean when we get to talking about how numbers are represented. For now, just know that there are lots of ways to write integers! (exercise for the keen: what do these "funny" integer literals evaluate to?) --- # Floating-point literals ### `3.0` — is this the same as `3`? -- ### `3.1` — ok, definitely not the same as `3` -- ### `3.1415927` — _definitely_ not the same as `3` -- ([Professor Frink notwithstanding](https://www.youtube.com/watch?v=L1eegVTwDS0)) -- ### Scientific notation: `3.14e0`, `1e100`... --- # Imaginary literals -- ### With integer prefix: `1j`, `2j`, ... -- ### With floating-point prefix: `1.0j`, `1.1j`... -- ### Not _complex_ literals, _imaginary_ literals -- * `1+2j` is actually an _expression_ --- # Variables -- ### Named values -- _Actually, it's slightly more complicated than that, but..._ ??? We'll talk more about variables in later lectures when we talk about how to **create** them. For now we will just focus on **using** them. -- ```python >>> from math import * >>> pi 3.141592653589793 >>> 2j * pi 6.283185307179586j ``` -- ```python r1 + r2 ``` --- layout: false # Expressions ### **Values** and **operations** that **evaluate** to a **value** -- ### ~~Values~~ ✔ * ~~literals~~ ✔ * ~~variables~~ ✔ -- ### Operations --- # Operations ### Starting with arithmetic operators: -- .centered[ | Symbol | Meaning | Usage | Math | |:------:|-----------------|:----------:|:-------:| | `+` | addition | `1 + 2` | $1 + 2$ | `-` | subtraction | `3 - 4` | $3 - 4$ | `*` | multiplication | `5 * 6` | $5 \times 6$ | `/` | division | `7 / 8` | $7 \div 8$ or $\frac{7}{8}$ ] --- # Evaluation: precedence .floatright[ | Operation | Kind | |:---------:|-----------------| | `()` | parenthetical | `*`, `/` | multiplicative | `+`, `-` | additive ] ### _Order of operations_ matters ### Just like math! -- (for now) ??? Top Hat question: Order of Operations (literals only) --- # Division operator -- ### $ x \div y $ -- or $y \overline{)x}$ -- ### Integers and real (_floating-point_) numbers ??? We can perform division on **integers** and **real numbers**. It doesn't make sense to divide, say, a string and an integer. Syntactically, `x / y` is valid, but it could be semantically nonsense. ### Q: what is $7 \div 2$? -- ```python >>> 7 / 2 3.5 ``` ??? ### How about in long division? When performing long division, we will often leave the result as 3 with a remainder of 1. -- ```python >>> 7 // 2 3 >>> 7 % 2 1 ``` --- # Exponentiation ??? ### Q: what's your favourite equation? -- #### Euler's identity: $e^{j \pi} = -1$ -- † .footnote[ † Since $e^{jx} = \cos x + j \sin x$... but that's for another course! ] -- ### In Python: the `**` operator -- ### $ x^y $ in Python is `x**y` -- ```python >>> from math import * >>> e ** (1j * pi) (-1+1.2246467991473532e-16j) ``` ??? **Note** that the answer isn't precisely -1: it's $-1 + 1.22646\times10^{-16}j$. This is due to the way that floating-point numbers are represented in a computer, and it's emblematic of the **limits of computation**. --- class: big # Questions? --- class: big, middle (here endeth the lesson)