Python 2
Python 2
Question 1
1. IF
2. Test if
3. if-else
4. if-elif
5. if
6. if-elif-else
Answer
IF and Test if
Question 2
1. for
2. while
3. iter
4. repeat
Answer
iter, repeat
Reason — iter and repeat are not valid Python loop. for and while are valid
loop statements.
Question 3
1. { }
2. ( )
3. indentation
4. Quotation
Answer
indentation
Question 4
if None :
print "Up"
else:
print "Down"
1. Up
2. Down
3. No output
4. Up Down
Answer
Down
Reason — Since None is treated as false, the if condition results in false. The
block of if will be ignored and the else block will be executed and Down will
be printed on the output screen.
Question 5
for i in range(1) :
print i
1. 0
2. 1
3. No output
4. Error in code
Answer
Reason — The range(n) function generates a list as [0, 1, 2..., n - 1]. Thus,
the range(1) function will generate a list as [0]. Thus the for loop will execute
only one time and print 0 on the output terminal.
Question 6
for i in range(1, 0) :
print i
1. 0
2. 1
3. No output
4. Error in code
Answer
No output
Question 7
while 3 >= 3 :
print 3
1. 3 is printed once
Answer
Reason — Since the given condition (3 >= 3) will always remain true, the
while loop will execute indefinitely and keep on printing 3 on the output
screen till the program is closed.
Theoretical Questions
Question 1
Answer
num = 5
if num >= 0 :
print "Positive"
else :
print "Negative"
Question 2
Answer
num = 3
while num > 0 :
print num
num = num - 1
Question 3
Answer
S.
No if if-else
.
Question 4
Answer
S.
No for while
.
for loop is used when the number of while loop is used when the number of i
1.
iterations are known beforehand. are not fixed and depends on a specific
for loop is used for iterating over a while loop is used for repeated executio
2.
sequence or a range. on a condition.
3. Syntax: Syntax:
S.
No for while
.
Question 5(a)
print num % 10
num = num/10
Answer
l = [1]
for x in l:
l.append(x + 1)
if num <= 0:
break
num = num/10
Question 5(b)
i = 100
while (i > 0) :
print i
i -= 3
Answer
print (i)
Question 6
if n < 1 :
else :
print i * i
Answer
The code will print the square of each number from 1 till the number given as
input by the user if the input value is greater than 0. Output of the code for
input as 3 is shown below:
Enter an integer:3
Question 7(a)
min = 0
max = num
if num < 0 :
min = num
max = 0
sum += i
Answer
min = 0
max = num
if num < 0 :
min = num
max = 0
i = min
sum += i
i += 1
Question 7(b)
if i % 3 == 0 :
print i
Answer
i=1
if i % 3 == 0 :
print (i)
i += 1
Question 8(i)
1 4 7 10 ........... 40.
Answer
Solution
Output
1 4 7 10 13 16 19 22 25 28 31 34 37 40
Question 8(ii)
Answer
Solution
x=1
x *= -1
Output
Question 9(a)
count = 0
print "Hello"
count += 1
Answer
Output
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Explanation
Question 9(b)
x = 10
y=0
while x > y:
print x, y
x=x—1
y=y+1
Answer
Output
10 0
91
82
73
64
Explanation
x y Output Remarks
10 0 10 0 1st Iteration
9 1 10 0 2nd Iteration
x y Output Remarks
91
10 0
8 2 91 3rd Iteration
82
10 0
91
7 3 4th Iteration
82
73
10 0
91
6 4 82 5th Iteration
73
64
Question 10
What will be output of the following code if the user enters Principal amount
as 20000 and Time as 10 years.
T = input("Enter Time:")
if T > 10:
SI = P*T*10/100
else:
SI = P*T*15/100
Answer
Output
Explanation
This Python program calculates the Simple Interest (SI) based on the given
Principal amount (P) and Time (T) values. For time value of more than 10
years, the program takes the rate of interest as 10% and for 10 years or less
it takes the rate of interest as 15%.
Question 11
Write a Python script that asks the user to enter a length in centimetres. If
the user enters a negative length, the program should tell the user that the
entry is invalid. Otherwise, the program should convert the length to inches
and print out the result. There are 2.54 centimetres in an inch.
Answer
Solution
if len < 0:
print("Invalid input")
else:
Output
Question 12
Write a program that asks the user for two numbers and prints Close if the
numbers are within .001 of each other and Not close otherwise.
Answer
Output
Close
Question 13
Answer
Solution
n = int(input("Enter n: "))
x=n*2-1
print(i)
Output
Enter n: 5
Question 1
while
n=3
print n == 3
Answer
(a) The given code will generate a syntax error.
(b) The condition of while and colon after that is missing. Thus a syntax error
is generated.
Question 2
n=3
while n == 3
print n
n=n-1
(c) What change/corrections will you make to above code to make it print
some output ?
Answer
(b) The syntax of while statement has a colon after the condition which is
missing in the given code. Thus, a syntax error is generated.
(c) The following changes must be made to the code to print some output:
n=3
while n == 3 :
print (n)
n=n-1