Python Revision Tour-1
Python Revision Tour-1
1. What are tokens in Python? How many types of tokens are allowed
in Python? Exemplify your answer.
Answer =
Token are smallest individual unit in a program.
Type of tokens:-
1 keywords : False, True , for , while
2 identifiers : a , A , lst , dic
3 literal : “python”,5,9, ‘class11’
4 operator : +,-,/,*,**,%,//
5 punctuators :&,^,?,#,@,!
11. What are variables? How are they important for a program?
Answer =
Variable is a label that can used in process of program. it can contain numeric values, character, string is
called variable.
Variable play very important role in programming because they enable programmers to write flexible
programs. without variable we can not enter the data directly in the program
They have a very important role in programming because without any data of the program, the program can
cause errors, so the program needed data.
14. How many integer types are supported by Python? Name them.
Answer =
Two type of integer are supported by python
(I)Integer (signed)
(II)Booleans
Mutable type: -
Those type whose value can be change is known as mutable type.
Example = list, dictionary, set
2. It will never modify, so we can use it in our whole program without any fear.
3. Immutable objects are thread safe. The state of an object cannot be changed after its construction. This
implies that read-only data is shared among threads, which provides thread safety. Operations involving
mutations like (changing value at a particular index) can be implemented in such a way that they create
new objects instead of modifying the existing ones.
Example = in looping
Example =
(vi) L = inputline.split()
while L != ( ) :
print (L)
L = L[1:]
Answer = list of str
(vii)
L= [ 'Hiya', Zoya', 'Preet' ]
print (L[0] + L[1] )
Answer = str
TYPE-B
1. Fill In the missing lines of code in the following code. The code reads in a limit amount a prices and
prints the largest price that is less than the limit. You can assume that all prices and the limit are
positive numbers. When a price 0 is entered the program terminates and prints the largest price that
is less than the limit.
(a)
count = 0
while count < 10 :
print ("Hello")
count += 1
(b)
x = 10
y=0
while x > y :
print (x, y)
x=x-1
y += 1
(c)
keepgoing = True
x = 100
while keepgoing :
print (x)
x = x - 10
if x < 50 :
keepgoing = False
(d)
x = 45
while x < 50 :
print (x)
(e)
for x in [1,2,3,4,5] :
print (x)
(f)
for p in range(1, 10) :
print (p)
(g)
for z in range (-500, 500, 100) :
print (z)
(h)
x = 10
y=5
for i in range (x - y * 2) :
print ("%", i)
(i)
c=0
for x in range (10) :
for y in range (5) :
c += 1
print (c)
(j)
x = [1,2,3]
counter = 0
while counter < len(x) :
print(x [counter] * '%' )
for y in x :
print(y *'* ')
counter += 1
(k)
for x in 'lamp' :
print(str.upper(x))
(l)
x = 'one'
y = 'two'
counter = 0
while counter < len(x) :
print ( x[counter], y[counter])
counter += 1
(m)
x = "apple, pear, peach"
y = x.split(", ")
for z in y :
print(z)
(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))
Answer =
(a)
Output:-
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
>>>
(b)
Output: -
10 0
91
82
73
64
>>>
(c)
Output: -
100
90
80
70
60
50
>>>
(d)
Output: -
45
45
45
45
45
45
45
45
45 infinite times.
>>>
(e)
Output: -
1
2
3
4
5
>>>
(f)
Output: -
1
2
3
4
5
6
7
8
9
>>>
(g)
Output: -
-500
-400
-300
-200
-100
0
100
200
300
400
>>>
(h)
It will give no output because the precedence of the * operator is more than the - operator.
So for value in the range becomes zero after solving.
(i)
Output: -
50
>>>
(j)
%
*
**
***
%%
*
**
***
%%%
*
**
***
>>>
(k)
L
A
M
P
>>>
(l) o t
nw
eo
>>>
(m)apple
pear
peach
>>>
(n)apple
PEAR
PEACH
grapefruit
>>>
3. Find and write the output of the following python code:
for Name in ['Jayes', 'Ramya', 'Taruna', 'Suraj'] :
print (Name)
if Name[0] == 'T' :
break
else :
print("Finished!")
4.How many times will the following for loop execute and what's the
output?
(i)
for i in range(-1, 7, -2) :
for j in range (3) :
print(1, j)
(ii)
for i in range (1, 3, 1) :
for j in range (i+1) :
print('*')
Answer =
(a)
There is no output because of invalid range command.
(b)
This program execute 5 times
Output -
*
*
*
*
*
>>>
Explanation :-For i = 1
for j in range (2):
So , j loop will execute 2 times.
For i = 2
for j in range(3):
So, j loop execute 3 times.
So, total no. Execution is 5.
Is the loop in the code below infinite? How do you know (for sure)
5.
before you run it?
m=3
n=5
while n < 10 :
m=n-1
n=2*n-m
print(n, m)
Answer :-
This program executes finite because while loop has the condition (n<10). So when n becomes greater than
10 then the program break.
Output :-
64
75
86
97
10 8
>>>
TYPE-C:
1.Write a program to print one of the words negative ,zero or
positive. according to whether variable x is less than 0, 0 or greater
than 0 respectively.
Answer :-
Output :-
if x%2 == 0:
print ("True")
else :
print ("False")
Output :-
print ( "for non leap year = " ,60*60*24*365, " , for leap year = ", 60*60*24*366 )
Output :-
4.Write a python program that accepts two integers from the user
and print a message saying if first number is divisible by second
number or if it is not.
Answer :-
first = int(input("Enter the first number "))
second = int(input("Enter the second number "))
if first % second == 0 :
print ("first number is divisible by second number ")
else :
print ("first number is not divisible by second number ")
Output :-
5.Write a program that ask the user the day number in the year in
the range 2 to 365 and ask the first day of the year Sunday or
Monday or Tuesday e.t.c . then the program should display the day
on the day number that has been input.
Answer :-
d = {"Sunday":1,"Monday":2,"Tuesday":3,"Wednesday":4,"Thursday":5,"Friday":6,"Saturday":7}
days = { 1 :"Sunday",2:"Monday",3:"Tuesday",4:"Wednesday",5:"Thursday",6:"Friday",7:"Saturday"}
dic= { }
user = int(input("Enter a number between 2 to 365 = "))
day = input("Enter first day of year (Sunday, Monday, Tuesday,Wednesday,Thursday,Friday,Saturday)")
first = d[day]
for j in range(1,366) :
dic[ j ] = first
if first == 7 :
first = 0
first += 1
a = dic[ user ]
print()
print(days [ a ])
Output :-
Thursday
>>>
Enter a number between 2 to 365 = 100
Enter first day of year (Sunday, Monday, Tuesday,Wednesday,Thursday,Friday,Saturday)Monday
Tuesday
>>>
def inch(feet): #1
return feet*12
def ask(): #2
ft=int(input("Enter number in feet :-"))
return ft
def inch2(inch):
print(inch ,"Inches")
inch2(inch(ask()))
Output :-
sum = 0
n = int(input("Enter the number :-"))
if n > 0 :
for i in range (n,n*2+1):
sum += i
else :
for i in range (2*n, n+1):
sum += i
print (sum)
Output :-
mon = ["January", "February","March" , "April", "May" , "June" , "July" , "August ", "September", "October",
"November" , 'December']
month = a // 1000000
date = (a % 1000000) // 10000
year = a % 10000
Output :-
December 25 , 2019
>>>
Enter the date = 09162021
September 16 , 2021
>>>
9. Answer :-
mon = ["January", "February","March" , "April", "May" , "June" , "July" , "August ", "September", "October",
"November" , 'December']
month = a // 1000000
date = (a % 1000000) // 10000
year = a % 10000
Output :-
Answer :-
print("Miles\t | \tKilometer")
Output :-
Miles | Kilometer
1 | 1.6093
10 | 16.093
100 | 160.93
1000 | 1609.3
10000 | 16093.0
100000 | 160930.0
1000000 | 1609300.0
>>>
10. Answer :-
print("Miles\t | \tKilometer")
Output :-
Miles | Kilometer
1 | 1.6093
10 | 16.093
100 | 160.93
1000 | 1609.3
10000 | 16093.0
100000 | 160930.0
1000000 | 1609300.0
>>>
Answer :-
print("Pounds\t | \tKilogram")
Output :-
Pounds | Kilogram
0 | 0.0
5 | 2.25
10 | 4.5
15 | 6.75
20 | 9.0
25 | 11.25
30 | 13.5
35 | 15.75
40 | 18.0
45 | 20.25
50 | 22.5
55 | 24.75
60 | 27.0
65 | 29.25
70 | 31.5
75 | 33.75
80 | 36.0
85 | 38.25
90 | 40.5
95 | 42.75
100 | 45.0
>>>
a = sec - first
Output :-