dzone python training institute
Python Interview Questions And Answers
 INTERVIEW QUESTIONS AND ANSWERS  PYTHON INTERVIEW QUESTIONS What is Python? It is a high-level object-oriented scripting language developed by Guido Rossum. It is designed for rapid prototyping of complex applications. Python is an open-source language which is widely used in Scinetific Computing, Analytics, Artificial Intelligence, Neural Networks, etc. What is the purpose of PYTHONPATH environment variable? PYTHONPATH - It is as similar as PATH that informs the python interpreter where to locate the module files imported into a program. It includes the Python Source library directory and the directories containing Python source code. PYTHONPATH is sometimes a preset by the Python Installer. What are the supported data types in Python? Python has five standard data types -
  • Numbers
  • String
  • List
  • Tuple
  • Dictionary
What is the difference between list and tuples? List
  • Lists are mutable i.e they can be edited.
  • Lists are slower than tuples.
  • Syntax: list_1 = [10, 'Chelsea', 20]
  • Tuples are immutable that can't be edited.
  • Tuples are faster than list.
  • Syntax: tup_1 = (10, 'Chelsea', 20)
How is memory managed in Python?
  • Python memory is managed by Python private head space and all its Python objects and data structures are situated in a private heap. The programmer cannot access the private heap and interpreter takes care of this Python private heap.
  • Python Memory Manager do the allocation of Python heap space for Python Objects. The core API gives access to some tools for the programmer to code.
  • It also has an inbuilt garbage collector which recycle all the unused memory and free up the memory and makes it available to the heap space.
What is namespace in Python? A namespace is a naming system that is used to make sure that names are unique to avoid naming conflicts. What are python modules? Name some commonly built-in modules in Python? Python modules are files containing Python Code. This code can either be functions classes or variables. A python module is a .py file containing executable code.

Some of the commonly used built-in modules are:

  • OS
  • sys
  • math
  • random
  • data time
  • JSON
What is dictionary in python? Dictionary is the built-in datatypes in Python that defines one-to-one relationship between keys and values. Dictionaries are indexed by keys and contains pair of keys and their corresponding values.

For Example: 
It contains some keys like Country, State, State Capital. 
Their corresponding values are India, Rajastan and Jaipur respectively. dict={'Country':'India','State':'Rajasthan','State Capital':'Jaipur'} print dict[Country] Output is India
What is type conversion in Python? Type conversion refers to the conversion of one data type into another.
  • int() - Converts data type into integer type
  • float() - Converts data type into float type
  • ord() - converts characters into integer
  • hex() - converts integers to hexadecimal
  • oct() - converts integer to octal
  • tuple() - used to convert to a tuple
  • set() - It returns the type after converting to set
  • list() - Converts data type into list type
  • dict() - Convert a tuple of order(key,value) into a dictionary
  • str() - Convert integer into a string
  • complex(real,imag) - Converts real numbers to complex(real,imag) number
Explain split(), sub(), subn() methods of "re" module in Python. Python "re" module has 3 methods to modify the strings and they are:
  • split() - uses a regrex pattern to "split" a given string into a list.
  • sub() - finds all substrings where the regex pattern matches and then replace them with a different string
  • subn() – it is similar to sub() and also returns the new string along with the no. of replacements.
How to get indices of N maximum values ina NumPy array? We can get the indices of N maximum values in a NumPy array using the given code:

	import numpy as np
	arr = np.array([1, 3, 2, 4, 5])
How to display the contents of text file in reverse order?
  • Firstly, convert the given file into alist.
  • Now, by using reversed(), reverse the list.
	Eg: reversed(list(open(“file-name”,”r”))):
What is self in python? Self is basically an instance or an object of a class, This is explicitly included as the first parameter in python. It also helps to differentiate between the methods and attributes of a class with local variables.
The self variable in the init method refers to the newly created object while in other methods, it refers to the object whose method was called.
How does break, continue and pass work?
  • Break - It allows loop termination when some condition is met and the control is transferred to the next statement.
  • Continue - It allows skipping some part of a loop when some specific condition is met and the control is transferred to the beginning of the loop.
  • Pass - It is used when you need some block of code syntactically, but you want to skip its execution. This is basically a null operation. Nothing happens when this is executed.
Write a Python program to count the number of lines in a text file.
   def file_lengthy(fname):
   with open(fname) as f:
   for i, I in enumerate(f):
   pass return i + 1
   print("Number of lines in the file:


Lets get Confident to

Enter in Coding World

Institute provide Training on Object Oriented Programing Course with Certificate and JOB