Python Q&A
Python Q&A - Part4
144) d1={1:’a’,2:’b’,3:’c’}
print(d1.keys())
Answer: dict_keys([1, 2, 3])
print(d1.values())
Answer: dict_values([‘a’, ‘b’, ‘c’])
print(d1.items())
Answer: dict_items([(1, ‘a’), (2, ‘b’), (3, ‘c’)])
print(d1[2])
Answer: b
d2=d1.fromkeys(d1.keys())
print(d2.keys())
Answer: dict_keys([1, 2, 3])
print(d2.values())
Answer: dict_values([None, None, None])
d1.update({4:’d’})
print(d1.items())
Answer: dict_items([(1, ‘a’), (2, ‘b’), (3, ‘c’), (4, ‘d’)])
145) What is the output?
lst1=[1,2]
lst2=[3,4]
print(lst1)
Answer: [1,2]
lst1.extend(lst2)
print(lst1)
Answer: [1, 2, 3, 4]
146) What does this print?
import os
print(os.environ[‘PYTHONPATH’])
Answer: Prints the path of the location of module files
147) What does ord() return?
Answer: Returns the number representing the Unicode of specific character
148) print(ord(‘a’))
Answer: 97
149) print(chr(97))
Answer: a
150) print(abs(-2))
Answer: 2
151) print(format(1000000,’,’))
Answer: 1,000,000
152) print(“Pythor Test”.replace(‘r’,’n’))
Answer: Python Test
153) What is “try” block
Answer: The code to test for exception is placed in “try” block
154) How can you explicitly raise an error?
Answer: Using raise statement
155) Where can you place the code that should definitely execute whether the program errors out or not?
Answer: Place the code in finally block
156) print(“Python Test”.replace(‘ ‘,”))
Answer: PythonTest
157) Write the code using for loop to print the prime numbers between 2 and 100
Answer:
for x in range(2, 100):
for y in range(2, x):
if x % y == 0:
break
else:
print(x)
158) print(‘ Python’)
Answer: Python
159) print(‘ Python’.lstrip())
Answer: Python
160) print(‘Python ‘+’Test’)
Answer: Python Test
161) print(‘Python ‘.rstrip()+’Test’)
Answer: PythonTest
162) What does the following code do?
from random import shuffle
lst=[1,2,3]
print(lst)
shuffle(lst)
print(lst)
Answer: The code will randomly shuffle list items
163) What method is used to separate string?
Answer: split()
164) What is an iterator?
Answer: An object that contains countable number of values
165) x=iter([1,2])
print(next(x))
Answer: 1
print(next(x))
Answer: 2
166) Write code to print the following
*
**
***
Answer:
for x in range(1,4):
for y in range(1,x+1):
print(‘*’,end=”)
print()
167) What braces are used for dictionaries?
Answer: Curly braces { }
168) What are Python data types?
Answer: Python data types are Integer, Float, Complex number, Boolean, Strings, List, Tuple, Set and Dictionary
169) What are Python operators?
Answer: The Python operators are Arithmetic operators, Assignment operators, Bitwise operators, Comparison operators, Logical operators, Identity operators and Membership operators
170) How to get list of Python keywords?
Answer: help(‘keywords’)
171) How to get list of Python symbols?
Answer: help(‘symbols’)
172) How to get list of Python available topics?
Answer: help(‘topics’)
173) What does the following code print?
Import time
print(time.time())
Answer: Gives seconds elapsed since 1st Jan 1970
174) Print formatted local time
Answer: print(time.asctime(time.localtime(time.time())))
175) Print the calendar for June 2021
Answer:
import calendar
print(calendar.month(2021,6))
176) How to find if 2021 is leap year?
Answer:
Import calendar
print(calendar.isleap(2021))
177) What is the output of the following?
import copy
x=[[1,2,3],[4,5,6]]
y=copy.copy(x)
print(‘x is:’+str(x))
Answer: x is:[[1, 2, 3], [4, 5, 6]]
print(‘y is:’+str(y))
Answer: y is:[[1, 2, 3], [4, 5, 6]]
x[1][1]=8
print(‘x changed to:’+str(x))
Answer: x changed to:[[1, 2, 3], [4, 8, 6]]
print(‘y also changed to:’+str(y))
Answer: y also changed to:[[1, 2, 3], [4, 8, 6]]
178) What is the output of the following?
import copy
x=[[1,2,3],[4,5,6]]
y=copy.deepcopy(x)
print(‘x is:’+str(x))
Answer: x is:[[1, 2, 3], [4, 5, 6]]
print(‘y is:’+str(y))
Answer: y is:[[1, 2, 3], [4, 5, 6]]
x[1][1]=8
print(‘x changed to:’+str(x))
Answer: x changed to:[[1, 2, 3], [4, 8, 6]]
print(‘y remains same:’+str(y))
Answer: y remains same:[[1, 2, 3], [4, 5, 6]]