Interview Questions || Python


Interview questions || Python 

You all are very well aware of python's trend now-a-days. Being versatile in nature, it's popularity and demand is increasing day by day, as it's applications are increasing.

 Python is simple and easy to learn. Being versatile in nature,dynamically typed and interpreted, it is gaining popularity. Also the debugging of error is easy, which encourages the rapid development of application prototypes, marking itself a language to code with.

We all know that python is used in machine learning, web development, deep learning, data science and visualisation, desktop GUI, audio and video application, CAD application,etc.

Before you start reading this post, do follow Anand Tutorial blogs for more updates and information.

                                  

Here, as you can see the graph, the red line in the graph clearly that how demand of python has increased rapidly over some time.
With that out of the way, lets jump straight to into the Python interview questions so that you can ace any python interview that you are at in  :)

Make sure that you run all the coding part on yourself and practise it for better learning.


Ques~ What are classes and objects in Python ?
             

Classes can  basically be considered as blue prints and objects to be real world entity which are defined and created from classes.

You have various objects around you which are table, chair, fan, etc etc. Every object has some certain characteristics and show some behaviour. Like 
Chair is used to sit, Fan circulates the air.

     

This can simply be understood by the following example:

Here you can see the actual blue print of a house. Now, this blueprint can be used for rapid creation of unlimited no. of copies( which could be a particular house of same kind or you can call them as object). So, these copies of blueprint are nothing but objects.

Classes and Objects

Ques~ What do you understand by inheritance in python? Give examples.
        
Inheritance is basically one class acquiring the property of another class. For example you have inherited your features from your parents and your parents have inherited them  from your grandparents.
   Here, we have this family tree from which you can clearly  understand the inheriting traits. You will notice that hair colour or eye sight have passed from one generation to the other.

 Ques~ What are split(), sub(), and subn() methods in Python?

  • split(): This method is used to split a given string into a list.
  • sub(): This method is used to find a substring where a regex pattern matches, and then it replaces the matched substring with a different string.
  • subn(): This method is similar to the sub() method, but it returns the new string, along with the number of replacements.

These methods belong to Python RegEx ‘re’ module and are used to modify strings.

 Ques~ Explain all file processing modes supported in Python.    

 For opening files, there are three modes

    • read-only mode (r)
    • write-only mode (w)
    • read–write mode (rw)
  • For opening a text file using the above modes, we will have to append ‘t’ with them as follows:
    • read-only mode (rt)
    • write-only mode (wt)
    • read–write mode (rwt)
  • Similarly, a binary file can be opened by appending ‘b’ with them as follows:
    • read-only mode (rb)
    • write-only mode (wb)
    • read–write mode (rwb)
  • To append the content in the files, we can use the append mode (a):
    • For text files, the mode would be ‘at’
    • For binary files, it would be ‘ab’

Python has various file processing modes.




Ques~ How to create an empty class in python ?


An empty class is a class that does not have any code defined within its block. It can be created using the pass keyword.

However, you can create objects of this class outside the class itself.


IN PYTHON THE PASS command does nothing its executed. It's null statement.

For example:

   class b:

                pass

     obj=b()

                     obj.name="mnl"

                                    print("Name=",obj.name)

You can run this code and you will understand how is empty class created.


Ques~ What are negative indexes and why are they used ?

  The  sequence in Python are indexed in consists of the positive as well as negative number. the numbers that are positive uses '0' that is their first index and '1' as their second index and the process goes on like this.

The means that the index value is -1 gives the last element and -2 gives the second last element of an array . For instance our array/list is of size n, then for positive index 0 is the first index, 1 second, last index will be n-1. For negative index, -n is the first index, -(n-1) second, last negative index will be – 1.

 A negative index accesses elements from the end of the list counting backwards.

>>> import array

>>> a= [1, 2, 3]
>>> print a[-3]
1
>>> print a[-2]
2
>>> print a[-1]
3


Ques~  What is self in python ?

The self is used to represent the instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments. 

The reason why we use self is that Python does not use the ‘@’ syntax to refer to instance attributes. In Python, we have methods that make the instance to be passed automatically, but not received automatically.



The use of self makes it easier to distinguish between instance attributes (and methods) from local variables.

 It is not a keyword and has no special meaning in Python. We could use other names (like this ) but it is highly discouraged. Using names other than self is frowned upon by most developers and degrades the readability of the code.


Ques~ What is __init__( ) ?

One important conclusion that can be drawn from the information so far is that the __init__( )method is not a  constructor. Many naive Python programmers  get confused with it since __init__( ) gets called when we create an object.

A closer inspection will reveal that the first parameter in  __init__( ) is the object itself (object already exists). The function __init__(  ) is called immediately after the object is created and used to initialise. 


Refer to this video for further details:
                            

 

Follow Us On:

  

Comments