0% found this document useful (0 votes)
23 views70 pages

GR 7-5.print - Input - Flow of Control - Trace Table

Uploaded by

Sreehari
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)
23 views70 pages

GR 7-5.print - Input - Flow of Control - Trace Table

Uploaded by

Sreehari
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/ 70

Print Statement

Kindly Open Python IDE –replit


Practical session on Program 1,2,3
Program 1:
print(12312+5)
print(12312-5)
print(12312*5)
print(12312/5)
print(12312%5)
print(2**5)

Program 2: Write a program to print your name 10 times


>>> print("Sam" *10)
Program 3: Guess the output of the following print statement and
verify your answer on the computer.
>>>print(2+2*2 )
>>>print(4%2)
>>>print(3**4/9)
Input Statement

Reading INPUT from the keyboard

Input(): Pythons inbuilt function to


read input from the keyboard.

Syntax:

Variable = datatype (input())


Reading Numbers with the input
Function

Variable name =int (input())


Ex: a= int (input())

Variable name = float(input())


Ex: avg= float (input())
HW: Basic Programming worksheet 5
TYPES OF CONTROL STRUCTURES
TYPES OF CONTROL STRUCTURES

A Structured programming is an important feature of


a programming language which comprises following
structure:
1. SEQUENCE

2. SELECTION

3. ITERATION OR LOOPING
SEQUENC SELECTIO ITERATIO
E N N

CONDITIONAL AND ITERATION


1. SEQUENCE
1. SEQUENCE – FLOW CHART

Statement 1

Statement 2

Statement 3
SEQUENC
E
1. SEQUENCE - PROGRAM
1. SEQUENCE

Sequence is the default control structure;


instructions are executed one after another.
Sequence– the order we want the computer to
execute the instructions we provide as programmers.
For example, do this first, then do this, then do that,
and so forth.
Statement 1
Statement 2
Statement 3
……..
……..
……..
 Class work and
 For pseudocode -Keywords – are reserved words
and always written in Upper case. Example: -
INPUT, OUTPUT, IF, ELSE etc.
1.Draw a flowchart write a python program
and pseudocode to input the side of a square
and prints it area and perimeter.
Area of square = a* a
Perimeter =4*a, a =length of side
Basic Programming worksheet
4
Python program -Task 1 and task
2 in replit and in notebook.
Basics of programming
worksheet 5 .Pg no 3
2. SELECTION
A selection statement causes the
program control to be transferred to a
specific flow based upon whether a
certain condition is true or not.

Selection– selecting which path of an


algorithm to execute depending on some
criteria. For example, if you Complete the
worksheet, then we execute the operations
that clap and cheer and play a song. But if
you didn’t complete the worksheet, then
maybe we would say, “Do it next time,”
CONDITIONAL CONSTRUCT – if else
STATEMENT
CONDITIONAL CONSTRUCT – if else STATEMENT

Conditional constructs (also known as


ifelse statements) provide a way to execute a
chosen block of code based on the run-time
evaluation of one or more Boolean expressions.
CONDITIONAL CONSTRUCT – if else STATEMENT

FLOW CHART

False
Condition ? Statement 1 Statement 2

Main True
Body
Statement 1

else
Statement 2 body
CONDITIONAL CONSTRUCT – if else STATEMENT

Each condition is a Boolean expression, and


each body contains one or more commands that
are to be executed conditionally.

If the first condition succeeds, the first body


will be executed; no other conditions or bodies
are evaluated in that case.
CONDITIONAL CONSTRUCT – if else STATEMENT

If the first condition fails, then the process


continues in similar manner with the evaluation
of the second condition. The execution of this
overall construct will cause precisely one of the
bodies to be executed.

There may be any number of elif clauses


(including zero), and
 The final else clause is optional.
CONDITIONAL CONSTRUCT

EXAMPLE – if else STATEMENT


Python Syntax

if (condition):
#statements to be executed when condition is True.
else:
#statements to be executed when condition is False.

Note: It is important to note the indentation and colon symbol in this


structure.
EXAMPLE – if else STATEMENT

: Colon Must
Use
indent

else is
used
OUT
PUT
Try Example

name = "Luigi"
if name == "Mario":
print("It’s same, Mario!")
else:
print("I'm not Mario ;_;")
Keywords – are reserved words and always written in Upper case.
Example: - INPUT, OUTPUT, IF, ELSE etc.
Example: To find the greater number between two numbers.
This program represented in pseudocode would look like this:
START
OUTPUT “Enter two numbers”
INPUT a, b
IF a > b THEN
OUTPUT “a is greater”
ELSE
OUTPUT “b is greater”
ENDIF
END
Flow Chart
HW-Basic Programming
Worksheet 6
Trace Table

A trace table is a technique used to test an algorithm


and predict step by step how the computer will run
the algorithm. It can be used to understand or predict
what an algorithm is doing and to identify potential
logic errors. The manual exercise of working through
an algorithm step by step is called dry run.
Example:

Example: Trace table

a b Output
Largest
5 3 a is largest
8 9 b is largest
HW: Basic Programming Worksheet 7
Nested if statement:

There may be a situation when you want to check for


another condition after a condition resolves to true.
In such a situation, you can use the nested if
construct. In a nested if construct, you can have an
if...elif...else construct inside another if...elif...else
construct.
CONDITIONAL CONSTRUCT

EXAMPLE – Nested if STATEMENT


Nested if structure: Example

temp=float(input("Enter temperature:"))
if (temp >= 100):
if (temp == 100):
print("Temperature is equal to boiling point of water")
else:
print("Temperature is greater than boiling point of water")
else:
print("Temperature is less than boiling point of water")
Note: 100 C
Elif Statement:

The elif keyword is way of saying "if the previous


