Pythonfundamental typeBQ
Pythonfundamental typeBQ
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
Question 2
(i) 20 + 30 * 40
Answer
20 + 30 * 40
⇒ 20 + 1200
⇒ 1220
(ii) 20 - 30 + 40
Answer
20 - 30 + 40
⇒ -10 + 40
⇒ 30
(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
a, b, c = 2, 8, 9
print (a, b, c)
c, b, a = a, b, c
print (a ; b ; c)
Answer
(iv)
X = 24
4 = X
Answer
(v)
print("X ="X)
Answer
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
Output
2 3 6
11 3 33
Explanation
Output
side7
7 49
Explanation
Question 5
(i)
a = 3
print(a)
b = 4
print(b)
s = a + b
print(s)
Answer
(ii)
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
Question 6
(a)
x = 40
y = x + 1
x = 20, y + x
print (x, y)
Output
(20, 81) 41
Explanation
x, y = 20, 60
y, x, y = x, y - 10, x + 10
print (x, y)
Output
50 30
Explanation
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
Output
a, b, c : 10 20 30p, q, r : 25 13 16
Explanation
Question 8
(a)
y = x + 5
print (x, Y)
Answer
print (x = y = 5)
Answer
(c)
a = input("value")
b = a/2
print (a, b)
Answer
Question 9
Find the errors in following code fragment : (The input entered is XI)
Answer
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:
Question 11
Answer
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'.
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
Hola None
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
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
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)
(b)
>>> 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
Answer