Python Interview Questions & Answers
What is Python?
Python is a high-level, interpreted programming language known for its readability and simplicity.
It emphasizes code readability and allows developers to express concepts in fewer lines ofcode.
How do you comment out multiple lines of code in Python?
You can use triple-quoted strings as a multi-line comment.
Explain the difference between a list and a tuple.
Lists are mutable (can be modified), while tuples are immutable (cannot be modified).
Lists are defined using square brackets [], and tuples use parentheses ().
What is the purpose of the if __name__ =="__main__": statement?
It allows you to execute a block of code only if the script is run directly and not imported as a module into another script.
How is memory management handled in Python?
Python uses an automatic memory management system with a built-in garbage collector that handles memory allocation and deallocation.
Explain the concept of a decorator in Python.
A decorator is a design pattern that allows you to add new functionality to an existing function or method without modifying its structure.
They are commonly used for tasks like logging, authorization, and performance monitoring.
What are docstrings in Python?
Docstrings are strings used as documentation for Python modules, classes, functions, or methods.
They can be accessed using the .__doc__ attribute.
How do you handle exceptions in Python?
You can use try-except blocks to catch and handle exceptions.
What is the Global Interpreter Lock (GIL) in Python?
The Global Interpreter Lock is a mutex used in CPython (the standard Python interpreter) to synchronize access to Python objects.
It prevents multiple native threads from executing Python bytecodes simultaneously.
How can you open and read a file in Python?
You can use the open() function to open a file and various methods like read(), readline(), or readlines() to read its contents.
What is a virtual environment in Python?
A virtual environment is an isolated Python environment that allows you to install packages and dependencies separately from the system-wide Python installation.
This helps in managing project-specific dependencies.
Explain the concept of list comprehension.
List comprehension is a concise way to create lists.
It allows you to create a new list by applying an expression to each item in an existing iterable.
How can you make a copy of a list or dictionary?
You can use the copy() method for lists and the copy() method from the copy module for dictionaries.
Differentiate between deep copy and shallow copy.
A shallow copy creates a new object but does not create copies of nested objects.
A deep copy creates new objects for both the main object and all the nested objects.
How do you define a class in Python?
You define a class using the class keyword.
It contains attributes (variables) and methods (functions).
What is inheritance in Python?
Inheritance is a way to create a new class that inherits properties and behaviors from an existing class.
The new class is called the derived class or subclass, and the existing class is called the base class orsuper class.
How does Python manage memory for objects?
Python uses a private heap space to manage memory for objects.
The memory allocation and deallocation are automatic through built-in functions like id() and the garbage collector.
What is the purpose of the __init__ method in a class?
The __init__ method is a constructor that is automatically called when a new instance of a class is created.
It initializes the attributes of the object.
How can you create a generator in Python?
You can create a generator using a function with the yield keyword. Generators allow you to iterate over a sequence of items without storing the entire sequence in memory.
Explain the concept of a lambda function.
A lambda function is an anonymous function defined using the lambda keyword. It's often used for short, simple operations.
How can you handle and raise exceptions in Python?
You can handle exceptions using try, except, else, and finally blocks.
To raise an exception, you can use the raisestatement.
What is a set in Python?
A set is an unordered collection of unique elements. Sets are defined using curly braces {}.
Explain the use of the map() function.
The map() function applies a given function to each item of an iterable and returns an iterator that yields the results.
What is the difference between append() and extend() in a list?
The append() method adds an item to the end of a list. The extend() method takes an iterable and adds its elements to the end of the list.
How can you remove duplicates from a list?
You can convert the list to a set to remove duplicates and then convert it back to a list.