+ - 0:00:00
Notes for current slide
Notes for next slide

Engineering 1020: Introduction to Programming

Lecture 1: Expressions

/lecture/1/

1 / 22

Last time

Introduction to Introduction to Programming

Computer structure

Knowledge and Computation

2 / 22

A simple algorithm

e.g., find the sum of the set S=4,7,2,11,5,8,1

Mathematically:

i=1nS1..n

Algorithmically?

(do this as an exercise)

3 / 22

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

Python:

y = x / 2
if (2 * y == x):
print(x, 'is even')

C++:

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

4 / 22

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 or integrated development environments (IDEs). I have provided information about getting started with Python tools on the tools page.

Programming languages

5 / 22

Programming languages

vs natural languages

  • like natural languages
  • unlike natural languages
5 / 22

Like natural languages, a medium for expressing semantics

Unlike natural languages, highly constrained (more like math). Allows succinct yet powerful constructions.

Programming languages

vs natural languages

  • like natural languages
  • unlike natural languages

Syntax and semantics

5 / 22

Like natural languages, a medium for expressing semantics

Unlike natural languages, highly constrained (more like math). Allows succinct yet powerful constructions.

Programming languages

vs natural languages

  • like natural languages
  • unlike natural languages

Syntax and semantics

  • syntax: rules of well-formed language
5 / 22

Like natural languages, a medium for expressing semantics

Unlike natural languages, highly constrained (more like math). Allows succinct yet powerful constructions.

Programming languages

vs natural languages

  • like natural languages
  • unlike natural languages

Syntax and semantics

  • syntax: rules of well-formed language
  • semantics: the meaning of it all
5 / 22

Like natural languages, a medium for expressing semantics

Unlike natural languages, highly constrained (more like math). Allows succinct yet powerful constructions.

Write some software

Yes, right now!

6 / 22

Write some software

Yes, right now!

  1. Think about a problem
6 / 22

Write some software

Yes, right now!

  1. Think about a problem, e.g., what is 1+2×34?
6 / 22

Write some software

Yes, right now!

  1. Think about a problem, e.g., what is 1+2×34?
  2. Compute an answer
6 / 22

Write some software

Yes, right now!

  1. Think about a problem, e.g., what is 1+2×34?
  2. Compute an answer
  3. Check your answer with Python

Type 1 + 2 * 3 - 4 into pythonmorsels.com/repl, then press Enter

6 / 22

What did you just do?

7 / 22

What did you just do?

Wrote an expression

7 / 22

What did you just do?

Wrote an expression

Expression was evaluated

7 / 22

What did you just do?

Wrote an expression

Expression was evaluated

What is an expression?

Algebra:

7 / 22

What did you just do?

Wrote an expression

Expression was evaluated

What is an expression?

Algebra: values, operators that evaluate

7 / 22

What did you just do?

Wrote an expression

Expression was evaluated

What is an expression?

Algebra: values, operators that evaluate to a value

7 / 22

What did you just do?

Wrote an expression

Expression was evaluated

What is an expression?

Algebra: values, operators that evaluate to a value

Programming:

7 / 22

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!

7 / 22

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)

7 / 22

Exercise 0

Submit a Python expression that evaluates to 42

Not:

Python 3.9.1
>>> x = 21
>>> y = 21
>>> x + y
42

Or:

x = 21
y = 21
print(x + y)

Just:

21 + 21

Or even:

42
8 / 22

Expressions

Values and operations that evaluate to a value

9 / 22

Expressions

Values and operations that evaluate to a value

Let's consider each of these words in turn

9 / 22

Expression: values and operations that evaluate to a value

Values

10 / 22

Expression: values and operations that evaluate to a value

Values

For now, just one kind:

10 / 22

Expression: values and operations that evaluate to a value

Values

For now, just one kind:

A literal value

10 / 22

Expression: values and operations that evaluate to a value

Literals

Literally mean what they literally say

11 / 22

Expression: values and operations that evaluate to a value

Literals

Literally mean what they literally say

  • 42 : an integer literal
11 / 22

Expression: values and operations that evaluate to a value

Literals

Literally mean what they literally say

  • 42 : an integer literal
  • 3.14 : a real-number (floating-point) literal
11 / 22

Expression: values and operations that evaluate to a value

Literals

Literally mean what they literally say

  • 42 : an integer literal
  • 3.14 : a real-number (floating-point) literal
  • 'hello' : a string literal
11 / 22

Expression: values and operations that evaluate to a 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
11 / 22

Expression: values and operations that evaluate to a value

Integer literals

12 / 22

Expression: values and operations that evaluate to a value

Integer literals

1, 2, 42... (ok, so like math!)

12 / 22

Expression: values and operations that evaluate to a value

Integer literals

1, 2, 42... (ok, so like math!)

1_000_000 (ok, so a bit like math...)

12 / 22

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)

Expression: values and operations that evaluate to a value

Integer literals

1, 2, 42... (ok, so like math!)

1_000_000 (ok, so a bit like math...)

0b10, 0o10, 0x10 (what!?)

12 / 22

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)

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?)

Expression: values and operations that evaluate to a value

Floating-point literals

3.0 — is this the same as 3?

13 / 22

Expression: values and operations that evaluate to a value

Floating-point literals

