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 to Java?
Both Python and Java are the most popular languages. Even though Java is faster than python as a beginner I need to opt for the one which is easier to learn like java python can be used everywhere nowadays which included Data Science, Machine Learning Applications, and AI related Applications because it has over 3 million pre-defined packages which make implementation easier.
3. What are keywords in python? Define any 5 among them.
Like in other programming languages keywords are nothing but reserved words which are meant for some specific functionalities when they are used in python programs. In python, there are 32 keywords some of which 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 function
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 python is a 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. Is strings are mutable and immutable in python?
In python, strings are defined as the immutable collection of characters which means once the initialization is done, we cannot modify it. The only way to do it using the string functions present in python.
6. Explain Docstring in python.
The Docstring is an acronym for Document String which is used for documentation for the code written in the python program. The docstring in python starts and ends with triple quotes(“or ‘’’).
7. What is the difference between del() and remove() in python.
The del() method is used to delete all the elements of a list in the given specified range and when it comes the remove() method is used to delete the first occurrence of a character in the given list
8. Functions and their types in python
The functions in python are used for code reusability. They are defined using the def keyword in python. There are three types of functions Built-In Functions, User Defined Functions, and lambda Functions.
9. What are the different types of arguments we can pass to function in python?
In general, the arguments are the actual values we pass to the function during the 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
10. What is the difference between a function and a 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 definition can contain functions along with classes and attributes.
11. What is the difference between a package and a module in python?
The package is a collection of modules where that 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.
12. Collections in Python
Collections in python are used to store a collection/ group of elements under a single variable. The collection in python includes List, Tuples, Set, and Dictionary.
13. Define class and Object. Implement them using python
Class is defined as a 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 the object is a 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 includes
Inheritance – The process of inheriting properties of the parent class to the base class
Encapsulation – The process of binding data members and member functions of a class
Polymorphism – Simply function with the same name but with different attributes
Data Abstraction – Hiding the sensitive code from others can be achieved by creating abstract classes in python.
15. Why Functions are Treated as First Class Objects in Python?
The functions can be passed as arguments to other functions, we can assign them to a variable and also, and we can even return functions in python. So simply we can say Functions are the pillars of python procedural programming so they are treated as first-class objects.
Advanced Python Interview Questions
1. 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.
2. Define generators and their use in python
Generators functions are special functions in python. When one wants to return an iterator or a group of statements he uses the yield keyword instead of the return keyword. The generator functions are defined using the syntax function* function_name.
3. Can we assign more than one value to a variable in python?
Yes, we can assign more than one value to a variable and all of them are considered by the default interpreter as elements of a tuple and stores them as a tuple.
4. Define decorators in python.
Decorators are used to calling the function before their definition of the function we want to decorate. Simply we can decorators are used to enhance the functionality of existing objects without changing their structure.
5. What is PEP 8 in python?
The PEP stands for Python Enhancement Proposal. It is a document that is responsible that navigating the users of python and how to write and structure the python code.
6. Is there any concept of a switch statement in python?
Yes, in the very latest version of python (I.e. python3.10) they introduced the concept which is very similar to the switch case in other languages and it is known as match case in python. This concept was not present till python3.9.
7. How are constructors created in python?
The constructors in python are created as __init__(self) which is a private method that’s why we were using those double underscores before its initialization. There is no concept of constructor overloading and constructor overriding in python as in java because here constructors are private methods.
8. What is meant by the Anonymous function in python?
The name itself suggests that Anonymous functions are the type of function which are declared but not defined with any name. We can implement these types of functions using the lambda keywords in python.
9. How to sort the list in python? or What is the difference between sort() and sorted() methods?
The lists are the mutable collection of objects in python and they can be sorted using either sort() or sorted() method but the difference between them is the sort method is going to return an object whereas the sorted() is going to return the sorted list itself.
10. Explain the concept of Pickling and Unpickling in python?
In general pickling and unpickling are used to serialize the python objects. Elaboratively, The process of converting the python objects into byte stream is called Pickling and vice versa. This process can be achieved using the pickle package in python.
11. Can you name some packages in python used in Data Science?
Numpy – Numeric Python is mostly used for data processing.
Pandas – Python Data Analysis is mostly used for data analysis.
Matplotlib – Used for visualizing the data patterns.
Scipy – Scientific Python used to perform complex calculations.
Tensorflow – Framework basically designed to build deep learning and neural network models.
12. Explain the difference between Flask, Pyramid, and Django.
Even though all of them are python frameworks based on their extensibility and flexibility Flask is considered as a “Micro Framework”. When it comes to Pyramid it’s more flexible to developers and Django is used to build larger applications and provide comparatively more tools to implement the project.
13. Discuss Exception Handling in Python.
Exception handling is a mechanism that is used to suppress unwanted errors during the run of a program. This can be achieved using try, except, and finally keywords in python.
14. Use of self keyword in python.
The self keyword is used in classes to ensure that the defined method is the current instance of the class and the variables defined with the self keyword ensure that they belong to the current instance and can be accessed when we create objects to that class.
15. Write a Program to convert two lists as keys and values in a dictionary.
The two lists in python can be converted as key and value pairs of a dictionary using dict(zip()) method. The lists that we want to convert should be of the same length (even though they are different lengths the python interpreter converts the lists as dictionaries up to the small-sized list).
L1=[1,2,3,4]
L2=[ ‘one’,’ two’,’ three’, four’ ]
D=dict(zip(L1,L2))
print(D)
Output:{1:’one’,2:’two’,3:’three’,’4’:’four’}