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.
-
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.
-
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.
-
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.
-
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.
-
What is the difference between range and Xrange?
Range returns the list, while Xrange returns the xrange object.
-
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
-
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.
-
How to generate random numbers in Python
To generate a random number in Python, we need to import command
import random
random.random()
-
What is slicing in Python?
Slicing is a mechanism to select a range of items from sequence types like strings , list, tuple etc.
-
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.
-
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.
-
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#.
-
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.
-
How to display the current time in python?
currenttime= time.localtime(time.time())
print (“Current time is”, currenttime)
-
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
-
How to define a string array in python?
colors=["red","green","blue","black","white"];
-
What are the different types of loops in python?
-
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
-
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)
-
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
-
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()
config.read('mypy.ini')
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