[go: up one dir, main page]

0% found this document useful (0 votes)
4 views20 pages

Slide 6

The document provides an overview of conditionals in Python, explaining how they allow programs to make decisions based on certain conditions using operators. It covers the use of if statements, elif, and else to streamline decision-making, along with examples of code implementations. Additionally, it includes two project ideas: a loan approval system and a currency converter.

Uploaded by

emonahmed.kl
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)
4 views20 pages

Slide 6

The document provides an overview of conditionals in Python, explaining how they allow programs to make decisions based on certain conditions using operators. It covers the use of if statements, elif, and else to streamline decision-making, along with examples of code implementations. Additionally, it includes two project ideas: a loan approval system and a currency converter.

Uploaded by

emonahmed.kl
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/ 20

Python Practical-04

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

Notice how you are providing a series of if statements. First, the


first if statement is evaluated. Then, the second if statement runs
its evaluation. Finally, the last if statement runs its evaluation.
This flow of decisions is called “control flow.”
5
If Statements
The code can be represented as follows:

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

The code can be


represented as follows:

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

The code can be


represented as follows:

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

The code can be


represented as follows:

14
And
Similar to or, and can be used within conditional
statements. For example:
score = int(input("Score: "))

if score >= 90 and score <= 100:


print("Grade: A")
elif score >=80 and score < 90:
print("Grade: B")
elif score >=70 and score < 80:
print("Grade: C")
elif score >=60 and score < 70:
print("Grade: D")
else:
print("Grade: F")
15
And
We could improve our code as follows:

score = int(input("Score: "))

if 90 <= score <= 100:


print("Grade: A")
elif 80 <= score < 90:
print("Grade: B")
elif 70 <= score < 80:
print("Grade: C")
elif 60 <= score < 70:
print("Grade: D")
else:
print("Grade: F")
16
And
Still, we can further improve our program:
score = int(input("Score: "))

if score >= 90:


print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")

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:

‘Hello, Raihan. The current value of USD 10 is


BDT 1093.0’
Or,
‘Hello, Raihan. The current value of BDT 1093 is
USD 10’

19
That’s All
For Today!
20

You might also like