Question 1
Question 1
From the following, find out which assignment statement will produce an error. State
reason(s) too.
(a) x = 55
(b) y = 037
(c) z = 0o98
(d) 56thnumber = 3300
(e) length = 450.17
(f) !Taylor = 'Instant'
(g) this variable = 87.E02
(h) float = .17E - 03
(i) FLOAT = 0.17E - 03
Answer
1. y = 037 (option b) will give an error as decimal integer literal cannot start with a 0.
2. z = 0o98 (option c) will give an error as 0o98 is an octal integer literal due to the 0o
prefix and 8 & 9 are invalid digits in an octal number.
3. 56thnumber = 3300 (option d) will give an error as 56thnumber is an invalid
identifier because it starts with a digit.
4. !Taylor = 'Instant' (option f) will give an error as !Taylor is an invalid identifier
because it contains the special character !.
5. this variable = 87.E02 (option g) will give an error due to the space present between
this and variable. Identifiers cannot contain any space.
6. float = .17E - 03 (option h) will give an error due to the spaces present in exponent
part (E - 03). A very important point to note here is that float is NOT a
KEYWORD in Python. The statement float = .17E-03 will execute successfully
without any errors in Python.
7. FLOAT = 0.17E - 03 (option i) will give an error due to the spaces present in
exponent part (E - 03).
Question 2
(i) 20 + 30 * 40
Answer
20 + 30 * 40
⇒ 20 + 1200
⇒ 1220
(ii) 20 - 30 + 40
Answer
20 - 30 + 40
⇒ -10 + 40
⇒ 30
Answer
(20 + 30) * 40
⇒ 50 * 40
⇒ 2000
Answer
15.0 / 4 + (8 + 3.0)
⇒ 15.0 / 4 + 11.0
⇒ 3.75 + 11.0
⇒ 14.75
Question 3
(i)
temperature = 90
print temperature
Answer
The call to print function is missing parenthesis. The correct way to call print function is this:
print(temperature)
(ii)
a = 30
b=a+b
print (a And b)
Answer
(iii)
a, b, c = 2, 8, 9
print (a, b, c)
c, b, a = a, b, c
print (a ; b ; c)
Answer
X = 24
4 = X
Answer
print("X ="X)
Answer
1. Variable X is undefined
2. "X =" and X should be separated by a comma like this print("X =", X)
(vi)
else = 21 - 5
Answer
Question 4
(i)
X = 10
X = X + 10
X = X - 5
print (X)
X, Y = X - 2, 22
print (X, Y)
Output
15
13 22
Explanation
(ii)
first = 2
second = 3
third = first * second
print (first, second, third)
first = first + second + third
third = second * first
print (first, second, third)
Output
2 3 6
11 3 33
Explanation
(iii)
side7
7 49
Explanation
1. side = int(input('side') ) ⇒ This statements asks the user to enter the side.
We enter 7 as the value of side.
2. area = side * side ⇒ area = 7 * 7 = 49.
3. print (side, area) ⇒ prints the value of side and area as 7 and 49 respectively.
Question 5
(i)
a = 3
print(a)
b = 4
print(b)
s = a + b
print(s)
Answer
name = "Prejith"
age = 26
print ("Your name & age are ", name + age)
Answer
In the print statement we are trying to add name which is a string to age which is an integer.
This is an invalid operation in Python.
(iii)
a = 3
s = a + 10
a = "New"
q = a / 10
Answer
The statement a = "New" converts a to string type from numeric type due to dynamic
typing. The statement q = a / 10 is trying to divide a string with a number which is an
invalid operation in Python.
Question 6
(a)
x = 40
y = x + 1
x = 20, y + x
print (x, y)
Output
(20, 81) 41
Explanation
(b)
x, y = 20, 60
y, x, y = x, y - 10, x + 10
print (x, y)
Output
50 30
Explanation
(c)
a, b = 12, 13
c, b = a*2, a/2
print (a, b, c)
Output
12 6.0 24
Explanation
a, b = 12, 13
print (print(a + b))
Output
25
None
Explanation
Question 7
a, b, c = 10, 20, 30
p, q, r = c - 5, a + 3, b - 4
print ('a, b, c :', a, b, c, end = '')
print ('p, q, r :', p, q, r)
Output
a, b, c : 10 20 30p, q, r : 25 13 16
Explanation
Question 8
(a)
y = x + 5
print (x, Y)
Answer
There are two errors in this code fragment:
(b)
print (x = y = 5)
Answer
Python doesn't allow assignment of variables while they are getting printed.
(c)
a = input("value")
b = a/2
print (a, b)
Answer
The input( ) function always returns a value of String type so variable a is a string. This
statement b = a/2 is trying to divide a string with an integer which is invalid operation in
Python.
Question 9
Find the errors in following code fragment : (The input entered is XI)
Question 10
Hi <name>,
How are you doing?
What could be the problem ? Can you suggest the solution for the same ?
Answer
The print() function appends a newline character at the end of the line unless we give our
own end argument. Due to this behaviour of print() function, the statement print ('Hi',
name, ',1) is printing a newline at the end. Hence "How are you doing?" is getting printed
on the next line.
To fix this we can add the end argument to the first print() function like this:
print ('Hi', name, ',1, end = '')
Question 11
1. c - 1 is outside the parenthesis of print function. It should be specified as one of the
arguments of print function.
2. c is a string as input function returns a string. With c - 1, we are trying to subtract a
integer from a string which is an invalid operation in Python.
Question 12
Answer
<class 'int'>
Answer
<class 'int'>
(c) >>>.type(int('0')
Answer
SyntaxError: invalid syntax
Answer
<class 'str'>
Answer
<class 'float'>
Answer
<class 'int'>
(g) >>>type(float(0))
Answer
<class 'float'>
Answer
<class 'float'>
Answer
<class 'float'>
Question 13
Output
'NoneOne'
Explanation
print() function doesn't return any value so its return value is None.
Hence, str(print()) becomes str(None). str(None) converts None into string 'None'
and addition operator joins 'None' and 'One' to give the final output as 'NoneOne'.
(b) >>> str(print("hello"))+"One"
Output
hello 'NoneOne'
Explanation
First, print("hello") function is executed which prints the first line of the output as hello. The
return value of print() function is None i.e. nothing. str() function converts it into string and
addition operator joins 'None' and 'One' to give the second line of the output as 'NoneOne'.
Output
Hola None
Explanation
First, print("Hola") function is executed which prints the first line of the output as Hola. The
return value of print() function is None i.e. nothing. This is passed as argument to the outer
print function which converts it into string and prints the second line of output as None.
Output
HolaNone
Explanation
First, print ("Hola", end = "") function is executed which prints Hola. As end argument is
specified as "" so newline is not printed after Hola. The next output starts from the same line.
The return value of print() function is None i.e. nothing. This is passed as argument to the
outer print function which converts it into string and prints None in the same line after Hola.
Question 14
Carefully look at the following code and its execution on Python shell. Why is the last
assignment giving error ?
>>> a = 0o12
>>> print(a)
10
>>> b = 0o13
>>> c = 0o78
File "<python-input-41-27fbe2fd265f>", line 1
c = 0o78
^
SyntaxError : invalid syntax
Answer
Due to the prefix 0o, the number is treated as an octal number by Python but digit 8 is invalid
in Octal number system hence we are getting this error.
Question 15
a, b, c = 2, 3, 4
a, b, c = a*a, a*b, a*c
print(a, b, c)
Output
4 6 8
Explanation
Question 16
The id( ) can be used to get the memory address of a variable. Consider the adjacent code and
tell if the id( ) functions will return the same value or not(as the value to be printed via print()
) ? Why ?
[There are four print() function statements that are printing id of variable num in the code
shown on the right.
num = 13
print( id(num) )
num = num + 3
print( id(num) )
num = num - 3
print( id(num) )
num = "Hello"
print( id(num) )
Answer
num = 13
print( id(num) ) # print 1
num = num + 3
print( id(num) ) # print 2
num = num - 3
print( id(num) ) # print 3
num = "Hello"
print( id(num) ) # print 4
For the print statements commented as print 1 and print 3 above, the id() function will return
the same value. For print 2 and print 4, the value returned by id() function will be different.
The reason is that for both print 1 and print 3 statements the value of num is the same which
is 13. So id(num) gives the address of the memory location which contains 13 in the front-
loaded dataspace.
Question 17
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(float(input("valuel:")) )
value1:67
67.0
Answer
In part a, the value entered by the user is converted to a float type and passed to the print
function by assigning it to a variable named num. It means that we are passing an argument
named num to the print function. But print function doesn't accept any argument named num.
Hence, we get this error telling us that num is an invalid argument for print function.
In part b, we are converting the value entered by the user to a float type and directly passing it
to the print function. Hence, it works correctly and the value gets printed.
Question 18
Output
Input days : 1
Input hours: 2
Input minutes: 3
Input seconds: 4
Total number of seconds 93784
Question 19
n1, n2 = 5, 7
n3 = n1 + n2
n4 = n4 + 2
print(n1, n2, n3, n4)
Answer
The code will result into an error as in the statement n4 = n4 + 2, variable n4 is undefined.
Question 20