conditions were not true, then try this condition".
CONDITIONAL CONSTRUCT – if else
STATEMENT
: Colon Must

if first condition:
first body
elif second condition:
second body
elif third condition:
third body
else:
fourth body
EXAMPLES – if elif STATEMENT

READ AS
18 is less
than age
and
18 is less
than 60

OUTPU
T
Python program

age=int(input("Enter age"))
if (age >= 60):
print("Senior Discount")
elif (18<= age and age<60):
print("No Discount")
else:
print("Junior Discount")
HW :
Basic Programming worksheet
8
3. ITERATION OR LOOPING

ITERATIO
N
ITERATION OR LOOPING

What is loop or iteration?

Loops can execute a block of code number of


times until a certain condition is met.
OR
The iteration statement allows instructions to be
executed until a certain condition is to be fulfilled.
The iteration statements are also called as
loops or Looping statements.
ITERATION OR LOOPING

Python provides two kinds of


loops & they are,

while loop

for loop
for LOOP
for LOOP

For loop is used to iterate a set of statements a


certain number of times.
▪ The basic syntax of for loop is:
for count in range (a, b):
#statements executed starting count=a and
incrementing every time the set of statements
are executed until count=b-1
for LOOP - range KEYWORD

The range() function returns a


sequence of numbers, starting from 0 by
default, and increments by 1 (by default),
and ends at a specified number.

range(start, stop, step)

for n in range(3,6):
print(n)
for LOOP - range KEYWORD

#Generating series of numbers

: Colon Must

Use
indent

OUTPU
T
range(start, stop, step)

OUTPU
T
Input range

i=int(input("Enter the number"))


for n in range(0,i):
print(n)
Lab Activity -Try the example
slide no. 45,46
Revision Worksheet
For loop: Example 1

▪ For loop is used in several programs where the programmer


knows exactly how many times the set of statements must be
repeatedly executed.
▪ In case the programmer wants to start the value of count from 0,
then he can simply use the statements given,

for count in range (5): count is initialised


with value 0 and is
print ("The count is ", count) incremented every
time the statement
inside for loop is
executed until count
= 4.
For loop: Example 2

▪ In the previous examples, count is incremented by 1.


▪ In case the programmer wants to increment by 3 (or any other
value), the following statement is used,

for count in range (2, 10,3):


print("The count is ",count)

count is initialised with 2.


count is incremented by 3 until count=8.
For loop: Example 3

▪ A variable can also be counted down using this structure.


For example,

for count in range (6, 0, -1):


print("The count is ", count)

count is initialised with 6.


count is decremented by 1 until count=1.
Practical session on example 1,2,3

Basics of Programming -Worksheet 10-


Q1 to Q4 in class. Execution and
writing the answer.
HW:

Basics Programming -Worksheet 10

Q5,Q6 and Q7
Cake Sale

You are organising a cake sale and


want to predict how much money you
will raise.
During this cake sale you will be
selling the following three types of
cakes:

Cake Sale | 101 Computing


Input
You decide to write a python script that asks the user three questions:
How many cupcakes do you plan to sell?
How many macarons do you plan to sell?
How many cheesecake do you plan to sell?
Process
The python script will then calculate the total money raised using the three
user inputs and the prices as follows:
Cupcakes: 40p per cupcake,
macarons: 50p per macaron,
cheesecake: 70p per slice.
Output
The script will finally display this information (total money raised) to the
end-user.
Testing

Once your code is done, complete the following tests to check that your code is working as it
should:
#Cake Sale Challenge
cupcakePrice = 0.40
macaronPrice = 0.50
cheesecakePrice = 0.70

#Step 1:
Inputcupcakes = int(input("How many cupcakes do you plan to sell?"))
macarons = int(input("How many Macarons do you plan to sell?"))
cheesecake = int(input("How many cheesecake do you plan to sell?"))

#Step 2: Process
total=cupcakes*cupcakePrice+macarons*macaronPrice+cheesecake*cheesec
akePrice

#Step 3: Output
print("In this cake sale you will raise:")
print(total)
Iteration
▪ Iteration is the repeated execution of a
set of statements. There are two ways
to create iteration in Python:
• By counting how many times the
statements are to be executed
• By repeatedly executing the
statements while a condition is true
Types of iteration
▪ Definite iteration: A for
loop is used to iterate a
set of statements for a
specific number of
times.
▪ Indefinite iteration: A
while loop is used to
iterate a set of
statement while a
While loop
condition is true.
Basics Programming -Worksheet 12
in class
For loop- 63
Recap

▪ For loop is used to iterate a set of statements a certain


number of times.
▪ The basic syntax of for loop is:
for count in range (a, b):
#statements executed starting count=a and incrementing
every time the set of statements are executed until
count=b-1
HW:

Basics Programming -Worksheet 11


and 12
The while loop
▪ In some cases, it is not easy to determine the
number of times the statements are to be
executed. It is termed as indefinite iteration.
▪ In such cases, we use the while loop. Also
can be used for definite iteration as given
below substituting for loop
▪ In while loop, statements are executed
repeatedly while a condition is true.
Definite Iteration Example:
count=0
while (count<5):
print("count = ", count)
count+=1
Can be represent as [ count =
count +1]
Indefinite Iteration Example 1:​
n = int(input("Enter a
number"))
while n != -1:
print(n)
n=int(input("Enter a
number:"))
print('Loop ended.')
# program to calculate the sum of numbers
# until the user enters zero

total = 0

number = int(input('Enter a number: '))

# add numbers until number is zero


while number != 0:
total += number # total = total + number

# take integer input again


number = int(input('Enter a number: '))

print('total =', total)


Review
Worksheet 12 and explain
the concept of while loop
for the same
HW:

Basics Programming -Worksheet 13


Thank You

You might also like