Python Interview Questions

1. Define Python?

Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation and it has fewer syntactical constructions than other languages.

2. Why did you prefer Python than Java?

Both Python and Java are most popular language. Even though Java is faster than python as a beginner I need to opt for the one which is easier to learn and like java python can be used everywhere now a days which included Data Science, Machine Learning Applications and AI related Applications because it has over 3 million pre-defined packages which makes implementation easier.

3. What are keywords in python? Define any 5 among them?

Like in other programming languages keywords are nothing but the reserved words which are meant for some specific functionalities when they are used in python programs. In python there are 32 keywords some of them are

for,while – used to iterations

in – used to check the existence of subsequence or substring in a collection or string respectively.

return – used to return something from a functions

lambda – used to define an anonymous function in python

def – used to define the functions in python

*All the keywords in python are written in lowercase

4. Basic Data Types in Python?

As the python is dynamically typed language there is no need to define the type of variable, we can simply assign the values to it and the basic data types include Integer, Float, Boolean, String

5. The term namespace in python refers to?

A namespace in python usually refers to the technique for ensuring all the names in the program are unique and distinct to each and every object in the program.

6. Explain Docstring in python.

The DocString in an acronym for Document String which is used to documentation for the code written in python program. The docstring in python starts and ends with triple quotes(“”” or ‘’’).

7. Functions and it’s types in python

The functions in python are used for code reusability. They are define using def keyword in python. There are three types of functions Built-In Functions, User Defined Functions and lambda Functions.

8. What are the different type of arguments we can pass to function in python?

In general, the arguments are the actual values we pass to function during function call. There are four types of arguments that can be passed to functions in python they are

-> Default Arguments

-> Keyword Arguments

-> Positional Arguments

-> Variable Length Arguments

9. What is the difference between function and module in python?

Both functions and modules are similar to each other where the functions are used to define only to obtain some specific functionality but module define can contain functions along with classes and attributes.

10. What is the difference between package and module in python?

Package is a collection of modules where they are used to reduce code redundancy by simply importing them into our program and using the modules or classes which are predefined in it. Whereas modules are defined as the collection of functions along with classes and their attributes.

11. Collections in Python

Collections in python are used to store collection/ group of elements under a single variable. The collection in python includes List, Tuples, Set and Dictionary.

12. Define generators and their use in python?

Generators functions are the special functions in python. When one want to return an iterators or a group of statements he uses yield keyword instead of return keyword. The generator functions are defined using syntax function* function_name.

13. Define class and Object. Implement them using python?

Class is defined as user defined data structure where it holds the data members and member functions of the instance we want to create. Simply class is defined as the blueprint of an object and object is real world entity where we will work on the instance, we created using this object.

14. Explain OOPS concepts available in python?

The OOPS concept in python include

Inheritance – The process of inheriting properties of parent class to base class

Encapsulation – The process of binding data members and member functions of a class

Polymorphism – Simply function with same name but with different attributes

Data Abstraction – Hiding the sensitive code from others this can be achieved by creating abstract classes in python

15. Use of self keyword in python.

The self keyword is used in classes to ensure that the defined method is current instance of the class and the variables defined with self keyword ensures that they belong to current instance and can be accessible when we create objects to that class.