Conditionals
Conditionals
Conditionals
Qëndrim Krasniqi
Floor division and modulus
The floor division operator, //, divides two numbers and rounds down
to an integer:
>>> minutes = 105
>>> hours = minutes // 60
>>> hours
1
Qëndrim Krasniqi
Floor division and modulus
In 105 minutes there is 1 hour. How to get the remaining minutes?
>>> minutes = 105
>>> hours = minutes // 60
>>> remainder = minutes - hours * 60
>>> remainder
45
Qëndrim Krasniqi
1.1 Exercises
Using modulus operator, do the following exercises:
1. How many bytes and bits are there in 323 bits?
2. What is the quotient and remainder of 333 divided by 4?
Qëndrim Krasniqi
1.1 Solutions
>>> # 1
>>> 323 // 8
40
>>> 323 % 8
3
>>> # 2
>>> 333 // 4
83
>>> 333 % 4
1
Qëndrim Krasniqi
2.1 Exercises
Using modulus operator, do the following exercises:
1. How many hours and minutes are in 135 minutes?
2. What is the quotient and remainder of 122 divided by 17?
Qëndrim Krasniqi
Boolean expressions
A boolean expression is an expression that is either true or false. The
following examples use the operator ==, which compares two
operands and produces True if they are equal and False otherwise
>>> 5 == 5
True
>>> 5 == 6
False
Qëndrim Krasniqi
Boolean expressions
True and False are special values that belong to the type bool;
they are not strings:
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
Qëndrim Krasniqi
Boolean expressions
The == operator is one of the relational operators; the others are:
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to y
Qëndrim Krasniqi
3.1 Exercises
Using boolean expressions, do the following exercises:
1. What is the value of 12 >= 12?
2. What is the value of “pse” == “pse”?
3. What is type of True?
4. What is type of 1 <= -1?
Qëndrim Krasniqi
3.1 Solutions
>>> 12 >= 12
True
>>> "pse" == "pse"
True
>>> type(True)
<class 'bool'>
>>> type(1 <= -1)
<class 'bool'>
Qëndrim Krasniqi
Logical operators
There are three logical operators: and, or, and not.
The semantics (meaning) of these operators is similar to their meaning
in English. For example, x > 0 and x < 10 is true only if x is
greater than 0 and less than 10.
n%2 == 0 or n%3 == 0 is true if either or both of the conditions is
true, that is, if the number is divisible by 2 or 3.
Finally, the not operator negates a boolean expression, so not (x >
y) is true if x > y is false, that is, if x is less than or equal to y.
Qëndrim Krasniqi
Logical operators
Strictly speaking, the operands of the logical operators should be
boolean expressions, but Python is not very strict. Any nonzero number
is interpreted as True:
Qëndrim Krasniqi
4.1 Exercises
1. What is the value of 12%2 == 0 or 13%3 == 0?
2. What is value of not False?
3. What is type of 12%2 == 0 or 13%3 == 0?
Qëndrim Krasniqi
4.1 Solutions
>>> 12%2 == 0 or 13%3 == 0
True
>>> not False
True
>>> type(12%2 == 0 or 13%3 == 0)
<class 'bool'>
Qëndrim Krasniqi
4.2 Exercises
1. What is the value of 12%2 == 0 and 13%3 == 0?
2. What is value of not True?
3. What is type of not True?
Qëndrim Krasniqi
Conditional execution
In order to write useful programs, we almost always need the ability to
check conditions and change the behavior of the program accordingly.
Conditional statements give us this ability. The simplest form is the if
statement:
if x > 0:
print('x is positive')
Qëndrim Krasniqi
Conditional execution
There is no limit on the number of statements that can appear in the
body, but there has to be at least one. Occasionally, it is useful to have
a body with no statements (usually as a place keeper for code you
haven’t written yet). In that case, you can use the pass statement,
which does nothing.
if x < 0:
pass # TODO: need to handle negative values!
Qëndrim Krasniqi
Alternative execution
A second form of the if statement is “alternative execution”, in which
there are two possibilities and the condition determines which one
runs. The syntax looks like this:
if x % 2 == 0:
print('x is even')
else:
print('x is odd')
elif is an abbreviation of “else if”. Again, exactly one branch will run.
There is no limit on the number of elif statements. If there is an
else clause, it has to be at the end, but there doesn’t have to be one.
Qëndrim Krasniqi
Chained conditionals
if choice == 'a':
draw_a()
elif choice == 'b':
draw_b()
elif choice == 'c':
draw_c()
The outer conditional contains two branches. The first branch contains
a simple statement. The second branch contains another if statement,
which has two branches of its own. Those two branches are both
simple statements, although they could have been conditional
statements as well.
Qëndrim Krasniqi
Nested conditionals
if 0 < x:
if x < 10:
print('x is a positive single-digit number.')
Qëndrim Krasniqi
Nested conditionals
The print statement runs only if we make it past both conditionals, so
we can get the same effect with the and operator:
if 0 < x and x < 10:
print('x is a positive single-digit number.')
Qëndrim Krasniqi
5.1 Exercises
Put all of your work in a script.
1. Give n an integer as its value.
2. Write a Python program that prints “n is positive” if n is greater
than zero.
3. Write a Python program that:
1. Prints True if n is equal to 1.
2. Prints False if n is smaller than 1.
3. Prints 0 otherwise.
4. Write a Python program that prints “Okay” if n is greater than 10 and
smaller than 15.
5. Assign a value to a variable.
6. Write a Python program that prints this variable if its value is “ani” and if
n is equal to 13.
Qëndrim Krasniqi
5.1 Solutions
conditionals.py
# 1
n = 12
# 2
if n > 0:
print("n is positive")
# 3
if n == 1:
print(True)
elif n < 1:
print(False)
else:
print(0)
Qëndrim Krasniqi
5.1 Solutions
conditionals.py
# 4
if 10 < n < 15:
print("Okay")
# 5
m = 'pse'
# 6
if m == 'ani' and n == 13:
print(m)
Qëndrim Krasniqi
5.2 Exercises
Put all of your work in a script.
1. Assign a value to a variable, a.
2. Write a Python program that prints “a is equal to zero or
negative” if a is not greater than zero.
3. Write a Python program that:
1. Prints True if a is greater than 0.
2. Prints False if a is equal to ‘Yes’.
3. Prints a if a is equal to “No”.
4. Prints “Different” otherwise.
4. Write a Python program that prints “Not possible” if a is smaller than
10 and greater than 15.
5. Give b a float as its value.
6. Write a Python program that prints a if b is not equal to 1.0 or if a is not
equal to 13.
Qëndrim Krasniqi