G8 Python Q3
G8 Python Q3
continues…
Generate the name of your school 5 times
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)
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
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.
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)