Python Questions and Answers for Experienced

 Q-1. Can You Describe What’s Wrong With The Below Code?test Proc([1, 2, 3]) # Explicitly passing in a list
test Proc()  # Using a default empty list def test Proc(n = []):
    # Do something with n print n                                                                   Ans. The above code would throw a <NameError>.The variable n is local to the function <testProc> and can’t be accessed outside.So, printing it won’t be possible.Q-2. 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)Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.8.2] on linux ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] Q-3. What Is The Result Of The Below Python Code? keyword = 'aeioubcdfg' print keyword [:3] + keyword [3:] Ans. The above code will produce the following result. <'aeioubcdfg'> In Python, while performing string slicing, whenever the indices of both the slices collide, a <+> operator get applied to concatenates them. Q-4. What Are The Methods You Know To Copy An Object In Python? Ans. Commonly, we use <copy.copy()> or <copy.deepcopy()> to perform copy operation on objects. Though not all objects support these methods but most do. But some objects are easier to copy. Like the dictionary objects provide a <copy()> method. Example. item = {n: n*2 for n in range(10)} newdict = item.copy() print newdict Q-5. 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. Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.8.2] on linux => ['I', ' ', 'a', 'm', ' ', 'l', 'e', 'a', 'r', 'n', 'i', 'n', 'g', ' ', 'P', 'y', 't', 'h', 'o', 'n', '.'] Q-6. What Is The Function To Randomize The Items Of A List In-Place? Ans. Python has a built-in module called as <random>. It exports a public method <shuffle(<list>)> which can randomize any input sequence. import random list = [2, 18, 8, 4] print "Prior Shuffling - 0", list random.shuffle(list) print "After Shuffling - 1", list random.shuffle(list) print "After Shuffling - 2", list Q-7. How Does Exception Handling In Python Differ From Java? Also, List The Optional Clauses For A <Try-Except> Block In Python? Ans. Unlike Java, Python implements exception handling in a bit different way. It provides an option of using a <try-except> block where the programmer can see the error details without terminating the program. Sometimes, along with the problem, this <try-except> statement offers a solution to deal with the error. There are following clauses available in Python language. 1. try-except-finally 2. try-except-else Q-8. What Do You Know About The <List> And <Dict> Comprehensions? Explain With An Example. Ans. The <List/Dict> comprehensions provide an easier way to create the corresponding object using the existing iterable. As per official Python documents, the list comprehensions are usually faster than the standard loops. But it’s something that may change between releases. The <List/Dict> Comprehensions Examples. #Simple Iteration item = [] for n in range(10):     item.append(n*2) print item 

Leave a Reply