Python Interview Questions

Python Interview Questions

  • What is Python? What are the benefits of using Python?

Python is a programming language with objects, modules, threads, exceptions and automatic memory management. The benefits of pythons are that it is simple and easy, portable, extensible, build-in data structure and it is an open source.

Python Interview Questions

  • What is PEP 8?

PEP 8 is a coding convention, a set of recommendation, about how to write your Python code more readable.

  • What is pickling and unpickling?

Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.

  • How Python is interpreted?

Python language is an interpreted language. Python program runs directly from the source code. It converts the source code that is written by the programmer into an intermediate language, which is again translated into machine language that has to be executed.

Python Interview Questions

  • How memory is managed in Python?

Python memory is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have an access to this private heap and interpreter takes care of this Python private heap.
The allocation of Python heap space for Python objects is done by Python memory manager. The core API gives access to some tools for the programmer to code.
Python also have an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.

  • What are the tools that help to find bugs or perform static analysis?

PyChecker is a static analysis tool that detects the bugs in Python source code and warns about the style and complexity of the bug. Pylint is another tool that verifies whether the module meets the coding standard.

Python Interview Questions

  • What are Python decorators?

A Python decorator is a specific change that we make in Python syntax to alter functions easily.

  • What is the difference between list and tuple?

The difference between list and tuple is that list is mutable while tuple is not. Tuple can be hashed for e.g as a key for dictionaries.

  • How are arguments passed by value or by reference?

Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result you cannot change the value of the references. However, you can change the objects if it is mutable.

Python Interview Questions

  • What is Dict and List comprehensions are?

They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable.

 

  • How is Multithreading achieved in Python?

Python has a multi-threading package but if you want to multi-thread to speed your code up.
Python has a construct called the Global Interpreter Lock (GIL). The GIL makes sure that only one of your ‘threads’ can execute at any one time. A thread acquires the GIL, does a little work, then passes the GIL onto the next thread.
This happens very quickly so to the human eye it may seem like your threads are executing in parallel, but they are really just taking turns using the same CPU core.
All this GIL passing adds overhead to execution. This means that if you want to make your code run faster then using the threading package often isn’t a good idea.

  • What is the difference between list and tuples?

Lists are mutable i.e they can be edited. Syntax: list_1 = [10, ‘Chelsea’, 20]
Tuples are immutable (tuples are lists which can’t be edited). Syntax: tup_1 = (10, ‘Chelsea’ , 20)

Python Interview Questions

  • How can the ternary operators be used in python?

