[Type B] Python Revision Tour-1 – Chapter 1

|
Table of Contents

2. Predict the outputs of the following programs:-

Question 2. (a):

count = 0
while count < 10 :
    print ("Hello")
    count += 1
Output
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Python

Question 2. (b):

x = 10
y = 0
while x > y :
    print (x, y)
    x = x - 1
    y += 1
Output
10 0
9 1
8 2
7 3
6 4
5 5
4 6
3 7
2 8
1 9
Python

Question 2. (c):

keepgoing = True
x = 100
while keepgoing :
    print (x)
    x = x - 10
    if x < 50 :
        keepgoing = False
Output
100
90
80
70
60
50
Python

Question 2. (d):

x = 45
while x < 50 :
    print (x)

This loop is infinite. There’s no modification of x inside the loop, so x remains 45 and the condition x < 50 stays true forever.

Question 2. (e):

for x in [1,2,3,4,5] :
    print (x)
Output
1
2
3
4
5
Python

Question 2. (f):

for p in range(1, 10) :
    print (p)
Output
1
2
3
4
5
6
7
8
9
Python

Question 2. (g):

for z in range (-500, 500, 100) :
    print (z)
Output
-500
-400
-300
-200
-100
0
100
200
300
400
Python

Question 2. (h):

x = 10
y = 5
for i in range (x - y * 2) :
    print ("%", i)
Output
% 0
% 1
% 2
% 3
% 4
Python

Question 2. (i):

c = 0
for x in range (10) :
    for y in range (5) :
        c += 1
print (c)
Output
50
Python

Question 2. (j):

x = [1,2,3]
counter = 0
while counter < len(x) :
    print(x [counter] * '%' )
    for y in x :
        print(y *'* ')
    counter += 1
Output
%
* 
**
*** 
1
* 
**
*** 
2
* 
**
*** 
3
* 
**
*** 
Python

Question 2. (k):

for x in 'lamp' :
    print(str.upper(x))
Output
L
A
M
P
Python

Question 2. (l):

x = 'one'
y = 'two'
counter = 0
while counter < len(x) :
    print ( x[counter], y[counter])
    counter += 1
Output
o t
n w
e o
Python

Question 2. (m):

x = "apple, pear, peach"
y = x.split(", ")
for z in y :
    print(z)
Output
apple
pear
peach
Python

Question 2. (n):

x = 'apple, pear, peach, grapefruit'
y = x. split(', ' )
for z in y :
    if z < 'm' :
        print(str.lower(z))
    else :
        print(str.upper(z))
Output
apple
pear
peach
GRAPEFRUIT
Python
"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *