from IPython.core.display import HTML
HTML(open("custom.html", "r").read())
We learned:
input
and print
functions+
, -
, *
, /
, **
and //
.import math
and how to use functions as math.sin
and constants as math.pi
+
for numbers and strings)"123"
is not the same as the number 123
.int
and float
.Up to now we know the following types:
int
to handle integer numbersfloat
to handle real numbersstr
to handle textIn addition to these types Python offers the so called bool
type for handling logical values True
and False
:
am_i_clever = True
print(am_i_clever)
print(type(am_i_clever))
python_is_boring = False
print(python_is_boring)
print(type(python_is_boring))
The range of bool
type consist of only two values True
and False
. The exact writing with a capital letter in the beginning is important !
As "123"
is different to the number 123
the string "True"
is not the same as the logical value True
.
Usually we create logical values when we compare data. For example if we ask if 3
is smaller than 4
we can express this as
print(3 < 4)
For comparison Python knows the following operators:
notation | meaning |
---|---|
< |
is smaller |
> |
is larger |
<= |
is smaller or equal |
>= |
is larger or equal |
== |
is equal |
!= |
is not equal |
The exact writing again is important, =<
or =>
are wrong !
Take care not to mistake =
and ==
: The first means "assign a value to a variable" which we noted ←
in the first script. The second checks if two given values are equal.
Here are some examples:
print(4 < 3.0)
print(4 > 3.0)
print(3 <= 3.0)
print(3 >= 4.0)
print(3 == 4.0)
print(3 != 4.0)
Similar to algebraic operators as +
for numbers we can compute with logical values with the operations and
, or
and not
:
x = 3
print(x >= 4 and x <= 3)
Above first the comparisons x >= 4
and x <= 3
are evaluated to False
resp. True
. Finally False and True
results in False
.
print(x >= 4 or x <= 3)
print(not True)
Example: you want to check if a value x
is 0
, 1
or 2
. The correct way to write this is
print(x == 0 or x == 1 or x == 2)
Sometimes programming beginners write this expression as they pronounce it:
print(x == 0 or 1 or 2)
This does not trigger an error message but you can see that something is going wrong because the result is not a boolean value ! (The "wrong" notation has a meaning in Python, but explaining this is beyond this course).
If you use logical computations the order of evaluation is
not
and
or
You can use brackets to change this or to make some expressions more readable. So the following two expressions are equivalent:
print(3 < 4 or 4 > 5 and 5 > 3)
print((3 < 4 or (4 > 5 and 5 > 3)))
If you compare strings, ==
and !=
will work as expected:
print("abcd" == "Abcd", "abc" != "abc ")
But strings are ordered as well, we use so called "phone book ordering". This is a < b
for strings means that you would find a
before b
in a phone book:
print("abc" < "abd", "abc" > "abcd")
Try to forecast the values of the following expressions, then use Python to check if you are right.
3 < 4 and 4 <= 5 and 5 != 6
3 == 4 or 3 != 4 and 3 == 4
(3 == 4 or 3 != 4) and (3 == 4)
"ab" <= "bcd" or 3 == 4
"ab" > "bcd" and 3 == 4
"" == " "
(3 = 3) or (3 != 4)
if
¶Branching allows execution of parts of the code depending on one or multiple conditions.
We already introduced an example for if
and else
in the wage calculation example.
The simplest form of branching is a plain if
:
x = 4
if x > 3:
print("x is greater than 3")
print("x is", x)
Program execution starts in the first line. So we set x
to 4
. Then the next line is executed: the Python interpreter checks if x > 3
is True
. This is the case, so the indented line after if
is executed: you can see this in the output below the code. Finally the last line prints a final message.
Again the spelling is important: the :
at the end of the if
statement is mandatory, the indentation of the following line too.
If we modify the first line and set x
to 3
the condition x > 3
is False
so the line after if
will be skipped:
x = 3
if x > 3:
print("x is greater than 3")
print("x is", x)
Again: Program execution starts in the first line. So we set x
to 3
. Then the next line is executed: the Python interpreter checks if x > 3
is True
. This is not the case, so the indented line after if
is skipped. Finally the last line prints a final message.
The line after if x > 3:
is called a code block. As we did see in the wage calculation example, such a block consists of one or more lines having the same indentation. Using the same indentation is important. You are free to choose one or more spaces, most Python programmers use four.
If we change the program a little, you can see that its behavior changes:
x = 3
if x > 3:
print("x is greater than 3")
print("x is", x)
The if
condition is False
so the subsequent indented code block (which now consists of two lines) is skipped. Then the interpreter reaches the end of the script and execution stops. This is why you see no output below the code !
Using inconsistent indentations within one code block is a syntax error. The Python interpreter raises an error message:
x = 3
if x > 3:
print("x is greater than 3")
print("x is", x)
else
¶Sometimes we want to branch code execution handle two exclusive situations. This is where else
is used:
x = 2
if x % 2 == 0: # you still know what "%" computes ????
print(x, "is even")
else:
print(x, "is odd")
You see two indented code blocks above: the first block after if x % 2 == 0:
is only executed if the condition is True
. If this is not the case the Python interpreter runs the code block after else:
.
We change the value of x
to 3
so you can see the different flow of execution in the output field:
x = 3
if x % 2 == 0: # you still know what "%" computes ????
print(x, "is even")
else:
print(x, "is odd")
Again both code blocks may contain multiple lines and the indentation must be the same for every line within a block.
0.0
to 1.0
and displays an appropriate message.Try to rewrite the wage computation script without cheating. (Remember: The user is asked for the number of hours he worked and for the wage per hour. Extra hours over 40 hours are honored by doubling the rate for the over hours. Finally the computed wage is displayed.).
elif
for multiple checks¶The last Python keyword for branched execution is elif
. This is an abbreviation for "else if" and can be used as follows:
x = -2
if x > 1:
print(x, "is a number greater than one")
elif x < 0:
print(x, "is a negative number")
Here the first condition x > 0
is False
, so program execution continues at the elif
statement. elif
always is followed by another condition. Here x < 0
is True
, so the following print
is executed, what you can see in the output field.
If we change the script to x = 2
in the beginning we see that the first code block is processed, but not the second one:
x = 2
if x > 1:
print(x, "is a number greater than one")
elif x < 0:
print(x, "is a negative number")
If none of the conditions is True
you see that both indented code blocks are skipped, execution continues after if
-elif
:
x = 0.5
if x > 1:
print(x, "is a number greater than one")
elif x < 0:
print(x, "is a negative number")
print("done")
elif
statements may be repeated as often as needed. So we could extend the previous example as:
x = 0.5
if x > 1:
print(x, "is a number greater than one")
elif x < 0:
print(x, "is a negative number")
elif x >= 0 and x <= 1:
print(x, "is in the range 0 to 1")
In contrast to else
, elif
is always followed by a condition. We use if
followed by multiple elif
to test a sequence of conditions until the first condition evaluates to True
.
if
, elif
and else
combined¶We can combine elif
and else
. If we look at the previous example the last elif
will always be executed if both preceding conditions are False
, we can simplify this to:
x = 0.5
if x > 1:
print(x, "is a number greater than one")
elif x < 0:
print(x, "is a negative number")
else:
print(x, "is in the range 0 to 1")
if
, elif
and else
:¶if
in the beginning is mandatory,elif
statements followelse
branch.This means:
elif
after else
is not allowed (and makes no sense)else
make no sense and are not allowed.x
.Write a program which checks if a number is a multiple of 3
and/or 4
and writes appropriate messages for all four cases.
Extend the wage computation with an extra rule: over hours over 60 hours are paid with the triple wage per hour !
The following two snippets are syntactically slightly different. Can you explain the different output displayed when you run the code ?
x = 1
if x > 0:
print(x, "is positive")
elif x < 0:
print(x, "is negative")
else:
print(x, "is zero")
x = 1
if x > 0:
print(x, "is positive")
if x < 0:
print(x, "is negative")
else:
print(x, "is zero")
We will later implement the https://en.wikipedia.org/wiki/Rock-paper-scissors game and this exercise is a preparation for this.
https://en.wikipedia.org/wiki/Rock-paper-scissors has the following rules, we use abbreviations R, P and S for rock, paper, scissors:
Write a script which first asks the user for the move of player one then for the move of player two and then displays who wins. (Hint: you can implement the decision with one if
followed by one elif
and one else
if you use and
and or
).
Extend this so that invalid inputs are detected and handled after both inputs were provided.
Write a program which asks the user for an integer number $n$ and then computes and displays the product of the first $n$ natural numbers.
if
-elif
-else
can be nested, here we have if
and else
within code blocks belonging to another if
or else
.
The line numbers below don't belong to the actual code, I added them to simplify following explanatins:
If the condition in line 3
below is true, the code block in lines 4 to 7
is executed, lines 9 to 13
are ignored.
If the condition in line 3
below is false, lines 4 to 7
are ignored, and program execution contineus at line 9
:
n = int(input("gimme a number: "))
if n % 4 == 0:
if n % 3 == 0:
print(n, "can be divided by 4 and by 3")
else:
print(n, "can be divided by 4 but not by 3")
else:
if n % 3 == 0:
print(n, "can be divided by 3 but not by 4")
else:
print(n, "can neither be divided by 3 nor by 4")
12
, 8
, 9
and 7
and try to follow program execution to understand how it worksif
and elif
and else
without nested if
statements (this means that your code uses only 0 and 4 spaces for identation and not more).