from IPython.core.display import HTML

HTML(open("custom.html", "r").read())

Script 1

Short Recap from Script 0:

This was our first Python program, it asks the user for a number n and computes the sum of the natural numbers 1 to n:

n = int(input("please enter n: "))

i = 0
acc = 0
for x in range(n):
    i = i + 1
    acc = acc + i

print("the sum is", acc)
---------------------------------------------------------------------------
StdinNotImplementedError                  Traceback (most recent call last)
<ipython-input-2-a824b0f958a3> in <module>()
----> 1 n = int(input("please enter n: "))
      2 
      3 i = 0
      4 acc = 0
      5 for x in range(n):

/Users/uweschmitt/Projects/python3-course/venv/lib/python3.6/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
    687         if not self._allow_stdin:
    688             raise StdinNotImplementedError(
--> 689                 "raw_input was called, but this frontend does not support input requests."
    690             )
    691         return self._input_request(str(prompt),

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

Python variables

We learned that variables can be conceived of being names for cells in your computers memory. There are a few rules for valid variable names in Python:

  1. The first letter may be an alphabetic character from a to z, A to Z or an under score _.
  2. The optionally following letters are a mixture of alphabetic characters from a to z, A to Z, digits form 0 to 9 and under scores _.

This means: you must not use umlauts or special characters in a variable name !

Some examples for valid variable names: i, j0, my_name, _my_name, _, HiYou, __0, this_is_a_long_name and ThisIsALongName.

The case of the characters of a variable name matter !, so SPAM is a different variable than sPaM, spam or sPAM ...

Not allowed variable names

You must not use built in statements as variable names. For example for is not allowed:

for = 3
  File "<ipython-input-3-8b5bc9af4f1d>", line 1
    for = 3
        ^
SyntaxError: invalid syntax

Input

In order to communicate with the user we used above

x = int(input("please give me a number and press the return key: "))
---------------------------------------------------------------------------
StdinNotImplementedError                  Traceback (most recent call last)
<ipython-input-4-5989ceab6026> in <module>()
----> 1 x = int(input("please give me a number and press the return key: "))

/Users/uweschmitt/Projects/python3-course/venv/lib/python3.6/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
    687         if not self._allow_stdin:
    688             raise StdinNotImplementedError(
--> 689                 "raw_input was called, but this frontend does not support input requests."
    690             )
    691         return self._input_request(str(prompt),

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

This is what happens if the Python interpreter executes the previous line:

  1. The text delimited by " is displayed,
  2. then the computer waits for user input
  3. and when the user presses the Enter key, the input is assigned to a variable (memory cell), here named x.

We explain later why we write the input statement this way (especially what the int is good for).

Output

The so called print function is used for showing values. You can separate text and variables to print with commas ,.

Now we complete the previous example:

x = int(input("please enter a number: "))
print("you gave me", x, "and the square of this value is", x * x)
---------------------------------------------------------------------------
StdinNotImplementedError                  Traceback (most recent call last)
<ipython-input-5-20ff3568e67f> in <module>()
----> 1 x = int(input("please enter a number: "))
      2 print("you gave me", x, "and the square of this value is", x * x)

/Users/uweschmitt/Projects/python3-course/venv/lib/python3.6/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
    687         if not self._allow_stdin:
    688             raise StdinNotImplementedError(
--> 689                 "raw_input was called, but this frontend does not support input requests."
    690             )
    691         return self._input_request(str(prompt),

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

It is important that you provide text for input and output delimited with ". The text between those marks is called string (as a "string of pearls", here: string of single characters). The Python interpreter needs those delimiters to distinguish Python commands from the plain text.

Another Python program

To warm up with Python we translate the salary calculation example from the last session to Python:

hours = float(input("number of hours worked:"))
salary_per_hour = float(input("salary per hour:"))

if hours <= 40:
    salary = hours * salary_per_hour 
else:
    salary_40 = 40 * salary_per_hour
    over_hours = hours - 40
    over_hours_salary = 2 * over_hours * salary_per_hour
    salary = salary_40 + over_hours_salary
    
print("the calculated salary is", salary)
---------------------------------------------------------------------------
StdinNotImplementedError                  Traceback (most recent call last)
<ipython-input-6-41ff98b2a56b> in <module>()
----> 1 hours = float(input("number of hours worked:"))
      2 salary_per_hour = float(input("salary per hour:"))
      3 
      4 if hours <= 40:
      5     salary = hours * salary_per_hour

/Users/uweschmitt/Projects/python3-course/venv/lib/python3.6/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
    687         if not self._allow_stdin:
    688             raise StdinNotImplementedError(
--> 689                 "raw_input was called, but this frontend does not support input requests."
    690             )
    691         return self._input_request(str(prompt),

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

Exercise 1

Enter the program above in PyCharm or at https://repl.it/languages/python3 and run it for different input values. DO NOT USE COPY AND PASTE. Be accurate, typing errors will result in error messages if you run the program.

More computations with numbers

In Python standard algebraic operations addition, subtraction, multiplication and division are written as +, -, * and /. For computing powers we use **. So $2^{20}$ is written as

print(2 ** 20)
1048576

Further the order of evaluation for such operations is as we know from mathematics: * and / have higher precedence than + and -, but we can use parenthesis to change evaluation order:

x = 2 * (3 + 7)
print(x)
20

In contrast to math as we know it we always have to provide a *. In math we can write $2 ( x + 3)$ which is not valid in Python:

x = 3
y = 2 (x + 3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-9fca408471a9> in <module>()
      1 x = 3
----> 2 y = 2 (x + 3)

TypeError: 'int' object is not callable

Above you see an example for an error messages in Python. Understanding the full output of this message is beyond this course, but you should recognize which line causes the problem.

The correct version works:

y = 2 * (x + 3)
print(y)
12

So called integer division in Python is expressed with //, it rounds the division result down to the next integer number:

print(13 / 3, 13 // 3)
4.333333333333333 4
print(-13 / 3, -13 // 3)
-4.333333333333333 -5

The division remainder is the "left over" if we divide two integer numbers. For example the division reminder of 7 / 2 is 1 and the remainder of 7 / 5 is 2.

To compute the division remainder Python programmers use %. So to compute the remainder of 20 / 3 we write

print(20 % 3)
2

We use this later to detect if a given number is even or odd. n % 2 is 0 for even number and 1 for odd numbers.

In general this can be used to detect if a number is a multiple of another number. For example 200 is a multiple of 8 because the remainder is zero:

print(200 % 8)
0

Further we can use scientific notation: the expression 1.2e3 is the same as $1.2 \times 10^3$ which is 1200. Or 3.4e-3 is $3.4 \times 10^{-3}$ which equals 0.0034.

So to express $\frac{2.21 \times 10^{17}}{1.1 \times 10^{-3}}$ we can write

print(2.21e17 / 1.1e-3)
2.009090909090909e+20

Exercise block 2

  1. Repeat the examples above in PyCharm or at https://repl.it/languages/python3

  2. If you put a grain of rice to the first field of a chess board and double the number of grains from field to field (so you put 1, 2, 4, 8, ... $2^{63}$ grains to the field), you have $2^{64} - 1$ grains in total. Assume a grain of rice has a weight of $25$ mg, which is $25 \times 10^{-3}$ g. What is the overall weight of the rice on the check-board in tons ? How many percent is this in relation to the weight of the earth which has $5.972 \times 10^{21}$ tons ? Use Python to compute this !

A bit more math

Python ships with so called $modules$ which extend the capabilities of Python.

So for example to compute mathematical functions as $\sin$ you have to use such a module. Before you can access such functions you have to "import" the module first.

Another name for "module" is "library".

To compute the $\sin$ function you first must instruct

import math

You have to include this statement in every script which wants to use functions from this module. It is not that you run this command once and then math is available on your computer forever.

After this import math, the "content" of this module can be accessed as attributes of the module, which means you write math. followed by the function of interest:

print(math.sin(2.0))
0.9092974268256817

You see terms like x.y often in Python and we say y is an attribute of x.

Some of the attributes of the math module are plain numbers, for example:

print(math.pi, math.e)
3.141592653589793 2.718281828459045

So to compute $\cos(\pi)$ you can write

print(math.cos(math.pi))
-1.0

And to compute $\log e$

print(math.log(math.e))
1.0

Exercise block 3

  1. Repeat the examples above in PyCharm or at https://repl.it/languages/python3
  2. Which number is bigger: $\pi^e$ or $e^\pi$ ? To answer this question use Python to compute both numbers.
  3. math.sqrt computes the square root of a given number. Use the Pythagorean theorem to compute the length of the hypotenuse of right triangle with catheti having lengths 1.0 and 2.0.
  4. What is an "attribute" in terms of Python ?

Some comments on numerical precision

We know that $\sin(\pi)$ is zero. But Python tells a different number:

print(math.sin(math.pi))
1.2246467991473532e-16

Don't panic, your computer is not broken, but this is a consequence of the fact that every computer has limited memory. So real numbers with an infinite number of digits after the decimal point (as $\pi$) have to be truncated at a given point to fit into the computers memory.

So math.pi does not provide the exact value of $\pi$ but a (quite good) approximation:

print(math.pi)
3.141592653589793

So if you see such effects, keep in mind that many numerical computations with numbers on a computer only give you an approximate result, but usually this is an approximation with high accuracy.

The value 1.2246467991473532e-16 we've seen above is the same as 0.00000000000000012246467991473532, so pretty close to 0.0

Final message: if you see strange effects as above or in the following example keep calm, neither Python nor your computer is broken :)

print(.8 - 1)
-0.19999999999999996

Introduction to types

When we said that you can imagine a variable as a name for a cell in your computers memory we simplified the situation a bit.

If a computer handles data as, every piece of data has a so called type. So every cell in your computers memory not only holds a value, but also knows the type of the value. Up to now we know two types: So called int numbers which represent natural numbers (without a decimal point), and float values which describe real numbers (decimal point allowed).

To find out what the type of a value is we can use the so called type function:

print(type(3))
print(type(3.0))
<class 'int'>
<class 'float'>

The concrete type of a value has three impacts:

  1. The type determines the range of values which the computer can handle.
  2. The type determines allowed operations with the value.
  3. The type determines the pattern how the computer internally stores the number as a sequence of binary 0 and 1 values.

So a int can only hold integer values without digits after the decimal point, but a float can. In contrast an int is more precise for representing large integer numbers.

For the types we know up to far, point 2 is not obvious, we will can show this more clearly for other types later in the script.

Discussing point 3 is beyond the scope of this course.

Introduction to strings

Computers not only process numbers but pieces of text (we say "sequences of characters") as well. Some examples are:

To represent this kind of data computers use so called strings. In general a string is a finite sequence of characters.

As every program is a sequence of characters we have to indicate in our programs which characters are instructions and which characters belong to a string. We use " characters as delimiters for this. So in our example

x = int(input("please enter a number: "))
print("x is", x, "and the square of x is", x * x)
---------------------------------------------------------------------------
StdinNotImplementedError                  Traceback (most recent call last)
<ipython-input-25-f3e8d336c9d4> in <module>()
----> 1 x = int(input("please enter a number: "))
      2 print("x is", x, "and the square of x is", x * x)

/Users/uweschmitt/Projects/python3-course/venv/lib/python3.6/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
    687         if not self._allow_stdin:
    688             raise StdinNotImplementedError(
--> 689                 "raw_input was called, but this frontend does not support input requests."
    690             )
    691         return self._input_request(str(prompt),

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

the sections delimited by quotes is data (as a numbers are data), the rest consists of instructions.

Some comments:

  • the quotes do not belong to the string.
  • you may use single quotes as well, but you have to use the same delimiter at the beginning and at the end of the string.
  • "" and '' represent the empty string.
  • " " is a string with one "space" character.

We used strings above but did not process them. To do so, we can store strings in variables and we can use them for computing:

greeting = 'hi'
name = "jo"
formula = greeting + " " + name
print(formula)
hi jo

Again our variables have types. To check this we execute:

print(type(greeting))
<class 'str'>

The len function computes the length of a string:

print(len(" "), len("abc"), len(""))
1 3 0

And as we said above the type defines the kind of data as well as how to operate on the data. So for int and float values the + means addition as we know it, but for str values the + (as we did see above) means concatenation !

Take care to distinguish strings which look like numbers and actual numbers. The string "123" looks like the number 123 but is a sequence of three characters. You see the difference below:

print("123" + "456", 123 + 456)
123456 579

Exercise block 4

  1. Reread the explanations above carefully, run the examples in PyCharm or at https://repl.it/languages/python3
  2. What happens if you run x = "1" + 1 ? Does y = "1" * "2" work ?

How to comment code

You can use the # symbol to mark comments in Python code, the # and the rest of the line after this marker will be ignored by Python:

# this is a full line comment

x = 1  # and this is a comment at the end of the line

For multi line comments you can use """ or ''' as delimiters:

"""
this is the first line of a multi line comment
and this is the second line. In a real program we might says something about x:
"""
x = 1

About Python functions

Mathematical functions transform values. A function has an argument and "computes something". So $\sin x$ takes an argument $x$ and computes a new value.

In Python and other programming languages functions are more general: some of them accept an arbitrary number of arguments, may "perform some action" and finally compute something.

An example for a mathematical functions we did see above was math.sin.

type is a function as well, it takes a value and returns a description of the type of the value:

print(type(1))
<class 'int'>

We say: we call the function type with the argument 1.

The print function accepts an arbitrary number of arguments of different types:

print(1, 2, 3)
1 2 3
print("1 + 2 is", 1 + 2)
1 + 2 is 3

Another Python function is input: the argument of input is a string, and when this function is called

  1. the argument is displayed
  2. the computer waits for the user to enter some text
  3. when the user presses the Enter key, the value of this function call is the user input as a string.
name = input("your name ? ")
print("name is", name, "and the type of name is", type(name))
---------------------------------------------------------------------------
StdinNotImplementedError                  Traceback (most recent call last)
<ipython-input-35-941983f4ee74> in <module>()
----> 1 name = input("your name ? ")
      2 print("name is", name, "and the type of name is", type(name))

/Users/uweschmitt/Projects/python3-course/venv/lib/python3.6/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
    687         if not self._allow_stdin:
    688             raise StdinNotImplementedError(
--> 689                 "raw_input was called, but this frontend does not support input requests."
    690             )
    691         return self._input_request(str(prompt),

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

How function calls work

If we call a function as math.cos(0) Python computes the return value which is 1.0 and replaces the full term math.cos(0) with this value. So

x = math.cos(0)

is "transformed" to

x = 1.0

And

print("cos(0) is ", math.cos(0))
cos(0) is  1.0

is transformed to

print("cos(0) is ",  1.0)
cos(0) is  1.0

This works for nested calls in the same way, we always evaluate from the inside to the outside:

Because of $sin(0) = 0$

x = math.cos(math.sin(0)) * 2

is first transformed to

x = math.cos(0) * 2

and then to

x = 1.0 * 2

Functions for type conversions

Two other functions we did see in the examples above are int and float.

=== Before you read on look above where and how we used them !!! ===

The argument of the int function is a string of digits and the return value is the according natural number:

one_two_three = "123"
print(one_two_three, type(one_two_three))
x = int(one_two_three)
print(x, type(x))
123 <class 'str'>
123 <class 'int'>
print("123" + "456")
print(int("123") + int("456"))
123456
579

print(int("123") + int("456")) is a nested function call again:

  1. first int("123") and ìnt("456") are evaluated and return integers 123 and 456.
  2. then 123 + 456 is computed which is 579.
  3. finally we call print with the argument 579 which displays this value

The float function works like int but for real numbers:

approx_pi = float("3.141")
print(approx_pi, type(approx_pi))
3.141 <class 'float'>

This is why we used

hours = float(input("number of hours worked: "))
---------------------------------------------------------------------------
StdinNotImplementedError                  Traceback (most recent call last)
<ipython-input-46-676ef04690f0> in <module>()
----> 1 hours = float(input("number of hours worked: "))

/Users/uweschmitt/Projects/python3-course/venv/lib/python3.6/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
    687         if not self._allow_stdin:
    688             raise StdinNotImplementedError(
--> 689                 "raw_input was called, but this frontend does not support input requests."
    690             )
    691         return self._input_request(str(prompt),

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

Here the call of input returns the string "12.5", so the previous example is "transformed" to

hours = float("12.5")

which results in

hours = 12.5

Exercise block 5:

  1. Reread the explanations above carefully, and run the code examples in PyCharm or at https://repl.it/languages/python3
  2. What happens if you call int("one") or float("pi") ?
  3. Write a program which asks the user for a temperature in Celsius and displays the equivalent value in Farenheit.

About Repetition exercises

This script includes so called "repetition exercises". Most exercises before asked you to repeat or modify Python code, here the goal is to consolidate your programming knowledge by solving a problem starting with a blank Python script.

You can find the solution (or a very similar solution) of such an repetition exercise in the preceding examples or in the homework solution proposals, but the idea is to look up the solution only if you need more than 15 minutes.

If you have to look up the solution:

  • type the solution and compare it to your approach.
  • try the same exercise the next day again

The more you automate basic programming procedures the easier it gets to solve larger problems.

These exercises are also called Code Katas which exist on different skill levels. The name is based on Kata from martial arts.

Repetition exercise

  • Write a Python program which asks the user to provide a natural (integer) number n and then computes and displays the sum of the first n natural numbers.