Python Revision Tour-I Class 12 Solved Assignment

Solved Assignment of Computer Science Python Revision Chapter Tour – I for CBSE Class 12 students. Assignment includes short question answer, long questions and program.

Short Answer Question :Python Revision Tour Class 12 Solved Assignment

Q1.What are tokens in python? How many types of tokens are allowed in python? Exemplify your answer.

Ans: The smallest individual unit in a program is known as a Token or a lexical unit.
Python has following tokens:
1.Keywords
2.Identifiers
3.Literals
4.operators
5.punctuators

Python Revision Tour Class 12 Solved Assignment

Q.2. How are keywords different from identifiers?
Answer:
Keyword are predefined words with special meaning to the language to interpreter. They are reserved for special purpose and must not be used as normal identifier name.
Identifiers are the names given to different parts of the program like variable, objects, classes, functions, list.

Q.3. What are literals in Python? How many type of literals are allowed in python?
Answer:
Literals are data items that have a fixed/constant value.
Four type of literal are allowed in python.
a)string literals
b)Numeric literals
c)Boolean Literals
d)Special literal None

Q,4. Can non graphic characters be used and processed in Python? How? Give examples to support your answer.
Answer:
yes, Non graphic characters be used and processed in python.
print(“good /n morning”) its output is
good
morning #here /n is new-line character.

Q.5. Out of the following, find those identifiers, which cannot be used for naming variable or Function in a Python program:
Price*Qty,class,for,do,
4thCol, totally, Row31, _Amount
Ans:

Price*Qty cannot be used because ‘*’ is not allowed in as identifier name
class can’t be used because it is a keyword
For can be used
do can be used
4thCol can’t be used because it starts with a number
totally can be used
Row31 can be used
__Amount can be used

Q.6. How are floating constants represented in Python? Give example to support your answer.
Ans:

Represent real numbers and are written with a decimal point dividing the integer and fractional parts. floating constant can be written in fractional as well as in exponent form.

Q.7. How are string-literals represented and implement in Python?
Ans:

Single line string Text1= “Hello World”
Mutiline strings with backslash Text2=
“Hello\
World”
Triple quote Text3 =
”’Hello
World”’

Q.8.What are operators ? what is their function? Give examples of some unary and binary operators.
Ans:

Operators are token that trigger some computational / action when applied to variables and other objects in an expression.
The operators can be arithmetic operators(+,-,*,/,//), bitwise operators, shift operators, identity operators, relational operators, assignment operator. Uniary operator that applies on one operator like -5. Binary operator that works on two operators like a+b,
c-d.

Q.9. What is an expression and a statement?
Ans:

An expression is any valid combination of operators, literals and variables. e.g. x>y, x+y
A statement is an instruction that the Python interpreter can execute, e.g. x = 5

Q.10. What all components can a python program contains ?
Ans:

Expressions , functions , comments , statement , Block.

Q.11. What are variable? How are they important for a program?
Ans:

Variable represent labelled storage locations, whose values can be manipulated during program run.
As they are manipulated from input to output and do much things in the program during runtime , That’s why they are important.

Q.12. Describe the concepts of block or suite and body. What is indentation and how it is related to block and body ?
Ans:

There are various statements in same indentation level which is called block or suite. Body is for all of the program but due to use for loop , if else case different indentation from next line is required.Body whole program and block same indentation code.

Q.13. What are data types? How are they important.
Ans:

Data types are means to identify type of data and set of valid operations for it. They are important as they are manipulated in program to give useful data.

Q.14. How many integer types are supported by python? Name them.
Ans:
There are two types of integers in Python:
i. Integers. It is the normal integer representation of whole numbers.
ii.Booleans. These represent the truth values False and True.

Q.15.What are immutable and mutable types? List immutable and mutable types of Python.
Ans:

The immutable types are those that can never change their value in place. Example-Integers, Floating point numbers, Booleans, strings, tuples.
Mutability means that in the same memory address, new value can be stored as and when we want. Example- Lists, dictionaries and sets.

Q.16.What is the difference between implicit type conversion and explicit type conversion?
Ans
:
Implicit type – Where no one need to especially link a data type to a variable . Example –
a=6 #int
b=1.0 #float
c=a+b #float
Explicit type- where the system forces to be of a data type . Example-
a=6.3
int(a)
Now a has become integer type.

Q.17.An immutable data type is one that cannot change after being created. Give three reasons to use immutable data.
Ans:

Three reasons to use immutable data types are:
· Immutable objects improve correctness and clarity. It provides a logical way to reason about the code. As the value of the object won’t change throughout the execution of the program, we can freely pass around objects without the fear of modification.
· They are better to be used when it is certain that no changes will be made to the variable afterwards.
· They are more faster than their mutable counterparts, e.g. tuples are faster than lists.

18. What is entry controlled loop? Which loop is entry controlled loop in Python?
Ans:

In entry controlled loop first the statement is checked if the statement is positive then the loop starts. While loop is entry controlled loop in Python.

19. Explain the use of the pass statement. Illustrate it with an example.
Ans:

Pass statement is an empty statement which passes the statement to the next statement. Example-
def sub() #you will be writing the code afterwards but for the interpretation to happen you need to write pass in next line
pass.

20. Below are seven segments of code, each with a part coloured. Indicate the data type of each coloured part by choosing the correct type of data from the following type.
Ans:

i) boolean
ii)string
iii)List of string
iv)integer
v)Boolean
vi)List of string
vii)String

