e.g., find the sum of the set
(do this as an exercise)
Description of instructions for a computer
Python:
y = x / 2if (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...
What is software?
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.
Like natural languages, a medium for expressing semantics
Unlike natural languages, highly constrained (more like math). Allows succinct yet powerful constructions.
Like natural languages, a medium for expressing semantics
Unlike natural languages, highly constrained (more like math). Allows succinct yet powerful constructions.
Like natural languages, a medium for expressing semantics
Unlike natural languages, highly constrained (more like math). Allows succinct yet powerful constructions.
Like natural languages, a medium for expressing semantics
Unlike natural languages, highly constrained (more like math). Allows succinct yet powerful constructions.
Type 1 + 2 * 3 - 4
into
pythonmorsels.com/repl, then press Enter
Python 3.9.1>>> x = 21>>> y = 21>>> x + y42
x = 21y = 21print(x + y)
21 + 21
42
Expression: values and operations that evaluate to a value
Expression: values and operations that evaluate to a value
Expression: values and operations that evaluate to a value
Expression: values and operations that evaluate to a value
Expression: values and operations that evaluate to a value
42
: an integer literalExpression: values and operations that evaluate to a value
42
: an integer literal3.14
: a real-number (floating-point) literalExpression: values and operations that evaluate to a value
42
: an integer literal3.14
: a real-number (floating-point) literal'hello'
: a string literalExpression: values and operations that evaluate to a value
42
: an integer literal3.14
: a real-number (floating-point) literal'hello'
: a string literalTrue
: a logical (Boolean) literalExpression: values and operations that evaluate to a value
Expression: values and operations that evaluate to a value
1
, 2
, 42
... (ok, so like math!)Expression: values and operations that evaluate to a value
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)
Expression: values and operations that evaluate to a value
1
, 2
, 42
... (ok, so like math!)1_000_000
(ok, so a bit like math...)0b10
, 0o10
, 0x10
(what!?)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
3.0
— is this the same as 3
?Expression: values and operations that evaluate to a value
3.0
— is this the same as 3
?3.1
— ok, definitely not the same as 3
Expression: values and operations that evaluate to a value
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
Expression: values and operations that evaluate to a value
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)
Expression: values and operations that evaluate to a value
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)
3.14e0
, 1e100
...Expression: values and operations that evaluate to a value
Expression: values and operations that evaluate to a value
1j
, 2j
, ...Expression: values and operations that evaluate to a value
1j
, 2j
, ...1.0j
, 1.1j
...Expression: values and operations that evaluate to a value
1j
, 2j
, ...1.0j
, 1.1j
...Expression: values and operations that evaluate to a value
1j
, 2j
, ...1.0j
, 1.1j
...1+2j
is actually an expressionExpression: values and operations that evaluate to a value
Expression: values and operations that evaluate to a value
Expression: values and operations that evaluate to a value
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.
Expression: values and operations that evaluate to a value
Actually, it's slightly more complicated than that, but...
>>> from math import *>>> pi3.141592653589793>>> 2j * pi6.283185307179586j
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
Actually, it's slightly more complicated than that, but...
>>> from math import *>>> pi3.141592653589793>>> 2j * pi6.283185307179586j
r1 + r2
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.
Symbol | Meaning | Usage | Math |
---|---|---|---|
+ |
addition | 1 + 2 |
|
- |
subtraction | 3 - 4 |
|
* |
multiplication | 5 * 6 |
|
/ |
division | 7 / 8 |
Operation | Kind |
---|---|
() |
parenthetical |
* , / |
multiplicative |
+ , - |
additive |
Operation | Kind |
---|---|
() |
parenthetical |
* , / |
multiplicative |
+ , - |
additive |
Top Hat question: Order of Operations (literals only)
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.
>>> 7 / 23.5
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.
When performing long division, we will often leave the result as 3 with a remainder of 1.
>>> 7 / 23.5
>>> 7 // 23>>> 7 % 21
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.
When performing long division, we will often leave the result as 3 with a remainder of 1.
† Since
† Since
**
operator† Since
**
operatorx**y
† Since
**
operatorx**y
>>> from math import *>>> e ** (1j * pi)(-1+1.2246467991473532e-16j)
Note that the answer isn't precisely -1: it's
(here endeth the lesson)
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 |