Python Interview Questions

In this python Interview questions article, I am listing down a couple of questions related to the latest language Python, which is high in demand among several top IT companies. So, if you are preparing for an interview, just go through it. All the Best!

Read Latest Python Interview Questions and answers - 2018

  • Q1.

    Explain the difference between a list and the tuple?

    Posted by Sharad Jaiswal
    There are many differences that makes list different from tuple. They are-
    • List is mutable, which means that it can’t be utilized as a key in a dictionary. Whereas, tuples are immutable due to which you can’t change the value in a tuple once you have made it.
    • The accurate syntax of tuples is displayed by parentheses {} while the accurate syntax of lists is displayed by square brackets [].
    • Tuples are varied data structures (i.e., their entries contain diverse implications), whereas lists are uniform series.
    • Tuples are for fixed length, lists are for uneven length.
    • Tuples illustrate structure while lists illustrate order.
    • Tuples contain O(N) insert, append, and delete performance while Lists contain O(1) insert, append, and delete performance.
  • Q2.

    How does Python handle the memory management?

    Posted by Sharad Jaiswal
    For memory management, Python includes a private heap consist all Python objects as well as data structures. Interpreter handles Python heap and that the programmer contains no access to it.

    Python memory manager allocates heap space to Python objects. The core API of Python gives a few tools for the programmer to code unfailing and highly robust program.

    Additionally, Python contains a built-in garbage collector that recycles the memory which is unused. Once an object is no more referenced by the program, it frees up the heap space it occupied. The garbage collector determines objects that are no more referenced by the program liberated the in used memory as well as make it available to the heap space.

  • Q3.

    How Python performs Compile-time and Run-time code checking?

    Posted by Sharad Jaiswal
    Some portion of the Python coding is executed at compile time, but almost all the checking like name, type and so on, are deferred until code execution. So, if the Python code references a user-defined function that hardly exists, the code will run effectively. The Python code will be unsuccessful just with an exception while the code execution path is not present.
  • Q4.

    Explain In detail – Inheritance in Python?

    Posted by Sharad Jaiswal

    Python also supports Inheritance as well as Multiple Inheritance. Any class can inherit behaviour as well as attributes methods from different class, known as superclass. A class that inherits from a superclass is known as subclass. Another name for superclasses is ancestors too. There presents a hierarchy association between classes.

    Syntax of Inheritance in Python:

    class BaseClass:
     Body of base class
    class DerivedClass(BaseClass):
      Body of derived class

    Various types of Inheritance

    • Single Inheritance In this type of inheritance, derived class obtains the members of a particular super class.
    • Multi-Level Inheritance – In this type of inheritance, a derived class d1 is inheriting the properties of base class base1, and another derived class is inheriting the properties of base2.
    • Hierarchical Inheritance – In this type of inheritance, from single base class you can inherit many child classes
    • Multiple Inheritance – In this type of inheritance, a derived class is inheriting the properties of many base classes.
  • Q5.

    What is the use of try, except, finally and raise in Error Handling.

    Posted by Sharad Jaiswal
    Python makes use of try, except and finally blocks for error handling. Try block is used to execute the code until an error occurs. We can make use of an except block to receive control which will receive control after all errors, or one can use specific exception handling blocks for various error types. Control is transferred to the appropriate except block. In all cases, the finally block is executed. Raise may be used to raise your own exceptions.
    In case of error handling, Python uses try, except, finally and raise blocks.
    • Use of try block helps in executing the code until an error arises.
    • Use of except block helps in getting the control which further get control after each and every errors, or simply you can utilize particular exception handling blocks for different error types. Control is moreover moved to the suitable except block.
    • The finally block is executed in each case.
    • You can raise our own exception as well with the help of raise block.
  • Q6.

    How are instance variables different from class variables?

    Posted by Sharad Jaiswal
    Instance variables are usually made locally within a class to refer to an object of the class. A class variable is made globally within a class as well as can be accessed in every instance of the class.

    Class variables are stated with keyword static as well as Instance variables are stated with static keyword.

    Class variables can be obtained anywhere in the class while an instance variable can be obtained in a specific object of the class.

    Class variables can be obtained by making use of class name of object reference. Instance variables can be obtained only with the help of object reference.

  • Q7.

    Point out some of the key features of Python?

    Posted by Sharad Jaiswal
    Here are some of the key features:
    • Python is basically an interpreted language, which means that unlike other programming languages and its variants, Python doesn’t need compiling before being run. Other interpreted languages include Ruby and PHP.
    • Another feature that makes Python special is that its dynamically typed. This simply means that one doesn’t need to state the types of variables when you declare them or anything like that.
    • It is very well suited to object orientated programming. It allows the definition of classes along with inheritance and composition. Python does not have access specifiers like public, private or C++, the justification for this point is given as “we are all adults here”
    • In this programming language the functions are all first-class objects. As the language can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects
  • Q8.

    What is the parameter passing mechanism in Python?

    Posted by Sharad Jaiswal
    Python consists of two parameter passing mechanism named as-
    • Pass by references: Every parameter (argument) is passed “by reference” to the functions by default. However, in case you modify the value of the parameter in the function, the impact can also be reflected in the called function.
    • Pass by value: The pass by value is that at whatever time you pass the parameters to functions which are of the type such as strings, numbers, tuples. This is due to the immutable nature of them.
  • Q9.

    What do you understand by the term Deep copy?

    Posted by Sharad Jaiswal
    To store the values, which has already copied, deep copy is used. The reference pointers to the object are not copied by the deep copy. It simply helps in making reference to the object and the new object that is pointed by some other object gets stored. The changes are made in the original copy that will not affect any other copy while using the object. The Deep copy makes execution of the program slower due to making certain copies for each object that is been called.
  • Q10.

     How is memory managed in Python?

    Posted by Sharad Jaiswal
    Following ways are used to manage memory 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 has an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.
  • Q11.

    How is Multithreading achieved in Python?

    Posted by Sharad Jaiswal
     Python programming has a multi-threading package that can be used if one wants to multi-thread to speed their code up.

    A construct called the Global Interpreter Lock (GIL) is available in Python. It is the work of the GIL to make sure that only one of the ‘threads’ can execute at any one point of time. Normally a thread accepts the GIL, does a little work, then passes it on the GIL to the next thread.  All this happens very quickly that will seem so to the human eye and 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.

  • Q12.

    What are the different uses of help() and dir() functions in Python?

    Posted by Sharad Jaiswal
    dir() and help(), both the functions are accessible from the Python interpreter and used are generally used for viewing a consolidated dump of built-in functions. 
    • Dir() function: The dir() function is used to display the defined symbols.
    • Help() function: This function is mainly used to display the string of documentation and also the facilitates you to see the help related to modules, keywords, attributes, etc.
  • Q13.

     What is the dictionary in Python?

    Posted by Sharad Jaiswal
    In Python, the built-in data-types is called dictionary. It is known to define the one-to-one relationship between the values and the keys. Dictionaries contain a pair of keys and their corresponding values. Dictionaries are generally indexed by the keys.
  • Q14.

     What does this mean: *args, **kwargs? And why would we use it?

    Posted by Sharad Jaiswal
    Basically *args is applied when you are not sure about the number of arguments that are going to be passed to a function, or if there arises a need to pass stored list or tuple of arguments to a function.

    Whereas, **kwargs is used when you are unsure about the number of keyword arguments that will be passed to a function, or it can be also used to pass the values of a dictionary as keyword arguments. The identifier args and kwargs are a convention, you could also use *bob and **billy but that would not be wise.

  • Q15.

    Explain split(), sub(), subn() methods of “re” module in Python.

    Posted by Sharad Jaiswal
    To modify the strings, Python’s “re” module is providing 3 methods. They are:
    • sub() – finds all substrings where the regex pattern matches and then replace them with a different string
    • split() – uses a regex pattern to “split” a given string into a list.
    • subn() – being similar to sub() it also returns the new string along with the number of replacements.
  • Q16.

    Shade some light on the modes in Python Programming Environment.

    Posted by Sharad Jaiswal
    Basically there are 2 types of modes in python programming that are:
    • Interactive Mode: This mode can be eventually taken up as a scratchpad that can be used to check out the codes in the Python Environment.
    • Script Mode: The script mode is basically used to save or compile the programs of Python that is rarely possible in the Interactive mode. To make such programs executable, the Script Mode is preferred.
  • Q17.

    Specify the differences between Pyramid, Django, and Flask.

    Posted by Sharad Jaiswal
     Here are some of the considerable differences:

    There are basically two frameworks available in the Python programming that are mainly

    • Major Frameworks: pyramid and Django
    • Minor Frameworks: flask and Bottle

    Firstly, Flask is a “microframework” that is built with simpler applications that are on primary basis. Flask uses external libraries and is always ready to use.  Secondly, Pyramid is generally built for larger applications. As it provides a lot of flexibility and allows the developer to use the right kind of tools for the project, which makes it developer friendly.

    The developer can easily choose the database, template style, URL structure, and much more. Pyramid is heavily configurable. Lastly, Django can be also used for larger applications just like Pyramid. It basically includes an ORM.

  • Q18.

     How is Python different from Java?

    Posted by Sharad Jaiswal

    Java and Python are way different from each other, but both of them can be useful tools for high-tech developers. Also, Python is quite easies to master than Java if you are new to learning how to write programs. Below mentioned are the few points which clearly shows that python is different from Java. Here they are-

    • Dynamic vs. Static Typing:
      The very first difference between both of the powerful languages is the way they handle variables individually. Java uses static typing in which it wants the user to define the variable type when the user initially declare it and further, will not enable you to change the type later on in the program. Whereas, Python utilized dynamic typing, in which user is free to change the variable type.
    • Braces vs. Indentation:
      Python make use of indentation so as to separate into blocks. Whereas, Java utilizes curly braces to clearly define the starting and end of every function as well as class definition.
    • Speed
      The plus point of the Python programming language is that its programs tend to run quite faster than the Java programs.
  • Q19.

    How does exception handling in Python differ from Java?

    Posted by Sharad Jaiswal
    Python uses its own techniques to implement exception handling. <try-except> is the block that can be utilized by the programmer to view any type of error details without having to end the program. Additionally, in a few cases, this <try-except> statement provides a solution to handle the error.
  • Q20.

    What is a module and package in Python?

    Posted by Sharad Jaiswal
    Modules can be defined as the Python files with an extension “.py”. The module name will be same as that of the file name. A Python module can own a collection of classes, functions or variables defined as well as applied.

    On the other side, Packages are namespaces which has various packages and modules themselves. They are merely directories.

    Every package in Python programming language is a directory which should have a special file known as This file can be void and it shows that the directory it consists is a Python package. This can be imported similar to the module which can be imported.

  • Q21.

    How to Install Python and Prepare Environment ?

    Posted by Pradeep Kumar

    To install python on different machine follow the steps below:

    • Linux( Linux based os already shipped with python installed. If you need any specific version of the python language you can download and install it from the official website Python)
    • Windows: You can go to official website of Python and download the python installer for windows as per your windows architecture (64/86) and execute the installer after installing it in your machine.
    • Mac:
  • Q22.

    What are the key features of using python?

    Posted by Pradeep Kumar
    • Python is the most flexible language due to its dynamic nature.

    • Functions in Python are first-class objects. Python follows OOPS paradigm that makes it more real-time coding experience.

    • Python code is very clean by using indentation syntax. So its code is more clear than any other language.

    • More natural language: you can express your ideas in more natural language compared to other languages.

    • It has a huge community and growing faster so that it can be best promising language in the world.

    • It has large number of packages based on almost every area of programming like machine learning, AI,

    • Python has lots of interesting features you can check out on the official website.

  • Q23.

    How does ternary operator work in python?

    Posted by Pradeep Kumar

    Python has interesting philosophy using ternary operator. It has no ternary operation syntax. Python uses if else statement to perform the action.

    print("Equal") if 5==5 else print("Not equal")
    5!=5 and 'Equal' or 'Not equal'
  • Q24.

    What is the use of ‘self’ keyword in python?

    Posted by Pradeep Kumar

    In python ‘self’ references the instance of the class.

  • Q25.

    What are generators in python?

    Posted by Pradeep Kumar

    Generators are used in python to return an iterable object. It is used to modify the behavior of the loop statement. Loops can be replaced with generators to perform on large dataset efficiently.

  • Q26.

    What is lambda in python?

    Posted by Pradeep Kumar

    Lambda is single line code block which can have multiple arguments. It behaves like a function without function name.

    Cube = lambda x : x * x * x
  • Q27.

    What is a negative indexing in python?

    Posted by Pradeep Kumar

    Python sequences are managed by integer values (-ve 0 -ve). They can be accessed both wise. Positive indices start with 0 and continue to 1, 2, 3, …. N and Negative indices start with -1 and point to the last index.

    Name = “python”
    Name[-1] /////// n
    Name[:-1]  ////// python
  • Q28.

    What is deep copy in python?

    Posted by Pradeep Kumar

    Generally, we use copy function of the copy module to make a duplicate data. But this copy function only makes the copy of reference that means if we change the new object then this will change the original as well.

    So, we use deep copy of the copy module so that we can create a new object with original one and can modify the newly created object separately.

  • Q29.

    What do you understand by mutable and immutable in python?

    Posted by Pradeep Kumar

    Python sequences are mutable if we can change the data after initialization whereas sequences that can not be changed once created then they immutable.

    In Python, tuples are immutable whereas lists and dictionaries are mutable.

  • Q30.

    What is monkey patching in python?

    Posted by Pradeep Kumar

    Monkey patching is a concept by which we can change the function behavior in runtime. Due to the dynamic nature of the python, we can replace the body of a class function to new one. It is very useful in testing the python application. For example, if we have any class method which returns some api data and during testing, we don’t want the api data and use some local data then we can change the function and assign another function to it.

  • Q31.

    What do you understand by *args and **kwarg python?

    Posted by Pradeep Kumar

    These are just arguments variable passed to function in the definition part. When we don’t know the number of arguments passed to a function then we use *args. Same is applied to *kwargs but it accepts keyword arguments(Python dictionary).

    The name can be anything like a variable name. We just use the above name (args,  kwargs) because it is very understandable to programmers.

Latest Posts.

Python Tutorials
Python popularity: Why and how PYTHON is growing Like Elephant

Python has been around for a couple of decades and the codes written in this entire time has been re...

Python Tutorials
Facts You Must Know About DJANGO Python

Facts You Must Know About DJANGO Python: DJANGO- THE FRAMEWORK BY PYTHON Django is a free and open s...

Python Tutorials
Python vs Swift: Which one is better?

You have to be 'Swift' to catch up with a 'Python' in the real world, isn't it? How else would you m...