Monday, August 5, 2019

Python Basics | Interview Questions (Updated)

Below we have gathered a list of Python Interview Questions. They are useful for any python programmer having 0 to 5 years of experience.

We keep adding new questions here regularly for you.

So bookmark this page and let's start:

Python basic interview questions - with coding examples

Python basic interview questions for beginners:


1) How can you delete a file in Python?

By using below commands:
os.unlink(Path)
os.remove(Path)

2) What is the difference between tuple and list?

Main difference between list and tuple is that the list is mutable while tuples are immutable.
Lists are slower than tuples.

3) Is Python a case-sensitive programing language?

Yes.

4) How can you convert an int into a str in python?

Get detailed answer with program code here

5) What are some of the useful Python web application frameworks?

  • django – Django is a high-level, secure and extremely flexible Python Web framework.
  • web2py – An open source, free, full-stack python web framework for database driven web applications.
  • flask – A lightweight Python web framework based on Werkzeug and Jinja 2. It is WSGI web application framework.
  • pylons – A lightweight web framework which is flexible.
  • tornado – Tornado is a scalable, non-blocking web server and web application framework.
  • turbogears – A Python-based database web app framework with Ajax integration built on top of Django and Rails.
  • grok – An open-source Web framework based on Zope Toolkit technology.
  • cherrypy – CherryPy is an  minimalist object-oriented web application framework.
  • google app engine – A platform for developing and hosting web applications. Quickly build and deploy applications using many of the popular languages like Java, PHP, Node.js, Python, C#, .Net, Ruby and Go or bring your own language runtimes and frameworks if you choose to. 

6) What is the difference between .pyc and .py files?

As we know that python is an interpreted language. When you run example.py source file the interpreter checks if a example.pyc exists. If yes and it is recent than .py file then interpreter runs it. If no then example.py file is compiled to example.pyc and then executed by appropriate virtual machine. 
.py files are Python source files. .pyc files are the compiled bytecode files that is generated by the Python interpreter.

7) What is namespace in Python?

In Python, every name used has a memory place where it resides. This space is called as namespace. Namespace can be understood as an address location where a variable name is mapped to the object placed. Whenever the variable is required, this address location will be searched, to access the corresponding object.

8) How to write multi line comments in Python?

var="Hello World"
"""this is multiline comment line no 1
this is multiline comment line no 2
this is multiline comment line no 3"""
print(var)


9) In Python what is PIP? 

PIP is required to install the desired package into our system. PIP is Package Management in Python also known as "Package Index".

10) How to find out PIP version?

 Usee below command to check the pip version,
pip --version

11) How can i free memory in Python explicitly?

Garbage collector can be used to free up any unused/unreferenced memory with the help of gc.collect().

You need to import gc package before using above method.

12) What can be the causes of memory leaks in python?

  • lingering large objects which are not released
  • underlying libraries/C extensions leaking memory
  • reference cycles within the code

13) How can you identify memory allocations in python?

To trace memory allocation in python we can use tracemalloc module. tracemalloc is a debug tool. To start tracemalloc module set PYTHONTRACEMALLOC environment variable to 1. To start tracing memory allocation in python use tracemalloc.start(). 

14) Is it possible to use Python for web client and web server side programming?

Python is best suited for server-side application development. This is because of its vast set of features for creating business logic, database handling, web server handling etc.

Also Python can be used for client-side application which needs some conversions for a browser to interpret the client side logic. Python can also be used to create standalone desktop applications.

15) Name some tools that can be used for unit testing in Python?

‘unittest' python standard library can be used for this purpose. It has similar features to the other unit testing tools such as JUnit and TestNG.

16) Describe how to use Arrays in Python?

Python does not have Arrays. In it's place there is List collection type which can store an unlimited number of elements.

17) What are the databases supported by Python?

MySQL (Structured) and MongoDB (Unstructured) are the databases that are supported in Python.

18) What is the use/purpose of _init_() function in Python?

_init_() is the first function that gets executed when an object of a class is instantiated. This is quite similar to constructors in C++.

19) What are the different environment variables in Python?

PYTHONPATH: This environment variable helps the interpreter to locate the module files imported in the program.

PYTHONCASEOK: This variable is used to find the first case-insensitive match in the import statement

PYTHONSTARTUP: This environment variable contains the path of the Initialization file
containing source code.

20) Marks is [23,41,3,75,88]. Find Marks[-1].

Answer: 88 (Marks[-1] index is last index in the list.)

21) What is split used for?

To separate a given string.

Example:

str = "Hello World"
print(str.split())


Output:
['Hello','World']
If You Enjoyed This, Take 5 Seconds To Share It