Python Fundamrntal Book Questions
Python Fundamrntal Book Questions
Program to obtain length and breadth of a rectangle and calculate its area.
1.2
rogram
# to input length and breadth of a rectangle and calculate its area
length float( input("Enter length of the rectangle: "))
breadth float ( input ("Enter breadth of the rectangle: "))
area = length * breadth
is
The output produced by above program as
shown below:
Rectangle specifications
Length 8.75 Breadth = 35.0
Area = 306.25
4.3 and GST and then print Invoice along with both CGST and SGST
Program to get selling price rate
values.
lrogram
INFORMAT CS PRACTI
92
sgst: state govt gst *
central govt gst
s SP: selling price cgst:
item n a m e : ")
item input("Enter item "+ item +": ") )
("Enter selling price of
SP float (input
"Enter GST rate (%): "))
gstRate float (input (
cgst SP* ( (gstRate/2)/ 100)
sgst cgst
will buy at this price
amount = SP + Cgst + sgst # consumer
INVOICE
LET US REVISE
APythonprogram can contain various components like expressions, statements, comments, ndéemt
An
functions,
expression is a legal combination of symbols that represents a value.
A statement is a programing instruction.
A variable in
Python is defined only when some value is
assigned to it.
Python supports dynamic typing i.e., a variable can hold values
The input() is used to obtain of difjerent types at different e
input from user; it always returnsa string type of
Output is generated through print( ) (by calling print value.
function) statement.
Chapter 4: PYIHON FUNDAMENTALS 93
Solved Problems
. What are literals in Python ? How many types of literals are allowed in Python ?
Solution. Literals mean constants i.e, the data items that never change their value during a program
run. Python allows five types of literals:
) String literals
(i) Numeric literals
(ii) Boolean literals
(iv) Special Literal None
(o) Literal Collections like tuples, lists etc.
Solution.
Size is 1 as there is 1 character (escape sequence) and it is a string literal enclosed in
single quotes.
Size is 1 as there is 1 character enclosed in double quotes.
"\a"
94 INFORMAT PRACi
is string having 7characters enclosed
"Reema's" Size is 7 because it a
in double quoke
for apostrophe and is considered a sinol
escape sequence
Size is 1. It is a character constant and is containing just one character
character.)
Size is 4. Python allows single quote without escape sequence in a d
"it's"
single string e.g, 'nuoeto
and a double without escape sequence in a quoted quoe
quote
vz"" Size is 5. Triple quoted multi-line string, EOL ( ) character is also counteds
ted in
5. How many types of strings are supported in Python ?
Solution. Python allows two string types
) Single-line Strings Strings that are terminated in single line
(i) Multi-line Strings Strings storing multiple lines of text.
6. How can you create multi-line strings in Python?
Solution. Multi-line strings can be created in two ways:
(a) By adding a backslash at the end of normal single-quote or double-quote strings eg,
Text "Welcome \
To
Python"
(6) By typing the text in triple quotation marks. (No backslash needed at the end of line)=
Str1= "u u " Welcome
To
Python
0.17251 E02
(ii) 151.02 =0.15102x10 =
0.15102E03
(iii) 0.00031 =0.31 10
=0.31E-3
(iv) 0.452 =0.0452 10 x = 0.0452E01
indicate
absence of value. It is also used to something that has not yet beern crea
indicate the end of lists in
Python.
PYTHON FUNDAMENTALS 95
Chapter 4:
Solution.
23.789 Floating point
23789 integer
True Boolean
True' String
"True" String
False Boolean
"False'" String
OXFACE Integer (Hexadecimal)
00213 Integer(Octal)
00789 Invalid token ( beginning with 0 means it is octal number but digits
8 and 9 are invalid digits in ociai numbers)
None None
Solution
Expression Statement
Legal combination of symbols Programming instruction as per Python syntax
Example Examples:
2.3 print ("Hello")
(3+5)/ 4 i fa >0:
State
12. Which of the following are syntactically correct strings? reasons.
(c) "Goodbye
(d) 'This course is great !'
(e) "Hello
) " I 1iked the movie "Bruce Almighty' very much."
Solution. Strings (a), (b), (d) and () are syntactically correct. (Strings (b) and (f) are also valid as single
don't match.
String (c) ( "Goodbye' ) is incorrect because opening and closing quotes
String (e) ("Hello) is invalid because it has no closing quotes.
statement ?
13. What is the error in following Python program with one
output as 40.0
Number input( "Enter Number")
DoubleTheNumber = Number * 2
Print (DoubleTheNumber)
Solution. The problem is that input() returns value as a string, so the input value 20 is reh
string '20' and not as
integer 20. So the returned
output is not 40.
Also Print() is not legal function of
Python; it should be print().
15. What would be the correction
forproblem of previous question ?
Solution. By using int( ) with
input(), we can convert the string into integer value, i.e., as
Number int(input ("Enter Number"))
DoubleTheNumber =Number *2
print (DoubleTheNumber)
Now the program will
print desired output as 40 if 20 is
typed
16.
as
input.
Why is following code giving errors ?
name "Rehman"
print ("Greetings!!!")
print ("Hello", name)
print ("How do you do ?")
17. Write a
program to calculate profit percentage from the sales of
Solution. goods that you nmade.
cgos =
print (name, ", you are", 17, "now but", end ")
print (" you will be ", age +1, "next year")
Solution.
Simar, you are 17 now but you will be 18 next year.
X, y = y, x+2
print (x, y)
Solution.
6 4
Explanation. First line of code assigns values 2 and 6 to variables x and y respectively.
Next line (second line) of code first evaluates right hand side i.e, y, x+2 which is 6, 2+2 i.e., 6, 4
and then assigns this to x, y ie., x, y=6,4;so x gets 6 and y gets 4.
Third line prints x and y so the output is 6 4.
GLOSS ARY
Constant A dota item that never changes its value during a program run.
Assignments
Selecded
Questions/Conceptual Questions
Answer
Type A: Short
1. What are tokens in Python ? How many types of tokens are allowed in Python 2
8. Which argument of
print( ) would you set for:
() changing the default separator
(space)? (i) printing the following line in current line ?
9. What are
operators? What is their function ? Give examples of some
10. What is an
unary and binary opera
expression and a statement ?
11. What all
components can a Python program contain ?
12. What do you understand
13.
by block/code block/suite in Python ?
What is the role of indentation
Python ? in
14.
What are variables? How are they
15. What do important for a
program
you understand by undefined variable in
16. What is
Dynamic Typing feature of Python ? Python ?
17. What would
the
18. What is following code do: X =Y 7? =
the error in
19. following code X, Y 7? =
=
0.17E 03
2
-
Find out
the
error(s) in
following code
temperature =90 fragmernts
(ii) a 30
print temprature b = a+ b
(ii) a, b, c 2, print (a And b)
8,9
print (a, b, c) X = 24
C,b, a =a, b, c (io)
4 X
print (a b; c)
() (vi) else = 21 5
print ("X ="
X)
Chapter 4: PYTHON FUNDAMENTALS 99
?
3. What will be the output produced by following code fragment (s)
X = 10 (i) first 2
X= X+ 10 second =3
third = first * second
X = X - 5
#side given as 7
ii) side =
int(input('side'))
area side * side
) a 3
print (a)
b 4
print (b)
S a+b
print (s)
(ii) a 3
S a+ 10
a = "New'"
a/10
5. Predict the output
X = 40
y X+ 1
X 20, y +X
print (x, y)
6. Predict the output
X, y = 20, 60
y, X,y =
X, y - 10, x+ 10
print (x, y)
7. Predict the output
() a,b 12, 13 (b) a, b = 12, 13
a =Bo12
>print(a)
10
>»b O013
>>C =078
File "<python-input-41-27fbe2fd265f>", 1ine 1
C 0078
A
SyntaxError:invalid syntax
17. Predict the output
a, b, c = 2, 3, 4
num 13
print( id (num) )
num = num + 3
print(id(num))
num = num 3
print( id (num))
num "Hello"
print( id (num))
19. Consider below given two sets of codes, which are nearly identical, along with their execution in Python
shell. Notice that first code-fragment after taking input gives error, while second code-fragment does
not produce error. Can you tell why ?
(a)
print (num float (input("value1:")) )
=
(b)
print (float (input("value1:")) )
value1:67
Code successfully executed. No error reported.
67.0
102
INFORMATIc PRACT
20. Predict the output of the following code: