0% found this document useful (0 votes)
25 views25 pages

Lab 02

Uploaded by

hsiddiqui011
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)
25 views25 pages

Lab 02

Uploaded by

hsiddiqui011
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/ 25

Applications of ICT

Lab #2
Dr Asad Mansoor Khan
LE Kashaf Raheem
LAB QUIZ #1
Write a python program which
calculates and displays the total surface
area of a trapezoid. All the values in the
formula should be taken as input from
the user.
• Formula:
𝑇𝑜𝑡𝑎𝑙 𝑆𝑢𝑟𝑓𝑎𝑐𝑒 𝐴𝑟𝑒𝑎
= 𝐴 + 𝐵 + (𝑃𝑒𝑟𝑖𝑚𝑒𝑡𝑒𝑟 × 𝐻𝑒𝑖𝑔ℎ𝑡)
2
Algorithms and
Decision Making
Aim
Understand the importance and use of decision making in problem
solving

4
Decision Making
• Selecting one choice from many
based on a specific reason or
condition
 If something is true, do A … if
it’s not, do B

• What are some real-life examples?


 Walking around campus
(construction!)
 Choosing where to eat lunch

5
Decision Making: Pseudocode
Problem Solution

Answer the
PositiveNumberCheck
1. Display “Enter the number: ”

question “Is 2. Get the number (call it num)


If num > 0

a number
3.

4. Display “It is positive”

positive?”
5. Else
6. Display “It is negative”
End

6
Representation: Decision Making

7
Decision Making: Flowchart
Display “Enter Get the
Start the number: ” number

TRUE FALSE
num > 0
Display Display
“It is positive” “It is negative”

End
8
Class Activity

Design an algorithm
that checks if
entered number is
even or odd.
9
Python Syntax

10
Python Basics
• Indentation controls everything
 A complete lack of braces {} to indicate code blocks
 Instead, indentation indicates the start and end of a
code block

• Lines of code that begin a block (often) end in a colon (:)


 The first line with less indentation is outside the block
 The first line with more indentation is inside the block

• A newline (Return) is used to indicate the end of a single


line of code
Conditional Statements
if condition:
statements
else:
statements

if a >= 0:
print(“a is a positive number”)
else:
print(“a is a negative number”)

12
Indentation and Conditional
Statements!!!
Incorrect Version Correct Version
a = 33 a = 33

b = 200 b = 200

if b > a: if b > a:
print("b is greater than a") # you will print("b is greater than a") # you will get
get an error an error

13
Conditional Operators

Operator Example Meaning


> (A > B) A is Greater than B
< (A < B) A is Less than B
== (A == B) A is Equal to B
!= (A != B) A is Not Equal to B

14
“Is a number positive?”: Python Code

Pseudocode Python Code:


• PositiveNumberCheck • n = input("Enter the number")

• Display “Enter the number: ” • if float(n)>0:

• Get the number (call it num) • print("positive")

• If num > 0 • else:

• Display “It is positive” • print("negative")

• Else

• Display “It is negative”

• End
Improved Version
• n = input("Enter the number")

• if float(n)>0:

• print("positive")

• elif float(n)==0:

• print("zero")

• else:

• print("negative")

16
Class Activity

Even/ODD Problem Python Code


• EvenOddCheck • n = input("Enter the number")

• Display “Enter the number: ” • if float(n)%2 == 0:

• Get the number (call it num) • print(“Even")

• If num % 2 == 0 • else:

• Display “It is even” • print(“Odd")

• Else

• Display “It is odd”

• End

17
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
18
Task 1:
Write a python
script that asks Operation Operation
user to select one of Number

the following 0 Addition

operations and 1 Subtraction

performs the said 2 Multiplication

operation on 2 3 Division

inputs provided by
user.
19
Task 2:
Modify last weeks grocery bill task such
that the cashier enter type of item and its
quantity to get the payment. Program
allows getting payment per item.
Item Type Item Unit Price Quantity
Multiple
0 Eggs (dozen) 300 Dozen
1 Bread 200 Single
2 Butter 400 Single
3 Jam 400 Single
20
Logical Operators
Operator Example Meaning
and (A > B) and (A != 0) A is Greater than B and A is not equal
to zero
or (A > 0) or (A == 0) A is Greater than or equal to zero

Input Validation
Input validation is the process of analyzing inputs and disallowing
those which are considered unsuitable.

21
Example (including input validation)
Write a program that takes a
student's score as input and prints
grades as A, B, C, D, or F
depending upon the thresholds
given below.

A: 100 - 90 C: <80 - 70
B: <90 - 80 D: <70 – 60
F: <60 - 0
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 3:
Design an algorithm and Write a
program that takes three nonzero
integers as input and determines and
prints whether they’re the sides of a
right triangle.

24
Task 4:
Draw flowchart and Write a
program that inputs temperature and
value of humidity from user and
display related forecast.
Temperature Humidity Forecast
Greater than 35 50% to 60% Hot Day
Between 25 and 35 35% to 50% Pleasant Day

Less than 25 Less than 50% Cool Day


26
Next week: Repetition
Operators!
Advice:
Install latest version of IDLE on your laptops
Link: https://www.python.org/downloads/
Or VS Code and integrate python
30

You might also like