The Ternary operator is the operator that is used to show the conditional statements. This consists of the true or false values with a statement that has to be evaluated for it.
Syntax:
The Ternary operator will be given as:
[on_true] if [expression] else [on_false]x, y = 25, 50big = x if x < y else y Example: The expression gets evaluated like if x

  • What is the usage of help() and dir() function in Python?
  • Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions.

    Help() function: The help() function is used to display the documentation string and also facilitates you to see the help related to modules, keywords, attributes, etc.
    Dir() function: The dir() function is used to display the defined symbols.

    Python Interview Questions

    • Whenever Python exits, why isn’t all the memory de-allocated?

    Whenever Python exits, especially those Python modules which are having circular references to other objects or the objects that are referenced from the global namespaces are not always de-allocated or freed.
    It is impossible to de-allocate those portions of memory that are reserved by the C library.
    On exit, because of having its own efficient clean up mechanism, Python would try to de-allocate/destroy every other object.

    • Mention the differences between Django, Pyramid and Flask.

    Flask is a “microframework” primarily build for a small application with simpler requirements. In flask, you have to use external libraries. Flask is ready to use.
    Pyramid is built for larger applications. It provides flexibility and lets the developer use the right tools for their project. The developer can choose the database, URL structure, templating style and more. Pyramid is heavy configurable.
    Django can also used for larger applications just like Pyramid. It includes an ORM.
    Python Interview Questions

    • Give an example how you can write a VIEW in Django?

    This is how we can use write a view in Django:
    1 from django.http import HttpResponse
    2 import datetime
    3
    4 def Current_datetime(request):
    5 now = datetime.datetime.now()
    6 html = “It is now %s” % now
    7 return HttpResponse(html)

    • List out the inheritance styles in Django.

    In Django, there is three possible inheritance styles:

    Abstract Base Classes: This style is used when you only wants parent’s class to hold information that you don’t want to type out for each child model.
    Multi-table Inheritance: This style is used If you are sub-classing an existing model and need each model to have its own database table.
    Proxy models: You can use this model, If you only want to modify the Python level behavior of the model, without changing the model’s fields.
    Python Interview Questions

    • What is map function in Python?

    map function executes the function given as the first argument on all the elements of the iterable given as the second argument. If the function given takes in more than 1 arguments, then many iterables are given. #Follow the link to know more similar functions.
    Python Interview Questions

    • Explain the use of decorators.

    Decorators in Python are used to modify or inject code in functions or classes. Using decorators, you can wrap a class or function method call so that a piece of code can be executed before or after the execution of the original code. Decorators can be used to check for permissions, modify or track the arguments passed to a method, logging the calls to a specific method, etc.

    • What is the difference between NumPy and SciPy?

    In an ideal world, NumPy would contain nothing but the array data type and the most basic operations: indexing, sorting, reshaping, basic elementwise functions, et cetera.
    All numerical code would reside in SciPy. However, one of NumPy’s important goals is compatibility, so NumPy tries to retain all features supported by either of its predecessors.
    Thus NumPy contains some linear algebra functions, even though these more properly belong in SciPy. In any case, SciPy contains more fully-featured versions of the linear algebra modules, as well as many other numerical algorithms.
    If you are doing scientific computing with python, you should probably install both NumPy and SciPy. Most new features belong in SciPy rather than NumPy.
    Python Interview Questions

    • How do you make 3D plots/visualizations using NumPy/SciPy?

    Like 2D plotting, 3D graphics is beyond the scope of NumPy and SciPy, but just as in the 2D case, packages exist that integrate with NumPy. Matplotlib provides basic 3D plotting in the mplot3d subpackage, whereas Mayavi provides a wide range of high-quality 3D visualization features, utilizing the powerful VTK engine.

    • What is the difference between range & xrange?

    For the most part, xrange and range are the exact same in terms of functionality. They both provide a way to generate a list of integers for you to use, however you please. The only difference is that range returns a Python list object and x range returns an xrange object.
    This means that xrange doesn’t actually generate a static list at run-time like range does. It creates the values as you need them with a special technique called yielding. This technique is used with a type of object known as generators. That means that if you have a really gigantic range you’d like to generate a list for, say one billion, xrange is the function to use.
    This is especially true if you have a really memory sensitive system such as a cell phone that you are working with, as range will use as much memory as it can to create your array of integers, which can result in a Memory Error and crash your program. It’s a memory hungry beast.

    • Write a sorting algorithm for a numerical dataset in Python.

    The following code can be used to sort a list in Python:
    1 list = [“1”, “4”, “0”, “6”, “9”]
    2 list = [int(i) for i in list]
    3 list.sort()
    4 print (list)
    Python Interview Questions

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

    from random import shuffle
    x = [‘Keep’, ‘The’, ‘Blue’, ‘Flag’, ‘Flying’, ‘High’]
    shuffle(x)
    print(x)
    The output of the following code is as below.
    [‘Flying’, ‘Keep’, ‘Blue’, ‘High’, ‘The’, ‘Flag’]

    • Explain what Flask is and its benefits?

    Flask is a web micro framework for Python based on “Werkzeug, Jinja2 and good intentions” BSD license. Werkzeug and Jinja2 are two of its dependencies. This means it will have little to no dependencies on external libraries. It makes the framework light while there is little dependency to update and less security bugs.
    A session basically allows you to remember information from one request to another. In a flask, a session uses a signed cookie so the user can look at the session contents and modify. The user can modify the session if only it has the secret key Flask.secret_key.

    KyrosAcademy

    This information box about the author only appears if the author has biographical information. Otherwise there is not author box shown. Follow YOOtheme on Twitter or read the blog.
    +91 9952948899 info@kyrosacademy.com

    Offcanvas