p

python tutorial - Python interview questions and answers - learn python - python programming



python interview questions :111

What is PEP8?

  • PEP8 consists of coding guidelines for Python language so that programmers can write readable code making it easy to use for any other person, later on.

python interview questions :112

Is all the memory freed when Python exits?

  • No it is not, because the objects that are referenced from global namespaces of Python modules are not always de-allocated when Python exits.

python interview questions :113

What does _init_.py do?

  • _init_.py :Is an empty py file used for importing a module in a directory. _init_.py provides an easy way to organize the files.
    • If there is a module maindir/subdir/module.py,_init_.py is placed in all the directories so that the module can be imported using the following command-
import  maindir.subdir.module
click below button to copy the code. By Python tutorial team

python interview questions :114

What is the different between range () and xrange () functions in Python?

  • range () returns a list whereas xrange () returns an object that acts like an iterator for generating numbers on demand.

python interview questions :115

How can you randomize the items of a list in place in Python?

  • Shuffle (lst) can be used for randomizing the items of a list in Python
python randomize

Learn python - python tutorial - python randomize - python examples - python programs

python interview questions :116

What is a pass in Python?

  • Pass in Python signifies a no operation statement indicating that nothing is to be done.
python pass statement

Learn python - python tutorial - python pass statement - python examples - python programs

python interview questions :117

If you are gives the first and last names of employees, which data type in Python will you use to store them?

  • You can use a list that has first name and last name included in an element or use Dictionary.
  • There are various data types in Python. Some of the important types are listed below.
    • Python Numbers
    • Python List
    • Python Tuple
    • Python Strings
    • Python Set
    • Python Dictionary
python-dictionarys

Learn python - python tutorial - python-dictionarys - python examples - python programs

python interview questions :118

What happens when you execute the statement mango=banana in Python?

  • A name error will occur when this statement is executed in Python.
python errors executed

Learn python - python tutorial - python errors executed - python examples - python programs

python interview questions :119

Optimization for Python Code?

  • Use builtin functions and libraries: Builtin functions like map() are implemented in C code. So the interpreter doesn’t have to execute the loop, this gives a considerable speedup.
  • The map() function applies a function to every member of iterable and returns the result. If there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables.
python-dictionarys

Learn python - python tutorial - python-dictionarys - python examples - python programs

# Python program to illustrate library functions
# save time while coding with the example of map()
import time
 
# slower (Without map())
start = time.clock() 
s = 'geeks'
U = []
for c in s:
    U.append(c.upper())
print U
elapsed = time.clock()
e1 = elapsed - start
print "Time spent in function is: ", e1
 
# Faster (Uses builtin function map())
s = 'geeks'
start = time.clock() 
U = map(str.upper, s) 
print U
elapsed = time.clock()
e2 = elapsed - start
print "Time spent in builtin function is: ", e2
click below button to copy the code. By Python tutorial team

Output

['G', 'E', 'E', 'K', 'S']
Time spent in function is:  0.0394747945637
['G', 'E', 'E', 'K', 'S']
Time spent in builtin function is:  0.0212335531192

python interview questions :120

What is monkey patching in Python?

  • Monkey patching is a technique that helps the programmer to modify or extend other code at runtime.
  • Monkey patching comes handy in testing but it is not a good practice to use it in production environment as debugging the code could become difficult.

Related Searches to Python interview questions and answers