Slide 6
Slide 6
Conditionals
Conditionals allow you, the programmer, to allow your
program to make decisions: As if your program has the
choice between taking the left-hand road or the
right-hand road based upon certain conditions.
Built within Python are a set of “operators” that can are
used to ask mathematical questions.
Conditional statements compare a left-hand term to a
right-hand term.
2
Conditional Operators
<=
> < >=
Greater Than Less Than Greater Than Less Than or
or Equal to Equal to
== !=
equals Not equal to
3
If Statements
If statements use bool or Boolean values (true or false) to
decide whether or not to execute. If the statement of x > y is
true, the compiler will register it as true and execute the code.
For example:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y:
print("x is less than y")
Notice how your program takes the input of the user for both x
and y, casting them as integers and saving them into their
respective x and y variables. Then, the if statement compares x
and y. If the condition of x < y is met, the print statement is
executed.
4
If Statements
The pervious code can be further revised as follows:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y:
print("x is less than y")
if x > y:
print("x is greater than y")
if x == y:
print("x is equal to y")
6
Elif
This program can be improved by not asking three consecutive questions.
After all, not all three questions can have an outcome of true! Revise your
program as follows:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
elif x == y:
print("x is equal to y")
Notice how the use of elif allows the program to make less decisions. First,
the if statement is evaluated. If this statement is found to be true, all the elif
statements not be run at all. However, if the if statement is evaluated and
found to be false, the first elif will be evaluated. If this is true, it will not run
7 the final evaluation.
Elif
8
If….Else
There is one final improvement we can make to our program. Notice how
logically elif x == y is not a necessary evaluation to run. After all, if logically
x is not less than y AND x is not greater than y, x MUST equal y. Therefore,
we don’t have to run elif x == y. We can create a “catch-all,” default
outcome using an else statement. We can revise as follows:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
else:
print("x is equal to y")
Notice how the relative complexity of this program has decreased through
our revision.
9
If….Else
10
Or
or allows your program to decide between one or more
alternatives. For example, we could further edit our
program as follows:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y or x > y:
print("x is not equal to y")
else:
print("x is equal to y")
Notice that the result of our program is the same, but
the complexity is decreased and the efficiency of our
code is increased.
11
Or
At this point, our code is pretty great. However, could
the design be further improved? We could further edit
our code as follows:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x != y:
print("x is not equal to y")
else:
print("x is equal to y")
Notice how we removed the or entirely, and simply
asked “is x not equal to y?” We ask one and only one
question. Very efficient!
12
Or
For the purpose of illustration, we could also change our
code as follows:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x == y:
print("x is equal to y")
else:
print("x is not equal to y")
Notice that the == operator evaluates if what is on the
left and right are equal to one another. That use of
double equal signs is very important. If you use only one
equal sign, an error will likely be thrown by the compiler.
13
Flowchart
14
And
Similar to or, and can be used within conditional
statements. For example:
score = int(input("Score: "))
17
Project 01: Loan Approval System
A client is eligible for loan if his/her credit eligibility is
more than 0.5. Credit eligibility of a client is measured
by dividing the client’s credit score by his/her total
income. The credit eligibility cannot be more than 1.
Create a python program to assess the loan eligibility of
a client and show whether he/she is eligible for loan or
not.
18
Project 02: Currency Converter
Create a currency converter program where the user
can convert USD into BDT and vice versa. Assume USD
1 = BDT 109.3. The output should be like any of the
following statements:
19
That’s All
For Today!
20