15 Python Questions
15 Python Questions
import random
random.shuffle(list)
random.shuffle(list)
Ans. We can use Python <split()> function to break a string into substrings based on
the defined separator. It returns the list of all words present in the input string.
Program Output.
Q-3. What is the right way to transform a Python string into a list?
Ans. In Python, strings are just like lists. And it is easy to convert a string into the list.
Simply by passing the string as an argument to the list would result in a string-to-list
conversion.
list("I am learning Python.")
Program Output.
=> ['I', ' ', 'a', 'm', ' ', 'l', 'e', 'a', 'r', 'n', 'i',
'n', 'g', ' ', 'P', 'y', 't', 'h', 'o', 'n', '.']
Q-4. How does exception handling in Python differ from Java? Also,
list the optional clauses for a <try-except> block in Python?
1. try-except-finally
2. try-except-else
💡 Must Read – 30 Most Important Python Interview Questions and Answers .
#Simple Iteration
item = []
for n in range(10):
item.append(n*2)
print item
#List Comprehension
print item
#Dict Comprehension
print item
💡 Fact – In interactive mode, the last printed expression is assigned to the variable _
(underscore).
Q-6. What are the methods you know to copy an object in Python?
Example.
newdict = item.copy()
print newdict
Ans. No objects in Python have any associated names. So there is no way of getting
the one for an object. The assignment is only the means of binding a name to the
value. The name then can only refer to access the value. The most we can do is to
find the reference name of the object.
Example.
class Test:
self.cards = []
self.name = name
def __str__(self):
obj1 = Test('obj1')
print obj1
obj2 = Test('obj2')
print obj2
Q-8. Can you write code to check whether the given object belongs
to a class or its subclass?
Ans. Python has a built-in method to list the instances of an object that may consist of
many classes. It returns in the form of a table containing tuples instead of the
individual classes. Its syntax is as follows.
The above method checks the presence of an object in one of the classes. The built-in
types can also have many formats of the same function like <isinstance(obj, str)> or
<isinstance(obj, (int, long, float, complex))>.
Also, it’s not recommended to use the built-in classes. Create an user-defined class
instead.
We can take the following example to determine the object of a particular class.
Example.
def lookUp(obj):
if isinstance(obj, Mailbox):
else:
[6, 6, 6, 6]
The output of the above code is <[6, 6, 6, 6]>. It’s because of the late binding as the
value of the variable <index> gets looked up after a call to any of multiplexers
functions.
items.append (1)
return items
print fast ()
print fast ()
[1]
[1, 1]
The function <fast> evaluates its arguments only once after the function gets defined.
However, since <items> is a list, so it’ll get modified by appending a <1> to it.
keyword = 'aeioubcdfg'
<'aeioubcdfg'>
In Python, while performing string slicing, whenever the indices of both the slices
collide, a <+> operator get applied to concatenates them.
Q-12. How would you produce a list with unique elements from a list
with duplicate elements?
Ans. Iterating the list is not a desirable solution. The right answer should look like this.
duplicates =
['a','b','c','d','d','d','e','a','b','f','g','g','h']
uniqueItems = list(set(duplicates))
print sorted(uniqueItems)
Q-13. Can you iterate over a list of words and use a dictionary to
keep track of the frequency(count) of each word? Consider the
below example.
def dic(words):
wordList = {}
try:
wordList[index] += 1
except KeyError:
wordList[index] = 1
return wordList
wordList='1,3,2,4,5,3,2,1,4,3,2'.split(',')
print wordList
print dic(wordList)
['1', '3', '2', '4', '5', '3', '2', '1', '4', '3', '2']
Q-14. What is the result of the following Python code?
class Test(object):
def __init__(self):
self.x = 1
t = Test()
print t.x
print t.x
print t.x
print t.x
Ans. All print statement will display <1>. It’s because the value of object’s attribute(x)
is never changing.
Q-15. Can you describe what’s wrong with the below code?
# Do something with n
print n