What are algorithms and what are programs?
Basic input and output
Variables and operators
Important parts of an algorithm:
Example: preparing a frozen pizza. Steps:
Example of the input data:
In [8]:
print("Hello, World!")
The output can be nicely formated, but more on that in the near future.
A very simple program, used to show the basic syntax of a programming language.
See the The Hello World Collection for the examples in many other programming languages.
The need for such an example can be clearly seen from the examples of more complex, but still fairly readable languages (for example, various versions of C++ and Java).
Beware of the Assembler-Z80-Console and BIT examples. ☺</span>
Note: A full program should always start with the following:
#!/usr/bin/env python3
So, a full "Hello World" program would look like this:
In [9]:
#!/usr/bin/env python3
"""
A program that prints the "Hello, World!" message.
"""
print("Hello, World!")
We shall ommit these elements in the lectures, as we shall mostly present chunks of code (that are not necessarily full programs) and to save the screen space.
The following line that Spyder adds to new files
# -*- coding: utf-8 -*-
is not necessary in Python 3. However, if you are using international characters in some encoding other than UTF-8 (which you really shouldn't do!), this is a way to specify that encoding.
In this course we shall not cover encodings, as it is a very technical subject and most of the time it is enough to just use UTF-8 (find it in your editor's settings). However, if you're ever to build an application with the need for the international characters, do look up the encodings on the internet (the Wikipedia page is a good start) and use UTF-8 whenever possible, as it is a widely accepted standard and the default in Python 3. You should also make sure that your editor saves files using the UTF-8 encoding (Spyder does that by default).
In Python 2, the default encoding is ASCII and the UTF-8 support has to be enabled manually.
It is often useful to add human language notes in the code. These are called comments and are ignored by a computer, but they help programmers read the code.
In Python, comments are done by prepending the hash sign #
in front of it. Each comments ends with the end of the line.
It is a standard to always write comments in English:
Python coders from non-English speaking countries: please write your comments in English, unless you are 120% sure that the code will never be read by people who don't speak your language
Source: PEP 8
For example:
In [ ]:
#!/usr/bin/env python3
"""
A program that prints the "Hello, World!" message.
"""
# A welcome message
print("Hello, World!")
# TODO: ask user their name and save it as a file
As you can see, the above code runs just as the previous one, but a programmer reading it can get more information about the code itself.
Some editors will recognize certain tags in comments and highlight them to make them more noticable (like the TODO
tag in the previous example). Some of the more common ones are, as listed in the Wikipedia's comments article:
FIXME
to mark potential problematic code that requires special attention and/or review.
NOTE
to document inner workings of code and indicate potential pitfalls.
TODO
to indicate planned enhancements.
XXX
to warn other programmers of problematic or misguiding code.
We shall often use the comments to denote what certain parts of the code do. These should always be descriptive and not merely rewritten code.
For example, this is good:
# Get the sum of primes in `L` as `prime_sum`
for x in L:
if is_prime(x):
prime_sum += x
as it makes clear what the code is doing, even to someone who doesn't "speak" Python.
This is bad:
# For each element `x` in the list, if `x` is a prime number,
# add it to `prime_sum`.
for x in L:
if is_prime(x):
prime_sum += x
because this comment is just a rewrite of the code that follows it and, as such, it is useless.
It is advisable to keep all lines (comments and docstrings) wrapped under 80 characters, although it shouldn't be forced when it reduces the code readability.
In [10]:
x = input()
print("The value of x is", x)
Well, this x
looks kinda important here. What could it be?
"word"
, "This is a mighty deep, philosophical sentence."
, "ŞƿҿÇïåĿ sɹǝʇɔɐɹɐɥɔ"
, "17",...""
, has no characters (its length is zero).True
and False
).NoneType
-- the type of a special constant None
that means "no value".None
is different from any other constant and any other value that a variable can get.Be careful: 17 is a number, while "17"
is a string!
In [12]:
x = input()
print("The value of x is", x)
Whatever is on the right hand side of the assignment =
gets computed first. Then the result is assigned to the variable on the left hand side. When this is done, the next line of code is executed.
In our example, this means:
input()
reads a sequence of characters from the standard input (usually user's keyboard, although it can be changed) and returns it as a string.x
(on the lefthand side of the assignment operator =
).x
holds - as a string - whatever we have typed up to the first newline, i.e., up to the first Enter key (the newline itself is omitted).print()
now outputs its arguments to the standard output (usually the user's screen), in order in which they were given, separated by a single space character. So,"The value of x is"
is written out.x
is written out (not the string "x" itself, because x
is a variable!).In other words, if we type "17
", our program will write
The value of x is 17
And if we type "a mistery
", our program will write
The value of x is a mistery
Don't expect Python to do anything smart here. It just writes out the values as they were given.
Python 2 remark: In Python 2, print
does not need parentheses (i.e., print "Hello, World!"
is fine).
However, do include them even if writing a Python 2 program, to make it easier to port to Python 3, as well as to avoid problems in some more advanced uses you might encounter in the future.
In [13]:
x = input("x: ")
y = input("y: ")
print(x, "+", y, "=", x+y)
The user types two numbers, which are saved -- as two strings -- in variables x
and y
. Then the program writes out (among other things) the value of x+y
.
How would we "add" one string to another in the real world?
For example, if x = "Bruce"
and y = "Wayne"
, what would x + y
be?
It may come as a little surprise that "x + y"
will not produce "Batman"
. Python is a well defined language that keeps Bruce's secret identity well hidden.
The result of x+y
would, less surprisingly, be "BruceWayne"
. Notice that there is no additional space here: the strings are glued (concatenated) one to another, with no extra separators!
So, what happens if x = "17"
and y = "19"
?
It would be very bad if Python looked at these and decided that they were numbers only because they have nothing but digits. Maybe we want them concatenated (as opposed to adding them one to another)!
So, the result is -- by now not a bit surprisingly -- "1719
", because the strings' addition +
is a concatenation, regardless of the value of the strings in question.
How do we explain Python to "treat these two as numbers"?
In [14]:
x = int(input())
y = float(input())
print("x = ", x)
print("y = ", y)
print("x+y = ", x+y)
z = 'This is a string: "' + str(x+y) + '"'
print(z)
We see three conversion functions:
int()
, which takes a string and converts it to an integer. If the argument is not a string representation of an integer, an error occurs.float()
, which takes a string and converts it to a "real" number (also called floating point number, hence the name of the function). If the argument is not a string representation of a real number, an error occurs.str()
, which takes a number (among other allowed types) and converts it to a string.Python 2 remark: In Python 2, input()
is similar to float(input())
in Python 3 (actually, eval(input())
, but that is well beyond this lecture), i.e., it loads a number and returns it as a floating point number (causing an error or a strange behaviour if anything else is given as the input).
To load a string in Python 2, one has to call raw_input()
(which does not exist in Python 3).
A note on the string creation: There are better ways to form the variable z
(using various formatting methods), but this will have to wait a few weeks until we cover strings in more depth than here.
In [15]:
x = 17
print("The value of x was", x)
x = x + 2
print("The value of x is", x)
As we said before: whatever is on the right hand side of the assignment =
, gets computed first. Only after that, the result is assigned to the variable on the left hand side.
So, when Python encounters the command
x = x + 2
while the value of x
is 17, it first computes x + 2
(for x = 17
), which is 19. After that, it preforms the assignment x = 19
, so 19 becomes the new value of x
(which is then printed with the second print
).
In most of the modern languages, x = x + y
can be written as x += y
. The same shortcut works for other operators as well, i.e., x = x op y
can be written as x op= y
.
For basic numerical operations, this means that we have the following translation:
Expression | Shortcut |
---|---|
x = x + y |
x += y |
x = x - y |
x -= y |
x = x * y |
x *= y |
x = x / y |
x /= y |
x = x // y |
x //= y |
x = x % y |
x %= y |
x = x ** y |
x **= y |
A note on other languages: there are no increment (++
) and decrement (--
) operators in Python.
Most of the operators from the previous slide have the same meaning as in mathematics (@C-people: /
means the usual, i.e., real division). The three not used in mathematics are defined as follows:
x // y
means floored quotient of x
and y
(also called integer division), i.e., x // y
$:= \left\lfloor \mathsf{x}\ /\ \mathsf{y} \right\rfloor$,x % y
means the remainder of $x / y$, i.e., x % y := x - y * (x // y)
,x ** y
means $x^y$ (x
to the power y
).Python 2 remark: In Python 2, the ordinary real division x/y
works in a C-like manner, which means that x/y
is equivalent to x//y
if both x
and y
are integers.
In Python 3, x/y
always means real division. In other words,
3//2 = 3/2 = 1
, but 3/2.0 = 3.0 / 2 = 3.0 / 2.0 = 1.5
;3//2 = 1
, but 3/2 = 3/2.0 = 3.0 / 2 = 3.0 / 2.0 = 1.5
.
In [16]:
a = 10
b = 0.1
print("a =", a, " b =", b, " -> ", "a + b - a =", a + b - a, "!=", b, "= b")
a = 10**7
b = 10**(-7)
print("a =", a, " b =", b, " -> ", "a + b - a =", a + b - a, "!=", b, "= b")
a = 10**11
b = 10**(-11)
print("a =", a, " b =", b, " -> ", "a + b - a =", a + b - a, "!=", b, "= b")
In [17]:
x = 474953
y = 1 / x
print(x * y)
Fibonacci numbers are defined as follows: $$F_0 := 0, \quad F_1 := 1, \quad F_{n+1} := F_n + F_{n-1}, \quad n \ge 1.$$ There is also a direct formula for computing $F_n$: $$F_n = \frac{\varphi^n - \psi^n}{\sqrt{5}}, \quad \varphi := \frac{1 + \sqrt{5}}{2}, \quad \psi := \frac{1 - \sqrt{5}}{2}.$$ However, in a computer, this will soon give you wrong results.
In the following code, fib1(n)
returns the n
-th Fibonacci number computed by a simple integer-arithmetics algorithm, while fib(2)
uses the above formula (never use the recursive definition for computation of Fibonacci numbers!).
In [20]:
def fib1(n):
f0 = 0
f1 = 1
while n > 1:
(f0, f1) = (f1, f0 + f1)
n -= 1
return f1
def fib2(n):
sqrt5 = 5 ** .5
phi = (1 + sqrt5) / 2
psi = (1 - sqrt5) / 2
return int((phi**n - psi**n) / sqrt5)
n = int(input("Type n (try to go for 73 or more): "))
fib1n = fib1(n)
fib2n = fib2(n)
print("|fib1(n) - fib2(n)| = |" + str(fib1n), "-", str(fib2n) + "| =", abs(fib1n - fib2n))
In [21]:
s = 0
for _ in range(1000):
s += 0.1
print(s)
s = 0
for _ in range(10000):
s += 0.1
print(s)
s = 0
for _ in range(10000000):
last = s
s += 0.1
print(s)
Notice how the result is sometimes smaller and sometimes bigger than the correct result.
In [22]:
from math import pi
x = 15 * pi
# Create the list of series elements
elts = [ ]
f = 1
for k in range(1, 150, 2):
elts.append(x**k / f)
f *= -(k+1) * (k+2)
# Summarize elements in the original order
sin1 = 0
for el in elts:
sin1 += el
print("sin1 =", sin1)
# Summarize elements in the reversed order
sin2 = 0
for el in reversed(elts):
sin2 += el
print("sin2 =", sin2)
# Summarize elements from the middle one to the ones on the edge
cnt = len(elts)
mid = cnt // 2
sin3 = 0
for i in range(mid + 1):
if mid + i < cnt:
sin3 += elts[mid + i]
if i:
sin3 += elts[mid - i]
print("sin3 =", sin3)
# Summarize elements from the ones on the edge to the middle one
sin4 = 0
for i in reversed(range(mid + 1)):
if mid + i < cnt:
sin4 += elts[mid + i]
if i:
sin4 += elts[mid - i]
print("sin4 =", sin4)
print("|sin1 - sin4| =", abs(sin1 - sin4))
print("the first element:", elts[0])
print("the last element:", elts[-1])
The above is the computation of $\sin 15\pi$ via the first $74$ elements of the Taylor series of the sine function,
sin1
computation starting from the first element,sin2
going towards it,sin3
going from the center out ($a_{37} + a_{36} + a_{38} + a_{35} + a_{39} + \dots$),sin4
going from the edges in ($a_1 + a_{74} + a_2 + a_{73} + \dots$).The difference between sin1
and sin4
is roughly $1353$, which may not look like much, but it is far more than the difference between any two sines should be.
You might also notice that $\sin 15\pi$ shouldn't be nowhere near $-3000$ or even $-1768$.
One might think that we should compute more elements of the sum, but this is not the case: the last element of the sum is only around $5.4 \cdot 10^{-12}$ (and those after it would be even smaller).
So, what happened here?
Here is a hint: take a look at how big the elements of our sums are.
In [23]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(elts)
Out[23]:
Let us define $$f(x,n) := \underbrace{\sqrt{\sqrt{\dots\sqrt{x}}}\,\hskip-1em}_{n}\hskip1em, \quad g(x,n) := \hskip3.7em\overbrace{\hskip-3.7em\left(\left(\dots\left(x\right)^2\dots\right)^2\right)^2}^{n}.$$ In other words, $f(x, n)$ is the number that we get by taking the square root of $x$, $n$ times in a row, and $g(x, n)$ is the number we get by computing the second power of $x$, $n$ times in a row.
Obviously, $x = f(g(x, n), n) = g(f(x, n), n)$ for any $n \in \mathbb{N}$ and $x \in \mathbb{R}^+_0$. But, let us see what a computer has to say if we input some $x \ne 1$ and $n = 50, 60, \dots$:
In [24]:
from math import sqrt
x = float(input("x = "))
n = int(input("n = "))
t = x
for _ in range(n):
t = sqrt(t)
for _ in range(n):
t *= t
print("g(f(" + str(x) + ", " + str(n) + "), " + str(n) + ") =", t)
t = x
for _ in range(n):
t *= t
for _ in range(n):
t = sqrt(t)
print("f(g(" + str(x) + ", " + str(n) + "), " + str(n) + ") =", t)
Consider the following two systems of linear equations:
$$\left\{\begin{array}{rcrcr} 1 \cdot x &+& 1 \cdot y &=& 2, \\ 1.000001 \cdot x &+& 1 \cdot y &=& 2.000001, \end{array}\right. \quad \text{and} \quad \left\{\begin{array}{rcrcr} 1 \cdot x &+& 1 \cdot y &=& 2, \\ 1.000001 \cdot x &+& 1 \cdot y &=& 1.999999. \end{array}\right.$$What are their solutions?
The solution to the first one is $(x, y) = (1, 1)$, but the solution to the second one is $(x, y) = (-1, 3)$.
Notice that only one element was changed, and only for an order of magnitude $10^{-6}$, which could've easily been caused by one of the small errors shown before. Similar results can be achieved with arbitrarily small errors.
Always be extra careful when working with "real" numbers in a computer (or, better, avoid them altogether if possible, like in the Fibonacci example)!
These errors cannot always be considered insignificant, as they can pile up and/or grow in subsequent computations.
Anyone intending to do any serious computations using computers should take the course "Numerical Analysis 1" (MATH20602), lest rockets will miss, elevators will fall, and bridges will collapse...