0% found this document useful (0 votes)
3 views

G8 Python Q3

The document provides programming exercises focusing on loops in Python, including generating repeated outputs, summing even numbers, and using accumulator variables. It also covers the use of while loops, built-in functions, and string manipulation techniques. Additionally, it includes practice tasks for debugging and converting code between loop types.

Uploaded by

vvmprivate
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

G8 Python Q3

The document provides programming exercises focusing on loops in Python, including generating repeated outputs, summing even numbers, and using accumulator variables. It also covers the use of while loops, built-in functions, and string manipulation techniques. Additionally, it includes practice tasks for debugging and converting code between loop types.

Uploaded by

vvmprivate
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Programming - Q3

continues…
Generate the name of your school 5 times

#Generating School Name


for x in range (1,6) :
print(“B.D.Somani International School”)

Output of the above program

B.D.Somani International School


B.D.Somani International School
B.D.Somani International School
B.D.Somani International School
B.D.Somani International School
Remember :
● When a Number series generation is needed to be displayed, most of
the times the loop control variable is displayed to get the number series.
● Also when a number series is generated through a loop control variable,
the loop control variable can either incremented or decremented with a
value required for that program.
● When a process of doing a task is needed to be repeated, most of the
times loop control variable begins with 1 and gets increment by 1. So
writing it in the code is optional.
Activity Set
1. Accept any word and print it 20 times
2. Print the 3 times table where the last output of the table will be the
user’s choice.
3. Print the name of a person’s hobby as many times as the person wants.
Adding the first 10 even numbers

#Sum of first 10 even numbers


s=0 # accumulator variable to add the even numbers
for x in range(2,21,2):
s=s+x
print("Sum of the first 10 even numbers: ",s)

Output of the above program


Sum of the first 10 even numbers: 110
Accumulator Variable
● In a code, to gather and accumulate numeric data or value the
accumulator variable is required.
● The accumulator variable is mostly used to gather the sum of a set of
numeric data and sometimes to gather the product of set of numeric
date.
● The accumulator variable must always be initialized with a starting
value.
● When it is used for sum the initial value for the accumulator is 0 and
when it is used for product the initial value for the accumulator is 1.
Product of the odd numbers between 11 to 15

#Product of odd numbers (11 to 15)


p=1
for n in range (11,16, 2):
p=p*n
print("Product of odd numbers between 11 to 15= ", p)

Output of the above program :


Product of odd numbers between 11 to 15=2145
Adding any 5 numbers accepted
#Adding any 5 numbers
s=0
for x in range (1,6,1):
n=int(input("Enter any number :"))
s=s+n
print()
print("Sum of the 5 numbers inputted
", s)
Output of the above program : WIll
be the total of the 5 numbers the user gives to the computer
Q3 -FA
Debug Dry Run
a=1
m=0
for n in range(1,10) : for t in range(20, 11, -
b= input(“ Number of chocolates bought :”) 3):
A =a + b m=m+t
print (“Total chocolates bought by 10 print (t)
people”, a)
print()
print (m)
Hint: The correct code should calculate the
total chocolates bought by 10 different people.
The output should be shown only once.
Q3 -FA-Solution

Debug(Possible Solution)
Dry Run(Output)
a=0 20
for n in range(1,11) : 17
14
b= int(input(“ Number of chocolates
bought :”))
51
a =a + b
print (“Total chocolates bought by 10 people”,
a)
While Loop
With the while loop we can execute a set of statements as long as a
condition is true. It ends when the condition is met.
While Loop Syntax
a is control variable
(numeric)

1 is starting value (can be different according to


requirements)
a= 1
while a < 6:
print(a) Loop
a+= 1 body

Increment (or decrement step)

Condition checking with final


value
Same problem with for loop and while loop
#Generating City Name with while
#Generating City Name with for loop
loop
a= 1
for x in range (1,6,1) :
while a < 6:
print(“Mumbai”) print(“Mumbai”)
a+= 1
OR
#Generating City Name with while
loop

a= 1
while a < =5:
print(“Mumbai”)
a+= 1
Built-in or In-built functions are pre-defined
functions provided by the Python language that can be
used to perform different tasks.
Some Common Examples :
input() len() : returns the number of items which includes
print() alphabets, numbers, special characters and space
within a string.
int()
Example for function len
float()
a="school"
str()
l=len(a)
upper()
print(l)
lower()
The output of the above program is 6
Extracting each characters from a string data

a="computer" Character c o m p u t e r
j=len(a)
x=0 Position or 0 1 2 3 4 5 6 7
while x<j: index
print(a[x]) This technique is called slicing
x+=1 where from a string data a
Output of the above character is extracted based on
its position is written within a
program : pair of square brackets.
c
o
m
p
u
t
e
r
Adding all the numbers in a string number

x="456279"
g=len(x)
s=0
for z in range (0,g,1):
n=int(x[z
n=(x[z])
])s=s+n
print("Sum of all the numbers in", x,"=",s)
Output of the above
program :
Sum of all the numbers in 456279 = 33
Dry Run

x="625 G.D.Somani Marg"


g=len(x)
m = g -1
for t in range (m, 0, -4):
n=(x[t])
print(n)
Output of the above
program :
g

m
D
5
Write a python code to print the name of your country through
a variable. Print it as many times a user wants.

Possible Python Code


#Printing country name for different time
c="India"
t=int(input("How many times the word is to be
printed?"))
for x in range (1,t+1,1):
print("My country's name is:",c)
Practice Task : Debug and then dry run the
correct code. Also convert it to while loop.

Hint: The correct code should print the 1st, 3rd, 5th, and so
forth with a gap of 2 till the end of the word stored in variable
h.

h = Tea or Coffee?
j = len[h]
for s in range (j, 0, 2):
t =(h[s])
Practice Task :Debug and then dry run the
correct code. Also convert it to for loop.

Hint: The correct code add all the characters stored in the number
in variable D.

D = 456
q=0
f=0
u =len(D)
while q<u :
x = D([q])
f=f+x
Q- = 1
print(f)

You might also like