Python Interview Questions and Answers Set 2

11. What is the output of the following program?

#!/usr/bin/python

deffoo(x=[]):

x.append(1)

return x

foo()

foo()

Output:    [1]    [1,1

12. What is the purpose of #!/usr/bin/pythonon the first line in the above code? Is there any advantage?

By specifying #!/usr/bin/pythonyou specify exactly which interpreter will be used to run the script on a particular system.This is the hardcoded path to the python interpreter for that particular system. The advantage of this line is that you can use a specific python version to run your code.

13. What is the output of the following program?

list =[‘a’,’b’,’c’,’d’,’e’]

printlist[10]

Output:

    IndexError. Or Error.

14. What is the output of the following program?

list =[‘a’,’b’,’c’,’d’,’e’]

print(list[10:])

Output:

[]

The above code will output [],andwill notresult inan Index Error.

As one would expect, attempting to access a member of a list using an index that exceeds the number of members results in an Index Error.

15. What does this list comprehension do:

[x**2 for x in range(10 ) if x%2==0]

Creates the following list:

[0,4,16,36,64]

16. Do sets, dictionaries and tuples also support comprehensions? Ans.Sets and dictionaries support it. However tuples are immutable and have generators but not comprehensions.

Set Comprehension:

r={x for x inrange(2,101)

if not any(x %y ==0 for y in range(2,x))}

Dictionary Comprehension:

{i:j for i,j in{1:’a’,2:’b’}.items()}

since

{1:’a’,2:’b’}.items() returns a list of 2-Tuple.i is the first element of tuple j isthe second.

17. Mutable Types Immutable Types

Dictionary number

List boolean

string

tuple

What are some mutable and immutable data­types / data­structures in python?

18. What are generators in Python?

A generator is simply a function which returns an object on which you can call next, such that for every call it returns some value, until it raises a Stop. Iteration exception, signaling that all values have been generated. Such an object is called an iterator.

Normal functions return a single value using return, just like in Java. In Python, however, there is an alternative, called yield. Using yield anywhere in a function makes it a generator.

19. What can you use Python generator functions for?

One of the reasons to use generator is to make the solution clearer for some kind of solutions.

The other is to treat results one at a time, avoiding building huge lists of results that you would process separated anyway.

20. When is not a good time to use python generators?

Use list instead of generator when:

• You need to access the data multiple times (i.e. cache the results instead of recomputing them)

• You need random access (or any access other than forward sequential order):

• You need to join strings (which requires two passes over the data)

• You are using PyPy which sometimes can’t optimize generator code as much as it can with normal function calls and list manipulations