[go: up one dir, main page]

0% found this document useful (0 votes)
9 views32 pages

Presentation - 35 Basic programming constructs

The document outlines a lesson plan for teaching basic programming constructs, specifically focusing on sequence, selection, and iteration using the Python programming language. It includes explanations, examples, and activities for students to practice their understanding of these concepts. Additionally, it concludes with end-of-topic questions to assess comprehension.

Uploaded by

joelmukumba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views32 pages

Presentation - 35 Basic programming constructs

The document outlines a lesson plan for teaching basic programming constructs, specifically focusing on sequence, selection, and iteration using the Python programming language. It includes explanations, examples, and activities for students to practice their understanding of these concepts. Additionally, it concludes with end-of-topic questions to assess comprehension.

Uploaded by

joelmukumba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Teach Computer

Science

Basic programming
constructs

teachcomputerscience.co
m
2

Lesson Objectives

Students will learn about:


▪ Three main programming constructs
▪ Implementing programs in Python programming language

teachcomputerscience.co
m
1.
Content

teachcomputerscience.co
m
4

Introduction
▪ There are three types of basic programming constructs:
a) Sequence
b) Selection
c) Iteration
▪ We will implement these programming constructs in Python.
▪ Python is an open-source, high-level programming language.

teachcomputerscience.co
m
5

Sequence
Execute first
statement
▪ Sequence as the name implies,
is the execution of statements
or functions one after the other. Execute second
statement

Execute third
statement

teachcomputerscience.co
m
6
Sequence: Calculating
area and perimeter of
rectangle
Length and width
are asked
length = float(input('Enter the length of a Rectangle: '))
width = float(input('Enter the width of a Rectangle: '))
Perimeter and area is
Perimeter = 2 * (width + length)
calculated
Area = width * height
print("Perimeter of Rectangle is: %.2f" %Perimeter)
Perimeter and
print("Area of a Rectangle is: %.2f" %Area) area are printed

teachcomputerscience.co
m
7
Sequence: Calculating
area and perimeter of
rectangle

length = float(input('Enter the length of a Rectangle: '))


Output
width = float(input('Enter the width of a Rectangle: :'))
Perimeter = 2 * (width + length)
Area = width * height
print("Perimeter of Rectangle is: %.2f" %Perimeter)
print("Area of a Rectangle is: %.2f" %Area)

teachcomputerscience.co
m
8

Selection

▪ Selection is used to execute only a particular set of statements if a


condition is satisfied.
▪ The program may have many paths from the starting point to the end
point.
▪ The path is chosen according to the condition satisfied.

teachcomputerscience.co
m
9

Selection

Condition
True Fals
e

Execute these Execute these


statements statements

teachcomputerscience.co
m
10

Selection: if-else
structure