Application Based Questions : Python Revision Tour Class 12 Solved Assignment

Q.1: Fill in the missing lines of code in the following code. The code reads in a limit amount and a list of 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.

# Read the limit
limit = float(input(“Enter the limit”))
max_price = 0
# Read the next price
next_price = float(input(“Enter a price or 0 to stop:”))
while next_price > 0:
<write your code here>
#read the next price
<write your code here>
if max_price>0:
<write your code here>
else :
<write your code here>

Answer:
limit = float(input(“Enter the limit”))
max_price = 0
# Read the next price
next_price = float(input(“Enter a price or 0 to stop:”))
while next_price > 0:

if next price < limit and max_price > next price :
max_price = next price
#read the next price

if max_price>0:

print( max_price )
else :
Pass

Q: 2.Predict the outputs of the following programs :
Q:2.a)
count=0
while count<10:
print(“hello”)
count +=1
Answer:

hello
hello
hello
hello
hello
hello
hello
hello
hello
hello

Q:2.b)
x=10
y=0
while x>y:
print(x,y)
x=x-1
y=y+1
Answer:

10 0
9 1
8 2
7 3
6 4

Q:2.c)
Keepgoing= True
x=100
while keepgoing:
print(x)

x=x-10
if x<50:
keepgoing=false
Answer:

100
90
80
70
60
50

Q:2.d)
x=45
while x<50:
print(x)
Answer:
45
45
45…..print like this infinite times

Q:2.e)
for x in [1,2,3,4,5]:
print(x)
Answer
:
1
2
3
4
5

Q:2.f)
for p in range (1,10):
print(p)
Answer:

1
2
3
4
5
6
7
8
9

Q:2.g)
for z in range (-500,500,100):
print(z)
Answer:

-500
-400
-300
-200
-100
0
100
200
300
400

Q:2.h)
x=10
y=5
for x in range (x-y*2):
print(“%”,i)

Answer:
No output

Q:2. i)
c=0
for x in range (10):
for y in range(5):
c+=1
print(c)

Answer:
50

Q:2. j)
x=[1,2,3]
counter=0
while counter < len(x):
print(x[counter]*”%”)
for y in x:
print(y*’*’)
counter +=1
Answer:

%
*
**
***
%%
*
**
***
%%%
*
**
***

Q:2. k)
for x in ‘lamp’:
print(str.upper(x))
Answer:

L
A
M
P

Q:2. l)
x=’one’
y=’two’
counter=0
while counter < len(x):
print(x[counter],y[counter])
counter +=1
Answer:

o t
n w
e o

Q:2. m)
x=”apple,pear,peach”
y=x.split(“,”)
for zin y:
print(z)
Answer:

apple
pear
peach

Q: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))
Answer:

apple
PEAR
PEACH
grapefruit

Q: 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!’)
print(‘Got it ! ‘)
Answer:

jayes
Finished!
ramya
Finished!
Taruna
Got it !

Q: 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)
Answer:

No output

Q: 4 (ii)
for i in range(1,3,1):
for j in range(i+1):
print(‘*’)
Answer:

*
*
*
*
*

Q: 5. Is the loop in the code below infinite? How do you know before you run it?
m=3
n=5
while n<10:
m=n-1
n=2*n-m
print(n,m)
Answer:

The code is finite.
we can find whether the code is infinite by checking the while condition . Which changes or remain the same and always all time according to that condition is checked it comes out true.

Programming practice knowledge based questions :
Python Revision Tour Class 12 Solved Assignment

Q: 1. Write a program to print one of the words negative, zero, or positive, according to whether variable x is less than zero, zero or greater than zero, respectively.

Answer:
a=int(input("Enter the number"))
if a>0 :
    print("greater than zero")
if a==0:
    print("zero")
if a<0:
    print("less than zero")

Q: 2. Write a program that returns True if the input number is an even number, False otherwise.

