Example HTML page

Python- Interview Questions

Python Programming

(If you are viewing this page in mobile, kindly switch to desktop view for better visual appearance).

The following are the basic interview questions in Python.

1. Mention any four key features of Python and explain them in one-liners?

A. Python has different features. Four of the key features of Python are:

  • Interpreted – Python is said as an Interpreted language. Because, it need not to be compiled before it run unlike the other languages.
  • Dynamically typed – In Python, it is not needed to mention the variable type but we can declare them directly as x= “hi”, y=2 and so on.
  • Object oriented programming- It allows the class definitions including with composition and inheritance.
  • Coding is quick – we can write python code quickly compared to other programming languages.

2. Define PEP 8.

A. PEP is Python Enhancement Proposal. A set of rules which specifies the python code format in order to reach maximum readability is PEP.

3. Define namespace in Python.

A. A namespace is a naming system that confirms the names are unique to ignore naming conflicts.

4. Give an example code for global and local variables declaration.

A.

>>> x=3	#Global variable
>>> def div( ):
	y=18                          	#Local variable
	z=y/x
	print(z)

	
>>> div( )
6.0

5. What is meant by Type conversion in Python. Provide examples.

A. Type conversion refers to the conversion of one data type into another. int( ), float( ),set( ), list( ), dict( ) and so on are the examples of type conversion.

6. What is the significance of indentation in Python?

A. Indentation in python will specify a section of code. The entire code existing inside the loops statements, classes, functions and so on is specified within an indentation block. Usually it is done by the use of four space characters. If your code is not intended properly then execution of your code fails and raises an error. Hence Indentation is must in Python.

7.What do you mean by __init__?

A. __init__ is a method or constructor in Python. This method is automatically called to allocate memory when a new object/ instance of a class is created. All classes includes __init__ method.

8. Define Lambda function. Give an example for it.

A. An anonymous function is known as a lambda function. This function can have any number of parameters but, can have just one statement.

Ex:

>>> x=lambda a,b : a*b
>>> print(x(5,8))
40

9. What are the built-in types of python?

A. The following are the built-in types in Python.

  • Integers
  • Floating-point
  • Complex numbers
  • Strings
  • Boolean
  • Built-in functions

10. What do you mean by iterators in Python?

A. Iterators are objects which can be traversed though or iterated upon.

11. Define Random module in Python. How can we import it?

A. Random module is the standard module that generates a random number. We can import it as follows:

>>> import random
>>> random.random

The statement “import random” helps in importing a random module whereas the statement random.random( ) method return the floating point number that is in the range of [0, 1).

12. Is Python language a programming or scripting?

A. Python is a scripting language but generally it is considered as a general purpose programming language.

13. How can you say that “Python is an Interpreted language”?

A. Any programming language is said as Interpreted when the code is not in machine level before runtime. But Python is not. Hence Python is an Interpreted language.

14.How the memory management done in Python?

A. Memory is managed in Python by Python private heap space. All the python objects as well as data structures are located in a private heap. As the programmer has no access on this private loop so the python interpreter manages it.

15. What is meant by PYTHONPATH?

A. PYTHONPATH is an environment variable that can be used after a module get imported. It also looked up to check whether the imported modules are presented in different directories. In order to specify which module to be imported, the interpreter uses PYTHONPATH.

16.Define Python modules List out some built-in modules in Python which are commonly used.

A. Python modules are the files comprising of Python code. The code might be functions, classes or variables. An executable code in python file has “.py” extension. Os,sys,math,random, date time are few of the commonly used built-in modules.

17.Can you state the difference between arrays and lists in Python with an example?

A. In Python, arrays and lists are having the similar way of storing the data. But in case of data type elements, arrays can hold only single data type elements whereas lists can hold any data type elements.

Ex:

>>> import array as arr
>>> Array_1=arr.array("i",[5,3,1,8])
>>> List_1=[6,"VMS",8.62]
>>> print(Array_1)
array('i', [5, 3, 1, 8])
>>> print(List_1)
[6, 'VMS', 8.62]

18. Define functions with an example?

A. A function is a block of code which is executed only when it is called. ‘def’ is a keyword that is used to define a function in Python.

Ex:

>>> def func( ):
	print("VMS class welcomes you")

>>> func();
VMS class welcomes you

19. Define Lambda function with an example.

A. An anonymous function is known as a lambda function. This function can have multiple parameters but, can have only one statement.

Ex:

>>> p=lambda q,r : q+r
>>> print(p(6,5))
11

20. What does ‘self’ means in python? State its importance.

A. An instance or an object of a class refers to a self in Python. It is explicitly added as the first parameter. ‘self’ helps to distinguish the methods and attributes of a class with the local variables. The ‘self’ variable in the ‘init’ method refers to the newly created object whereas in other methods, it refers to the object whose method was called.

21. Define the terms a) break b)continue and c)pass.

A. a) break – It allows loop termination when some condition becomes true and the control is transferred to the next statement.

b) continue – It allows skipping some section of loop when some specific condition becomes to true and the control is transferred to the beginning of the loop.

c) pass –This 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.

22. What will be the output for the following section of code? Explain how.

>>> import array as arr

>>> A=arr.array(“i”,[9,7,5,3,1])

>>> A[::-1]

A.

array(‘i’, [1, 3, 5, 7, 9])

Explanation:

The statement “A[::-1]“ reprints the data structures (such as an array or list)  in reverse order such as an array or a list. The actual array or list will not be changed.

23. How does randrange(x,y) works?

A. randrange(x,y) selects an integer and defines the range in between [x,y) . It returns the elements by random selection from the specified range.

24.How does uniform(a, b) works?

A. uniform(a,b) selects a floating point number that is defined in the range of [a,b).It returns the floating point number.

25.How the comments can be written in Python? Give an example.

A. By using “#” character we can start comments in Python. However, alternatively at times, commenting is done using docstrings(strings enclosed within triple quotes).

Ex:

#Comments in Python will be like

print(“# can be used to start a comment in Python”)

Output

“# can be used to start a comment in Python”

26. Define Picklinkg.

A. ‘Pickle’ is a module which accepts any Python object and converts it into string format and dumps it into a file by using a function known as “dump”. This process is called as ‘Pickling’.

27.What is unpickling?

A. The process of retrieving original Python objects from the stored string representation is known as ‘Unpickling’.

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest