0% found this document useful (0 votes)
59 views11 pages

Chap 1 Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views11 pages

Chap 1 Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter – 1 Python Revision Tour

Type - A

Q1. 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 :&,^,?,#,@,!
Q2. How are keywords different from identifiers?
Answer =Keywords have special meaning in python while identifier are define by user.
Q3. What are literals in Python? How many types of literals are allowed in Python?
Answer =Literals are data item or value in python.
There are five type of literal :–
1. string literal
2. numeric literal
3. Boolean literal
4. special literal
5. literal collection
Q4. Can non-graphic characters be used and processed in Python? How? Give examples to support your answer.
Answer =
Yes, non graphic character can be use in python . In python we can use by type in side of double quote or single quote .
Example –
print (“\\”)
print (“\n”)
Q5. Out of the following, find those identifiers, which cannot be used for naming Variables or Functions in a
Python program:
price*Qty, class, For, do,
4thCol, totally, Row31, _Amount
Answer = price*Qty , class , 4thCol .
Q6. How are floating constants represented in Python? Give examples to support your answer.
Answer =
Floating constant represented by positive or negative sign preceding. But real constant with no sign is assumed to be
positive.
Example = 2.0,365.23,0.002,3.
Q7. How are string-literals represented and implemented in Python?
Answer = String literal represented by enclosed by single or double quotes that from string literal in python .
Example:
a = “string ”
print (a)
>>> ‘string’

Print (“python”)
>>> ‘python’
Q8. What are operators? What is their function? Give examples of some unary and binary operators.
Answer = The operation between two operands is called operator .
Function of operator:- To operate two operand by instruction of user .
Example of unary operator:
+ unary plus , - unary minus etc.
Example of binary operator:
+ addition, - subtraction etc.
Q9. What is an expression and a statement?
Answer = Expression is a legal combination of symbol that represent a value. Statement is a instruction that does
something.
Q10. What all components can a Python program contain?
Answer = Component of program are as follow :
1. function
2. expression
3. statement
4. comments
5. blocks
6. indentation
Q11. 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.
Q12. Describe the concepts of block and body. What is indentation and how is it related to block and body?
Answer =
A block or suite is a group of statements which are part of another statement or function. Indentation refers to the
placement of text further to the right or the left, to separate it from surrounding text. All statements inside a block or suite
are intended at the same level.
Q13. What are data types? How are they important?
Answer :-
Representation of many types of data by provided facilities is called data type.
Example = integer, string, list etc.
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.
Q14. How many integer types are supported by Python? Name them.
Answer =Two type of integer are supported by python
(I)Integer (signed)
(II)Booleans
Q15. What are immutable and mutable types? List immutable and mutable types of Python.
Answer =
Immutable type :-
Those types whose value never change is known as immutable type.
Example = integer, string, Boolean, tuples, e.c.t
Mutable type: -
Those type whose value can be change is known as mutable type.
Example = list, dictionary, set

Q16. What is the difference between implicit type conversion and explicit type conversion?
Answer = The basic difference between implicit and explicit type conversion is that implicit is taken care by compiler
itself, while explicit is done by the programmer.
Q17. An immutable data type is one that cannot change after being created. Give three reasons to use immutable
data.
Answer =
1. To Increase efficiency of the Program :- for example Tuple load faster than list.
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.
Q18. What is entry controlled loop? Which loop is entry controlled loop in Python?
Answer =
Entry controlled loop, the loop which has condition checked at the entrance of the loop , the loop executes only and only
if the condition is satisfied is called as entry control loop.
Example = while loop, for loop.
OR
Loop where test condition is checked before entering the loop body, know as entry controlled loop.
Q19. Explain the use of the pass statement. Illustrate it with an example.
Answer =A pass statement is useful in those place where the syntax of language requires the presence of a statement but
where the logic of the program does not.
Example = in looping
Example =
for I in range (1,11):
if I % 2 == 0 :
pass
print (I)
Q20. Below are seven segments of code, each with a part colored . Indicate the data type of each colored part by
choosing the correct type of data from the following type.
(a) int (b) float (c) bool (d) str (e) function (f) list of int (g) list of str
(i)
if temp < 32 :
print("Freezirg")
Answer = function
(ii)
L = [ 'Hiya', 'Zoya', Preet' ]
print(L[1])
Answer = str
(iii)
M=[]
for i in range (3) :
M. append (i)
print(M)
Answer = list of int
(iv)
L = [ 'Hiya', 'Zoya', 'Preet' ]
n = len (L)
if ‘Donald’ in L[1: n] :
print (L) (i2)

Answer = int

v)
if n% 2 == 0 :
print("Freezing")

Answer = bool