3.0 — is this the same as 3?

3.1 — ok, definitely not the same as 3

13 / 22

Expression: values and operations that evaluate to a value

Floating-point literals

3.0 — is this the same as 3?

3.1 — ok, definitely not the same as 3

3.1415927definitely not the same as 3

13 / 22

Expression: values and operations that evaluate to a value

Floating-point literals

3.0 — is this the same as 3?

3.1 — ok, definitely not the same as 3

3.1415927definitely not the same as 3

(Professor Frink notwithstanding)

13 / 22

Expression: values and operations that evaluate to a value

Floating-point literals

3.0 — is this the same as 3?

3.1 — ok, definitely not the same as 3

3.1415927definitely not the same as 3

(Professor Frink notwithstanding)

Scientific notation: 3.14e0, 1e100...

13 / 22

Expression: values and operations that evaluate to a value

Imaginary literals

14 / 22

Expression: values and operations that evaluate to a value

Imaginary literals

With integer prefix: 1j, 2j, ...

14 / 22

Expression: values and operations that evaluate to a value

Imaginary literals

With integer prefix: 1j, 2j, ...

With floating-point prefix: 1.0j, 1.1j...

14 / 22

Expression: values and operations that evaluate to a value

Imaginary literals

With integer prefix: 1j, 2j, ...

With floating-point prefix: 1.0j, 1.1j...

Not complex literals, imaginary literals

14 / 22

Expression: values and operations that evaluate to a value

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
14 / 22

Expression: values and operations that evaluate to a value

Variables

15 / 22

Expression: values and operations that evaluate to a value

Variables

Named values

15 / 22

Expression: values and operations that evaluate to a value

Variables

Named values

Actually, it's slightly more complicated than that, but...

15 / 22

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.

Expression: values and operations that evaluate to a value

Variables

Named values

Actually, it's slightly more complicated than that, but...

>>> from math import *
>>> pi
3.141592653589793
>>> 2j * pi
6.283185307179586j
15 / 22

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.

Expression: values and operations that evaluate to a value

Variables

Named values

Actually, it's slightly more complicated than that, but...

>>> from math import *
>>> pi
3.141592653589793
>>> 2j * pi
6.283185307179586j
r1 + r2
15 / 22

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.

Expressions

Values and operations that evaluate to a value

16 / 22

Expressions

Values and operations that evaluate to a value

Values

  • literals
  • variables
16 / 22

Expressions

Values and operations that evaluate to a value

Values

  • literals
  • variables

Operations

16 / 22

Operations

Starting with arithmetic operators:

17 / 22

Operations

Starting with arithmetic operators:

Symbol Meaning Usage Math
+ addition 1 + 2 1+2
- subtraction 3 - 4 34
* multiplication 5 * 6 5×6
/ division 7 / 8 7÷8 or 78
17 / 22

Evaluation: precedence

Operation Kind
() parenthetical
*, / multiplicative
+, - additive

Order of operations matters

Just like math!

18 / 22

Evaluation: precedence

Operation Kind
() parenthetical
*, / multiplicative
+, - additive

Order of operations matters

Just like math! (for now)

18 / 22

Top Hat question: Order of Operations (literals only)

Division operator

19 / 22

Division operator

x÷y

19 / 22

Division operator

x÷y or y)x

19 / 22

Division operator

x÷y or y)x

Integers and real (floating-point) numbers

19 / 22

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÷2?

Division operator

x÷y or y)x

Integers and real (floating-point) numbers

>>> 7 / 2
3.5
19 / 22

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÷2?

How about in long division?

When performing long division, we will often leave the result as 3 with a remainder of 1.

Division operator

x÷y or y)x

Integers and real (floating-point) numbers

>>> 7 / 2
3.5
>>> 7 // 2
3
>>> 7 % 2
1
19 / 22

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÷2?

How about in long division?

When performing long division, we will often leave the result as 3 with a remainder of 1.

Exponentiation

20 / 22

Q: what's your favourite equation?

Exponentiation

Euler's identity: ejπ=1

20 / 22

Q: what's your favourite equation?

Exponentiation

Euler's identity: ejπ=1

† Since ejx=cosx+jsinx... but that's for another course!

20 / 22

Q: what's your favourite equation?

Exponentiation

Euler's identity: ejπ=1

† Since ejx=cosx+jsinx... but that's for another course!

In Python: the ** operator

20 / 22

Q: what's your favourite equation?

Exponentiation

Euler's identity: ejπ=1

† Since ejx=cosx+jsinx... but that's for another course!

In Python: the ** operator

xy in Python is x**y

20 / 22

Q: what's your favourite equation?

Exponentiation

Euler's identity: ejπ=1

† Since ejx=cosx+jsinx... but that's for another course!

In Python: the ** operator

xy in Python is x**y

>>> from math import *
>>> e ** (1j * pi)
(-1+1.2246467991473532e-16j)
20 / 22

Q: what's your favourite equation?

Note that the answer isn't precisely -1: it's 1+1.22646×1016j. This is due to the way that floating-point numbers are represented in a computer, and it's emblematic of the limits of computation.

Questions?

21 / 22

(here endeth the lesson)

22 / 22

Last time

Introduction to Introduction to Programming

Computer structure

Knowledge and Computation

2 / 22
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow