CO Lab 3
CO Lab 3
Lab #3
Dr Sajid Gul Khawaja
LE Kashaf Raheem
Points of discussion
› Previous Syntax
› Task Evaluation
› Contact decorum
2
IF/else if/else Statement
str = input()
if str =="Hello":
doSomething()
elif str =="World":
doSomethingElse()
else:
doTheRightThing()
3
Condition Nesting
5
What is Nested if/else
Example Code
> There may be a situation when › var = 100
› if expression1:
you want to check for another › if var < ›200:statement(s)
condition after a condition – print "Expression value is less than 200"
› if expression2:
resolves to true. – if var == 150:
> In such a situation, you can use › ›print "Which
statement(s)
is 150"
– elif var == 100:
the nested if construct. › elif expression3:
› print "Which is 100"
› == statement(s)
– elif var 50:
› print "Which is 50"
› else:
– elif var < 50:
› ›print "Expression
statement(s)
value is less than 50"
› else: › else:
– print› "Could not find true expression"
statement(s)
› print "Good bye!"
6
Example: Nested if/else
QUESTION SOLUTION
› Write a program that gets an num = float(input("Enter a number:
input number and check if the "))
number is positive or negative or if num >= 0:
zero and display an appropriate if num == 0:
message using nested if/else print("Zero")
statement else:
print("Positive number")
else:
print("Negative number")
7
Algorithms and Repetition
Aim
Understand the importance and use of
repetition structures in problem solving
8
Why are Repetition Structures Useful?
› Sometimes, based on some
criterion, one task must be
repeated several times
› That is where the repetition
structures are useful
9
Looping
› Doing something repeatedly
10
Looping: Example
Write an algorithm that counts from 1-20
11
Looping: Pseudocode
PROBLEM SOLUTION
› Write an algorithm that counts › Start with a plain English
from 1-20 description
1. Set num = 1
2. While num <= 20
3. Display num
4. num = num + 1
5. (End loop)
12
Looping: Flowchart
Start num = 1
There’s an error in this
flowchart… do you see
it?
TRUE Display
num >= 20 num
num = num + 1
FALSE
End 13
Looping: Flowchart
Start num = 1
TRUE Display
num <= 20 num
num = num + 1
FALSE
End
14
Class Activity
› Design an algorithm that generates the following
sequence and sums it up as well.
𝑓 𝑛 = {1, 2, 3, … 𝑛}
15
Python Syntax
16
Loops in Python
› while (loop until condition changes)
› for (iterate over iteraterable object)
17
While Loop
while(condition):
Statement(s) belonging to while loop
Statements outside while
› Examples:
– Write a python code that print 10 numbers
– Write a python code that takes 10 inputs from user and prints them
18
WHILE Statement: Example 1
QUESTION SOLUTION
› Write a python code that N= int(input("Enter N: "))
generates 1-N numbers where count = 0
“N” is entered by the user!
while count!=N:
count = count + 1;
print (count)
print ("Done")
19
Example 2:
QUESTION SOLUTION
› Write a program that reads 10 count = 0
positive numbers from the
keyboard and determines and sum = 0
displays the sum and average of while (count < 10):
the numbers number = input("Input a new
number: ");
sum = sum + int(number)
count = count + 1
print ("Sum of 10 numbers is: ",
sum)
20
Class Activity
› Write python code that generates the following
sequence and sums it up as well.
𝑓 𝑛 = {1, 2, 3, … 𝑛}
21
Class Activity
› Design an algorithm and write python script that prints all
even numbers from 𝟏 till 𝑵.
22
Tasks
Make sure to comment your code.
Use appropriate variable name
Know the location where file is being saved
Create different files for different tasks
23
Task 1:
› Design an Algorithm and write a program in python to
print multiplication table of a given number
24
Note: 𝑝, 𝑟, and 𝑛 are user defined
Task 2:
› A person invests $𝟏𝟎𝟎𝟎 in a savings account yielding 𝟓% interest.
Assuming that all interest is left on deposit in the account, calculate and
print the amount of money in the account at the end of each year for
𝟏𝟎 years. Use the following formula for determining these amounts:
𝑎 = 𝑝 1+𝑟 𝑛
› Where:
– p is the original investment (i.e., the principal)
– r is the annual interest rate
– n is the number of years
– a is the amount on deposit at the end of the 𝒏𝒕𝒉 year
Sample Output
Task 3:
› Write a program to check whether a number is Armstrong or
not.
(Armstrong number is a number that is equal to the sum of
cubes of its digits for example : 153 = 1^3 + 5^3 + 3^3)
28
More Examples
PROBLEM SOLUTION
› Use a value that will always be while True:
true to loop until a “break”.
string = input()
if string==“exit”:
break
print (string)
print ("Done”)
30
Example 3:
QUESTION SOLUTION
› Write a python code that stops str=""
whenever “quit” is entered by while str!="quit":
the user! str = input();
print (str)
print ("Done“)
31
Next week: More on
Repetition Operators!
Advice:
Explore Leetcode for problem solving!
32