Join Class

Python Interview Questions Answers

Python is the most popular programming language among all, good python developer has huge demand in job market.

Here are some commonly python interview questions and answer for beginners and experienced developers.
Python job interview questions
  1. What are the benefits of using Python?
    Python is very powerful object oriented programming language with automatic memory, Python is open source, has built-in data structure for advanced scientific computing, well designed for developing data science driven application.
  2. What is namespace in Python?
    In Python, every name introduced has a place where it lives and can be hooked for. This is known as namespace. It is like a box where a variable name is mapped to the object placed. Whenever the variable is searched out, this box will be searched, to get corresponding object.
    import csv
    import numpy as np
    above are the examples of adding namespace in python programming.
  3. How Python is interpreted and gets executed?
    Python program converts the source code into an intermediate language, and then intermediate language gets again translated into machine language that has to be executed.
  4. How memory management in Python?

    Python memory management controlled by Python private heap space. All Python data structures and objects are located in a private heap.

    Python heap space allocation for Python objects is done by Python memory manager. Then core API gives access to some tools for the programmer to code.

    Python has built-in garbage collector, which recycle all unused memory and frees the memory and makes it available to the heap space.

    Read more about python memory management in details.

  5. What is the difference between range and Xrange?
    Range returns the list, while Xrange returns the xrange object.
        for i in range(0, 20):
            // code block
        for i in xrange(0, 20):
            // code block

    Xrange are no more supported in python version 3 or later, to check which version of python you are using, just run the command python --version.

    Python range function has multiple overloads, default range(start, stop, step).

    for i in range(0, 5, 2):
    for i in range(0, 5):
    for i in range(2):    
  6. What is package and module in Python?

    Package is basically the way we organise project, the folder of Python program is considered as package. A package can have modules or subfolders.

    Module is the programme file (.py), each Python program file (.py) is a module, and a module can import other modules like objects and attributes.

    Learn more about Modules and Packages in Python
  7. How to share a global variable across modules?
    To share a variable as global variable across modules within a single program, we need to create a separate module, then need to Import that config module in all modules wherever we want to access that variable.
  8. How to generate random numbers in Python
    To generate a random number in Python, we need to import command
    import random
  9. What is slicing in Python?
    Slicing is a mechanism to select a range of items from sequence types like strings , list, tuple etc.
  10. What is PIP software in the Python programming?
    PIP is an acronym for Python Installer Package which provides a seamless interface to install various Python modules. PIP is a command line tool which can search for packages over the internet and install them without any user interaction.
  11. Which tools can be used to unit testing of the Python code?
    The easiest way is to use "unittest" python standard library to test classes. The features supported are very similar to the other unit testing tools like TestNG, JUnit.
  12. What is the purpose of _init_() function in Python?
    It is the first function that gets executed when an object of a class is instantiated. This is equivalent to the constructor concept in C#.
  13. Name some of important modules available in Python.
    Cryptographic services, Mathematics, Networking, Internet data handling, and Multi-threading modules are prominent modules. Apart from these, there are many other modules available in the Python developer community.
  14. How to display the current time in python?

    To access date time in python library, we need to add following library reference in our code.

    from datetime import date
    from datetime import datetime
    currenttime= time.localtime(time.time())
    print (“Current time is”, currenttime)

    Learn more about python date time.

  15. Name few environment variables identified by Python.
    • PYTHONPATH: This environment variable tells the interpreter as to where to locate the module file imported in the program.
    • PYTHONSTARTUP: This environment variable contains the path of the Initialization file containing source code.
    • PYTHONCASEOK: This variable is used to find the first case-insensitive match in the import statement
  16. How to define a string array in python?

    There are various ways we can define array in python programming.


    Learn more about python array with examples.

    In python, there is one library has been developed for advanced array handling -called numpy, using numpy we can create array in various ways, single dimension array, two dimension array, multi dimension array etc. Learn more about numpy array.

  17. What are the different types of loops in python?
    In python we often use for loop, while loop, loop with range function etc. here are some examples of how to write loops in python
  18. How to write function in python?
    Python function always starts with "def" keyword. def functionName, look at the example below.
    def welcomeStudent(name):
        print("Welcome ", name)

    Learn more about python basic syntax

  19. How to achieve inheritance in Python ?

    We can implement inheritance by writing like ChildClass1(ParentClass), please take a look at the example below where “Student” class is inherited from “People” class.

    from School.People import People 
    #this is an example of inheritence
    class Student(People):
        def __init__(self):
            self.StuId = id,
            self.Title = "Student";
            print("student class");
        def welcomeMessage(obj):
           print ("Welcome  " + obj.first_name)      
  20. Can we have multiple inheritances in python?
    Yes, we can easily achieve multiple inheritance by adding comma separated multiple base classes like ChildClass1(ParentClass1, ParentClass2). If we use the reference of above example, we can add another base class like
    from School.People import People 
    from School.Course import Course
    #this is an example of multiple inheritence
    class Student(People, Course):
        def __init__(self):
            self.StuId = id,
            self.Title = "Student";
            print("student class");
        def welcomeMessage(obj):
           print ("Welcome  " + obj.first_name)      

    Learn more about OOPs in Python

  21. How we can read information from config file in Python?
    With the help of configParser we can read information from .ini configuration file.

    We need to simply import the reference of configParser , then read like value = config.get('section-name', 'key')

    Take a look at the example below how we can read database information from .ini file
    from configparser import ConfigParser
    class StudentDTO():        
        def __init__(self):
            config = ConfigParser()
            database = config.get('dbinfo', 'database')
            dbusername = config.get('dbinfo', 'dbusername')
            dbpassword = config.get('dbinfo', 'dbpassword')
            dbhost = config.get('dbinfo', 'dbhost')

    Learn more about read information from config file

Python Course Online and Latest Python Interview Questions Answers
php course online
free python tutorial online
Interview Question Answers | Fresher Job Interview Tips