Answer:
a=int(input("Enter the number"))
if a%2==0:
    print("number is even")
else:
    print("false")

Q: 3. Write a Python program that calculates and prints the number of seconds in a year.

Ans:
a=606024*365
print(a)

Q: 4. Write a Python program that accepts two integers from the user and prints a message saying if first number is divisible by second number or if it is not.

Answer:
a=int(input(“enter the first number”))
b=int(input(“enter the second number”))
if (a%b)==0:
print(“First number is divisible by second number”)
else:
print(“it is not divisible”)

Q: 5. Write a program that asks the user the day number in a year in the range 2 to 365 and asks the first day of the year- sunday or Monday or Tuesday etc. Then the program should display the day on the day number that has been input.

Answer:
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

firstday = input("Enter the first day of the year: ")

index = days.index(firstday)

day = int(input("Enter a number between 2-365: "))

print(days[index+(day%7)])

Q: 6. One foot equals 12 inches. Write a function that accepts a length written in feet as an argument and returns this length written in inches. Write a second function that asks the user for a number of feet and returns this value. Write a third function that accepts a number of inches and displays this to the screen. Use these three functions to write a program that asks the user for a number of feet and tells them the corresponding number of inches.

Answer:
ft=float(input(“Enter the ft”))
inch=ft*12
print(inch, “this is the number of inches”)

Q: 7. Write a program that reads an integer N from the keyboard computes and displays the sum of the numbers from N to (2*N) if N is nonnegative. If N is a negative number, then it’s the sum of the numbers from (2*N) to N. The starting and ending points are included in the sum.
Answer:

n = int(input(“Enter a number: “))
m = 2 * n
if n > 0:
sum = 0
for i in range(n, m+1):
sum += i
print(sum)
elif n < 0:
sum = 0
for i in range(m, n+1):
sum += i
print(sum)
else:
print(“Enter a valid no. “)

Q: 8. Write a program that reads a date as an integer in the format MMDDYYYY. The program will call a function that prints print out the date in the format <Month Name><day>, <year>.
Answer
:

dt = input(“Enter date in the format MMDDYYYY: “)
if dt[0] == “0”:
if dt[1] == “1”:
a = “January”
elif dt[1] == “2”:
a = “February”
elif dt[1] == “3”:
a = “March”
elif dt[1] == “4”:
a = “April”
elif dt[1] == “5”:
a = “May”
elif dt[1] == “6”:
a = “June”
elif dt[1] == “7”:
a = “July”
elif dt[1] == “8”:
a = “August”
elif dt[1] == “9”:
a = “September”
elif dt[1] == “10”:
a = “October”
elif dt[0] == “1”:
if dt[1] == “2”:
a = “November”
elif dt[1] == “12”:
a = “December”
print(a,” “, dt[2],dt[3],”,”, ” “, dt[4:], sep = “”)

Q.9.Write a program that prints a table on two columns – table that helps converting miles into kilometers.

Ans:
mile = []

kilometer = []

choice = 1

while choice != 0:

    a = int(input("Enter the distance in miles: "))

    mile.append(a)

    kilometer.append(a*1.6)

    print(a, "miles is", a*1.6, "kilometers")

    choice = int(input("Enter 1 to continue or 0 to quit: "))
    print(" Miles|Kilometers ")

for i in range(len(mile)):

    print(" ",mile[i],"|",kilometer[i]," ")

Q.10. Write another program printing a table with two columns that helps convert pounds in kilograms.

Ans:
pound = []

kilogram = []

choice = 1

while choice != 0:

    a = int(input("Enter mass in pounds: "))

    pound.append(a)

    kilogram.append(a*0.45)

    print(a, "pounds is", a*0.45, "kilograms")
    choice = int(input("Enter 1 to continue or 0 to quit: "))

print(" Pounds|Kilograms")

for i in range(len(pound)):

    print(" ",pound[i],"|",kilogram[i]," ")

Q.11.Write a program that reads two times in military format (0900, 1730) and prints the number of hours and minutes between the two times.

Answer:
a = int(input("Please enter the 1st time: "))

b = int(input("Please enter the 2nd time: "))

c = b - a

if c > 1000:

    c = str(c)

    print(c[0],c[1], " ", "hours", " ", c[3],c[4]," ", "minutes", sep = "")

else:

    c = str(c)

    print(c[0], " ", "hours", " ", c[1],c[2]," ", "minutes", sep = "")

We are also providing the solutions of all the chapters of Computer Science (Python) in this site. the links of relevant topics are provided below:

Computer Science with Python Class 12 Sumita Arora Solutions – CS Study

Leave a Comment

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