Python Interview Questions

01 February 2023

Intoduction to Python:

Python is a high-level, interpreted programming language that is widely used for web development, data science, machine learning, and scientific computing. It is a general-purpose language that is known for its simplicity, readability, and flexibility.

Python was created by Guido van Rossum in 1991 and has since become one of the most popular programming languages in the world. It has a large, active community of users and developers, and a wealth of libraries and frameworks that make it a versatile language for a wide range of tasks.

One of the key features of Python is its dynamic, object-oriented nature, which allows for the creation of modular, reusable code. Python also has a strong emphasis on simplicity and readability, which makes it a great language for beginners. It also supports multiple programming paradigms, including procedural, functional, and object-oriented programming.

Python is often used for web development, data analysis and visualization, machine learning, and scientific computing. It is also widely used in scripting and automation tasks. Some of the popular applications and libraries that use Python include Django, Flask, Pandas, NumPy, and TensorFlow.

What is an Interpreted language?

An Interpreted language executes its statements line by line. Languages such as Python, Javascript, R, PHP, and Ruby are prime examples of Interpreted languages. Programs written in an interpreted language runs directly from the source code, with no intermediary compilation step.

What is PEP 8 and why is it important?

PEP stands for Python Enhancement Proposal. A PEP is an official design document providing information to the Python community, or describing a new feature for Python or its processes. PEP 8 is especially important since it documents the style guidelines for Python Code.

What are lists and tuples? What is the key difference between the two?

Lists and Tuples are both sequence data types that can store a collection of objects in Python. The objects stored in both sequences can have different data types. Lists are represented with square brackets ['talent', 16, 7.26], while tuples are represented with parentheses ('battle', 15, 2.56).
The key difference between the two is that while lists are mutable, tuples on the other hand are immutable objects. This means that lists can be modified, appended or sliced on the go but tuples remain constant and cannot be modified in any manner.

What is pass in Python?

Ans: The pass keyword represents a null operation in Python. It is generally used for the purpose of filling up empty blocks of code which may execute during runtime but has yet to be written.

What is the use of self in Python?

Ans: 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. self is used in different places and often thought to be a keyword. But unlike in C++, self is not a keyword in Python.

What is init?

Ans: init is a constructor method in Python and is automatically called to allocate memory when a new object/instance is created. All classes have a _init_ method associated with them. It helps in distinguishing methods and attributes of a class from local variables.

What is break, continue and pass in Python?

Break: The break statement terminates the loop immediately and the control flows to the statement after the body of the loop.
Continue: The continue statement terminates the current iteration of the statement, skips the rest of the code in the current iteration and the control flows to the next iteration of the loop.
Pass: As explained above, the pass keyword in Python is generally used to fill up empty blocks and is similar to an empty statement represented by a semi-colon in languages such as Java, C++, Javascript, etc.

What is docstring in Python?

Ans: Documentation string or docstring is a multiline string used to document a specific code segment.
The docstring should describe what the function or method does. 

Ask Us Anything !