Unit III Lecture Hour 2
Unit III Lecture Hour 2
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:
x = 10
if x > 0:
print('x is positive')
>>> x is positive
The boolean expression after if is called the condition. If it is true, the indented statement runs. If not,
nothing happens. if statements have the same structure as function definitions: a header followed by an
indented body. Statements like this are called compound statements.
if(condition):
indented Statement Block
The block of lines indented the same amount after the colon (:) will be executed whenever the condition
is TRUE.
a = 33
b = 200
if b > a:
print("b is greater than a")
a = 330
b = 200
In this example we use two variables, a and b, which are used as part of the if statement to test
whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print
to screen that "b is greater than a".
Alternative execution
Elif
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this
condition".
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:
x = 20
if x % 2 == 0:
print('x is even')
else:
print('x is odd')
>>> x is even
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.
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Chained conditionals
Sometimes there are more than two possibilities and we need more than two branches. One way to
express a computation like that is a chained conditional:
Else
The else keyword catches anything which isn't caught by the preceding conditions.
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Nested conditionals
We could have written the example in the previous section like this:
x,y = 10,20
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.
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:
x =7
if 0 < x:
if x < 10:
print('x is a positive single-digit number.')
>>> x is a positive single-digit number.
The print statement runs only if we make it past both conditionals, so we can get the same effect
with the and operator:
x =7
if 0 < x and x < 10:
print('x is a positive single-digit number.')
>>> x is a positive single-digit number.