0% found this document useful (0 votes)
14 views30 pages

Conditionals

Uploaded by

ubcode01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views30 pages

Conditionals

Uploaded by

ubcode01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 5

Conditionals

The main topic of this chapter is the if statement, which executes


different code depending on the state of the program. But first we will
introduce two new operators: floor division and modulus.

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

An alternative is to use the modulus operator, %, which divides two


numbers and returns the remainder.
>>> remainder = minutes % 60
>>> remainder
45
Qëndrim Krasniqi
Floor division and modulus
The modulus operator is more useful than it seems. For example, you
can check whether one number is divisible by another—if x % y is
zero, then x is divisible by y.
>>> 21 % 2
1
>>> 21 % 3
0

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

1. There are 40 bytes and 3 bits in 323 bits.


2. 333 divided by 4 has a quotient of 83 and the remainder is 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'>

1. Value of 12 >= 12 is True.


2. Value of “pse” == “pse” is True
3. Type of True is bool.
4. Type of 1 <= -1 is bool.
Qëndrim Krasniqi
3.2 Exercises
Using boolean expressions, do the following exercises:
1. What is the value of 12 != 13?
2. What is the value of “pse” == “ani”?
3. What is type of False?
4. What is type of 12 < 12?

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:

>>> 42 and True


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'>

1. Value of 12%2 == 0 or 13%3 == 0 is True.


2. Value of not False is True.
3. Type of 12%2 == 0 or 13%3 == 0 is 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')

The Boolean expression after if is called the condition. If it is true, the


indented statement runs. If not, nothing happens.

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')

If the remainder when x is divided by 2 is 0, then we know that x is


even, and the program displays an appropriate message. If the
condition is false, the second set of statements runs. Since the
condition must be true or false, exactly one of the alternatives will run.
The alternatives are called branches, because they are branches in the
flow of execution.
Qëndrim Krasniqi
Chained conditionals
if x < y:
print('x is less than y')
elif x > y:
print('x is greater than y')
else:
print('x and y are equal')

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()

Each condition is checked in order. If the first is false, the next is


checked, and soon. If one of them is true, the corresponding branch
runs and the statement ends. Even if more than one condition is true,
only the first true branch runs
Qëndrim Krasniqi
Nested conditionals
One conditional can also be nested within another:
if x == y:
print('x and y are equal')
else:
if x < y:
print('x is less than y')
else:
print('x is greater than y')

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

Although the indentation of the statements makes the structure


apparent, nested conditionals become difficult to read very quickly. It
is a good idea to avoid them when you can.
Logical operators often provide a way to simplify nested conditional
statements. For example, we can rewrite the following code using a
single conditional:

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.')

For this kind of condition, Python provides a more concise option:

if 0 < 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

You might also like