0% found this document useful (0 votes)
2 views22 pages

L.4 - Python & Conditional Algorithms

Uploaded by

Salma Wesam
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)
2 views22 pages

L.4 - Python & Conditional Algorithms

Uploaded by

Salma Wesam
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

Taught by: Hisham Othman

Slides are based on: Prof. Dr. Slim Abdennadher’s slides

German University Cairo, Department of Media Engineering and Technology


How to construct an algorithm
 Identify the input of the algorithm (if needed)
 Introduce variables for
 Input
 (intermediate) results
 Analyze the task into steps
 Provide for detailed output

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Objectives
 By the end of this lecture, you should be able to:
 Design algorithms using conditional operations

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Algorithms: operations
 Algorithms can be constructed by the following
operations:
 Sequential Operations
 Conditional Operations
 Iterative Operations

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – idea
 Decision

name_of_speaker = input(“Tell who is the speaker:”)


if name_of_speaker == “Einstein" :
print("I will go home")
else:
print("I will stay at the GUC")

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – principle
 Rationale
 Determines whether or not a condition is True; and based on whether or not it
is True; selects the next step to do

 Notation
 Use the same primitives as before plus the following:

if condition:
# <operations for the if-part>
else:
# <operations for the else-part>

 Execution
 Evaluate <condition> expression to see whether it is True or False.
 If True, then execute operations in if-part
 Otherwise, execute operations in else-part

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation
 Example:
 Write an algorithm that takes a number as an input
and prints out a message indicating whether the
number is negative or positive.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – diagram
 Example:
 Write an algorithm
that takes a number
as an input and
prints out a message
indicating whether
the number is
negative or positive.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – diagram
 solution:
num = eval(input())
if num < 0:
print("The number is negative")
else:
print("The number is positive")
print("Good-bye for now")

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – examples
 Example :
 Write an algorithm to compute the absolute value of
a given number.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – examples
 Example :
 Write an algorithm to compute the absolute value of
a given number.

 solution:
Number = eval(input())
if (Number >= 0):
Value = Number
else:
Value = (-1) * Number
print(Value)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – examples
 Example:
 Write an algorithm that gets the age of the patient and the full
adult dose of the drug as inputs.

 If the patient’s age is 12 years or older,


 the algorithm should print the full adult dose

 If the patient’s age is less than 12 years,


 the algorithm should calculate and print the child dose according to
Young’s formula

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – examples
 Example:
 Write an algorithm that gets the age of the patient and the full
adult dose of the drug as inputs.

 If the patient’s age is 12 years or older,


 the algorithm should print the full adult dose

 If the patient’s age is less than 12 years,


 the algorithm should calculate and print the child dose according to
Young’s formula

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – examples
age = eval(input())
adult_dose = eval(input())
if age >= 12:
dose = adult_dose
else:
dose = adult_dose * (age / (age+12))
print("dose :", dose)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – examples
 Example :
 Write an algorithm to convert Euro (EUR) to
Egyptian Pound (EGP) and Egyptian Pound to Euro.
The inputs to your algorithm are the following:
 Amount of money to be converted
 Conversion Type, i. e.:
 1 for EUR to EGP

 2 for EGP to EUR

 Exchange Rate (i. e., a EUR is equivalent to 20 EGP)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional operation – examples
 Solution :

amount, type = eval(input()), eval(input())


Rate = 20
if type == 1:
amount = amount * rate
if type == 2:
amount = amount / rate
print(amount)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Compounded conditions
 Conditions may be compounded using:
 AND, OR, NOT

 E1 or E2:
 True if at least one of them is True;
 False otherwise.

 E1 and E2:
 True if both are True;
 False otherwise.

 not E:
 True if E is False and False if E is True.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Compounded conditions
 Conditions may be compounded using:
 and, or, not
E1 E2 E1 or E2

 E1 or E2: True True True


True False True
 True if at least one of them is True;
False True True
 False otherwise. False False False

 E1 and E2:
 True if both are True;
 False otherwise.

 not E:
 True if E is False and False if E is True.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Compounded conditions
 Conditions may be compounded using:
 AND, OR, NOT

 E1 or E2:
 True if at least one of them is True;
 False otherwise.
E1 E2 E1 and E2
 E1 and E2: True True True

 True if both are True; True False False


False True False
 False otherwise.
False False False

 not E:
 True if E is False and False if E is True.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Compounded conditions
 Conditions may be compounded using:
 AND, OR, NOT

 E1 or E2:
 True if at least one of them is True;
 False otherwise.

 E1 and E2:
 True if both are True;
 False otherwise.

 not E: E not E

 True if E is False and False if E is True. True False


False True

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Compounded conditions –
examples
 Example :
 Find the sum of three positive numbers

 solution:
A, B, C = eval(input()), eval(input()), eval(input())
if (A > 0) and (B > 0) and (C > 0):
Sum = (A+B+C)
print(Sum)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Any Question?

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman

You might also like