Professional Documents
Culture Documents
Chapter 2
Conditional statements
Alternative branches using the if statement
Example 1
age = int(input("How old are you? "))
When the user is over the age of 17, the execution of the program
should look like this:
Sample output
1
ISTLS Asma HADYAOUI
How old are you? 18 You are of age! Here's a copy of GTA6 for you.
Next customer, please!
Sample output
How old are you? 16 Next customer, please!
Comparison operators
Very typically conditions consist of comparing two values. Here is a
table with the most common comparison operators used in Python:
Example
if number < 0:
print("The number is negative.")
if number > 0:
print("The number is positive.")
2
ISTLS Asma HADYAOUI
if number == 0:
print("The number is zero.")
Indentation
Python recognizes that a block of code is part of a conditional
statement if each line of code in the block is indented the same. That
is, there should be a bit of whitespace at the beginning of every line of
code within the code block. Each line should have the same amount
of whitespace.
You can use the Tab key, short for tabulator key, to insert a set amount
of whitespace:
3
ISTLS Asma HADYAOUI
These types of values are often called Boolean values, named after the
English mathematician George Boole. In Python, they are handled by
the bool data type. Variables of type bool can only have two
values: True or False.
Example:
a = 3
condition = a < 5
print(condition)
if condition:
print("a is less than 5")
Sample output
True a is less than 5
Programming exercise 1:
Loyalty bonus
This program calculates the end-of-year bonus a customer receives
on their loyalty card. The bonus is calculated with the following
formula:
• If there are less than a hundred points on the card, the bonus is 10 %
• In any other case the bonus is 15 %
Sample output
How many points are on your card? 55 Your bonus is 10 % You now
have 60.5 points
Programming exercise 2:
4
ISTLS Asma HADYAOUI
Sample output
What is the weather forecast for tomorrow?
Temperature: 21
Will it rain (yes/no): no
Wear jeans and a T-shirt
Sample output
What is the weather forecast for tomorrow?
Temperature: 11
Will it rain (yes/no): no
Wear jeans and a T-shirt I recommend a jumper as well
Programming exercise 3:
Solving a quadratic equation
In the Python math module there is the function sqrt, which
calculates the square root of a number. You can use it like so:
print(sqrt(9))
This should print out
Sample output
3.0
5
ISTLS Asma HADYAOUI
x = (-b ± sqrt(b²-4ac))/(2a).
You can assume the equation will always have two real roots, so the
above formula will always work.
Sample output
Value of a: 1
Value of b: 2
Value of c: -8
6
ISTLS Asma HADYAOUI
Example:
if number % 2 == 0:
print("The number is even")
else:
print("The number is odd")
Sample output
Please type in a number: 5 The number is odd
7
ISTLS Asma HADYAOUI
8
ISTLS Asma HADYAOUI
Example:
goals_home = int(input("Home goals scored: "))
goals_away = int(input("Away goals scored: "))
Sample output
Home goals scored: 4 Away goals scored: 2 The home team won!
Programming exercise:
Alphabetically last
Python comparison operators can also be used on strings. String a is
smaller than string b if it comes alphabetically before b. Notice
however that the comparison is only reliable if
• the characters compared are of the same case, i.e. both UPPERCASE or
both lowercase
• only the standard English alphabet of a to z, or A to Z, is used.
Please write a program that asks the user for two words. The program
should then print out whichever of the two comes alphabetically last.
Sample output
Please type in the 1st word: car Please type in the 2nd word: scooter
scooter comes alphabetically last.
9
ISTLS Asma HADYAOUI
Combining conditions
• Logical operators
You can combine conditions with the logical operators and and or.
The operator and specifies that all the given conditions must be true
at the same time. The operator or specifies that at least one of the
given conditions must be true.
10
ISTLS Asma HADYAOUI
Example
n1 = int(input("Number 1: "))
n2 = int(input("Number 2: "))
n3 = int(input("Number 3: "))
n4 = int(input("Number 4: "))
Sample output
Number 1: 2
Number 2: 4
11
ISTLS Asma HADYAOUI
Number 3: 1
Number 4: 1
4 is the greatest of the numbers.
Programming exercise:
Alphabetically in the middle
Please write a program that asks the user for three letters. The
program should then print out whichever of the three letters would
be in the middle of the letters were in alphabetical order.
You may assume the letters will be either all uppercase or all
lowercase.
Sample output
1st letter: x
2nd letter: c
3rd letter: p
The letter in the middle is p
Programming exercise:
Gift tax calculator
Some say paying taxes makes Finns happy, so let's see if the secret of
happiness lies in one of the taxes set out in Finnish tax code.
When the gift is received from a close relative or a family member, the
amount of tax to be paid is determined by the following table:
12
ISTLS Asma HADYAOUI
Value of gift Tax at the lower limit Tax rate for the exceeding part (%)
So, for a gift of 6 000 euros the recipient pays a tax of 180 euros (100
+ (6 000 - 5 000) * 0.08). Similarly, for a gift of 75 000 euros the
recipient pays a tax of 7 100 euros (4 700 + (75 000 - 55 000) * 0.12).
Please write a program which calculates the correct amount of tax for
a gift from a close relative. Have a look at the examples below to see
what is expected. Notice the lack of thousands separators in the input
values - you may assume there will be no spaces or other thousands
separators in the numbers in the input, as we haven't yet covered
dealing with these.
Sample output
Value of gift: 3500 No tax!
Sample output
Value of gift: 5000 Amount of tax: 100.0 euros
Sample output
Value of gift: 27500 Amount of tax: 1950.0 euros
13