Presentation - 35 Basic programming constructs
Presentation - 35 Basic programming constructs
Science
Basic programming
constructs
teachcomputerscience.co
m
2
Lesson Objectives
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
teachcomputerscience.co
m
8
Selection
teachcomputerscience.co
m
9
Selection
Condition
True Fals
e
teachcomputerscience.co
m
10
Selection: if-else
structure
teachcomputerscience.co
m
11
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
if-else structure
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
teachcomputerscience.co
m
19
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
Condition
True Fals
e
teachcomputerscience.co
m
25
Iteration: break
statement
teachcomputerscience.co
m
26
teachcomputerscience.co
m
2.
Activity
teachcomputerscience.co
m
28
Activity-1
Duration: 20 minutes
teachcomputerscience.co
m
29
Activity-1
Duration: 20 minutes
teachcomputerscience.co
m
3.
End of topic questions
teachcomputerscience.co
m
31
teachcomputerscience.co
m