(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

Q1. 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.

# Read the limit.


limit = float(input ('enter the limit'))
max_price = 0
#Read the next price
next_price = float(input ("Enter a price or 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 =
# Read the limit.
limit = float(input ('enter the limit'))
max_price = 0
#Read the next price
next_price = float(input ("Enter a price or to stop:"))
while next_price > 0 :
if next_price > max_price and next_price < limit :
max_price = next_price
# Read the next price
next_price = float(input ("Enter a price or to stop:"))
if max_price > 0 :
print("Largest number :-",max_price)
else :
print("Invalid")
Q2. Predict the outputs of the following programs: -

(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)
ot
nw
eo
>>>
(m)
apple
pear
peach
>>>
(n)
apple
PEAR
PEACH
grapefruit
>>>
Q3. 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!
>>>
Q4. 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.
Q5. Is the loop in the code below infinite? How do you know (for sure) 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
Q1. 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 :-

x = int (input ("Enter the number : "))


if x > 0:
print ("positive number ")
elif x == 0 :
print ("zero number ")
else :
print ("negative number ")
Output :-
Enter the number : 53
positive number
>>>
Enter the number : -9
negative number
>>>
Enter the number : 0
zero number
>>>
Q2. Write a program that return True if the input number is an even number, False otherwise.
Answer :-
x = int (input ("Enter the number "))
if x%2 == 0:
print ("True")
else :
print ("False")
Output :-
Enter the number 2
True
>>>

Enter the number 564


True
>>>
Enter the number 524163
False
>>>
Q3. Write a python program that calculates and prints the number of seconds in a year.
Answer :- print ( "for non leap year = " ,60*60*24*365, " , for leap year = ", 60*60*24*366 )
Output :- for non leap year = 31536000 , for leap year = 31622400
>>>
Q4. 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 :-
Enter the first number 4
Enter the second number 2
first number is divisible by second number
>>>
Enter the first number 455
Enter the second number 2
first number is not divisible by second number
>>>
Enter the first number 5563
Enter the second number 563
first number is divisible by second number
>>>
Q5. 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 :-
Enter a number between 2 to 365 = 56
Enter first day of year (Sunday, Monday, Tuesday,Wednesday,Thursday,Friday,Saturday)Friday
Thursday
>>>
Enter a number between 2 to 365 = 100
Enter first day of year (Sunday, Monday, Tuesday,Wednesday,Thursday,Friday,Saturday)Monday
Tuesday
>>>
Q6. One foot equals 12 inches. Write a function that accept a length written in feet as an argument and returns
this length written in inches. write a second function that ask the user for a number of feet and return this value.
write a third function that accept a number of inches and display 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 :-
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 :-
Enter number in feet :-100
1200 Inches
>>>
Enter number in feet :-568
6816 Inches
>>>
Enter number in feet :-9
108 Inches
>>>
Q7. Write a program that reads an integer N from the keyboards computes and displays the sum of 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 :-
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 :-
Enter the number :- 10
165
>>>
Enter the number :- -10
-165
>>>
Enter the number :- 95
13680
>>>
Enter the number :- -754
-853905
>>>
Q8. Write a program that reads a date as an integers in the format MMDDYYYY. The program will call a
function that prints print out the date in the format <month name > <day>,<year>.
Answer :-
mon = ["January", "February","March" , "April", "May" , "June" , "July" , "August ", "September",
"October", "November" , 'December']
a = int (input("Enter the date = " ))
month = a // 1000000
date = (a % 1000000) // 10000
year = a % 10000
print ( mon [ month - 1 ] , date ,", ", year )
Output :-
Enter the date = 12252019
December 25 , 2019
>>>
Enter the date = 09162021
September 16 , 2021
>>>
Enter the date = 11302020
November 30 , 2020
>>>
Q9. Write a program that prints a table on two columns – table that helps converting miles into kilometers.
Answer :-
print("Miles\t | \tKilometer")
for j in range (7) :
print(10**j , end= "\t" )
print(" | " , end= "\t")
print( 1.6093 * 10** j )
Output :-
Miles | Kilometer
1 | 1.6093
10 | 16.093
100 | 160.93
1000 | 1609.3
10000 | 16093.0
100000 | 160930.0
1000000 | 1609300.0
>>>
Q10. Write another program printing a table with two columns that helps convert pounds in kilograms.
Answer :-
print("Pounds\t | \tKilogram")
for j in range (0,101,5) :
print( j , end= "\t" )
print(" | " , end= "\t")
print( 0.45 * j )
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
>>>
Q11. Write a program that reads two times in military format and prints the number of hours and minutes
between the two times .
Answer :-
first = int(input("Please Enter the first time = "))
sec = int(input ("Please Enter the second time = " ))
a = sec - first
print (a // 100, "hours " , a % 100, "min")
Output :-
Please Enter the first time = 0900
Please Enter the second time = 1730
8 hours 30 min
>>>
Please Enter the first time = 1115
Please Enter the second time = 2355
12 hours 40 min
>>>
Please Enter the first time = 1214
Please Enter the second time = 2230
10 hours 16 min
>>>

You might also like