weight=int(input("Enter weight in kg:


Weight>85
"))
True Fals
if weight>85: e
print("Do not allow on to ride")
Not allowed to Allowed to
else: ride ride
print("Allow on to ride")

teachcomputerscience.co
m
11

Selection: nested age=int(input("Enter age: "))


if-else structure if age<10:
print("Do not allow on to ride")
else:
▪ In this program, two height=int(input("Enter height in
conditions are checked: age cm: "))
and height.
if (height<130):
▪ A person must more than 10
print("Do not allow on to ride")
years old and his/her height
must be greater than 130 cm else:
to be allowed on the ride. print("Allow on to ride")
teachcomputerscience.co
m
12

Selection: nested age=int(input("Enter age: "))


if-else structure if age<10:
print("Do not allow on to ride")

Output
else:
1 height=int(input("Enter height in
cm: "))
if (height<130):
Output
2 print("Do not allow on to ride")
else:
print("Allow on to ride")
teachcomputerscience.co
m
13

Selection: nested age=int(input("Enter age: "))


if-else structure if age<10:
print("Do not allow on to ride")
else:
▪ An alternate approach to height=int(input("Enter height in
simplify this program is to cm: "))
use Boolean operators. if (height<130):
▪ AND can be used to check print("Do not allow on to ride")
both height and age in a
else:
single statement.
print("Allow on to ride")
teachcomputerscience.co
m
14

Selection: nested
if-else structure

age=int(input("Enter age: "))


▪ An alternate approach to
simplify this program is to height=int(input("Enter height in cm:
use Boolean operators. "))
▪ AND can be used to check if age<10 & height<130:
both height and age in a print("Do not allow on to ride")
single statement.
else:
print("Allow on to ride")
teachcomputerscience.co
m
15

Selection: if… elif…


else… statement if expression1:
statement(s)
elif expression2:
▪ Multiple conditions can be
statements(s)
checked using this structure.
elif expression3:
statement(s)

else:
statement(s) teachcomputerscience.co
m
age=int(input("Enter age: "))
height=int(input("Enter height
in cm: "))
if age < 10:
print("Do not allow on to
ride")
elif height < 130:
print("Do not allow on to
ride")
else:
print("Allow on to ride")
teachcomputerscience.co
m
17

Arithmetic operators in
Python
Operator Function
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder of division (MOD)
// When both operands are positive, // returns whole
number part of quotient (DIV)
** Exponentiation
teachcomputerscience.co
m
18

Iteration

▪ Iteration is used to execute a particular set of statements repeatedly


until a condition is satisfied. There are three ways to create iteration:
a) By counting how many times the statements are to be executed
b) By repeatedly executing the statements until a condition is true
c) By repeatedly executing the statements while a condition is true

teachcomputerscience.co
m
19

Iteration: For structure

▪ Counts how many times the for k in range(1,11):


statements are to be executed. print (k, "times 4 is ", k*4)
▪ Program to print multiplication
table of 4 from 1 to 10 is given.
▪ The value of k is incremented
from 1 to 10 each time the
print statement is executed.
teachcomputerscience.co
m
20

Iteration: For structure

Output: for k in range(1,11):


print (k, "times 4 is ", k*4)

teachcomputerscience.co
m
21

Iteration
▪ In some cases, it is not easy to determine the number of times
the statements are to be executed.
▪ In such cases, we use the second and third approach.
▪ By executing statements until a condition is met or while a
condition is met.
▪ Executing statements until a condition is met is achieved by
using “Repeat until” or “Do while” loop.
▪ This type of loop is not available in Python but is available in
other programming languages like Javascript.
teachcomputerscience.co
m
22

count=0
Iteration: “While” looptotal=0
while (count<5):
▪ Executing statements while a value=int(input("Enter a
condition is met is achieved number: "))
by using “While” loop.
count=count+1
▪ Python code to calculate total
total=total+value
and average of 5 numbers
entered by user is given. print("The total is: ", total)
average=total/count
print("The average is: %.2f"
%average)

teachcomputerscience.co
m
23

Iteration: “While” loop


count=0
▪ Output: total=0
while (count<5):
value=int(input("Enter a
number: "))
count=count+1
total=total+value
print("The total is: ", total)
average=total/count
print("The averageteachcomputerscience.co
is: %.2f"
%average) m
24

Iteration: “While” loop

Condition
True Fals
e

Execute these Execute these


statements statements

teachcomputerscience.co
m
25

Iteration: break
statement

▪ Break statement in Python terminates the current loop and


resumes operation from the next statement.

teachcomputerscience.co
m
26

Let’s review some


concepts

Sequence Selection Iteration


Sequence is the execution of Selection is used to execute Iteration is used to execute a
statements or functions one only a particular set of particular set of statements
after the other. statements if a condition is repeatedly until a condition is
satisfied. satisfied.

teachcomputerscience.co
m
2.
Activity

teachcomputerscience.co
m
28

Activity-1
Duration: 20 minutes

1. Write a Python program to convert temperature from Celsius


to Fahrenheit and vice versa. Your program asks the user to
input temperature either in Celsius, converts to Fahrenheit
and prints the result.
Temperature (⁰F)= Temperature (⁰C) × 1.8 + 32

teachcomputerscience.co
m
29

Activity-1
Duration: 20 minutes

2. Write a Python program to find out the divisors of a number.


3. Write a Python program to find out whether a number is
prime or not.

teachcomputerscience.co
m
3.
End of topic questions

teachcomputerscience.co
m
31

End of topic questions


1. What are the three basic programming constructs?
2. What is sequence programming construct used for?
3. What is the syntax of if… else… statement in Python
programming language?
4. How can multiple conditions be checked in if… else…
statement?
5. What are the different iteration statements available in
Python?
teachcomputerscience.co
6. What is the syntax of “for loop” statement? m
32

End of topic questions


7. How is for loop different from while loop?
8. What is the syntax of while loop used in Python?
9. What is the purpose of break statement in loops?
10.Write a program to find LCM (Lowest common multiple) of
two numbers.

teachcomputerscience.co
m

You might also like