- What is Python?
- Python is a high-level, interpreted, and general-purpose programming language.
- Explain the difference between Python 2 and Python 3.
- Python 2 is legacy and no longer maintained, while Python 3 is the current version with improvements and backward-incompatible changes.
- How do you comment in Python?
- Single-line comments start with #, and multiline comments are enclosed in triple quotes ('''or""").
- What is PEP 8?
- PEP 8 is the style guide for Python code, providing recommendations on formatting, naming conventions, and code structure.
- How to create a virtual environment in Python?
- python -m venv myenvcreates a virtual environment named “myenv.”
- What is the purpose of the __init__method in Python classes?
- __init__is a constructor method called when an object is created. It initializes the object’s attributes.
- Explain list comprehensions in Python.
- List comprehensions provide a concise way to create lists. For example, [x**2 for x in range(5)]generates a list of squares.
- How is exception handling done in Python?
- Exceptions are handled using try,except,else, andfinallyblocks.exceptcatches specific exceptions, andelseexecutes if no exception occurs.
- What is the difference between append()andextend()methods for lists?
- append()adds an element to the end of a list, while- extend()adds elements from an iterable.
- How do you open and read a file in Python?
- Use open('filename', 'r')to open a file in read mode, andfile.read()to read its contents.
 
- Use 
- What is the Global Interpreter Lock (GIL) in Python?
- The GIL is a mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecodes at once.
 
- Explain the use of decorators in Python.
- Decorators modify or enhance functions or methods. They are denoted by the @decoratorsyntax.
 
- Decorators modify or enhance functions or methods. They are denoted by the 
- What is the purpose of __name__in Python?- __name__is a special variable that holds the name of the current module. It is set to- "__main__"if the module is run as the main program.
 
- How do you perform string formatting in Python?
- String formatting can be done using %operator,format()method, or f-strings (Python 3.6+).
 
- String formatting can be done using 
- What is the purpose of the yieldkeyword in Python?- yieldis used in generator functions to produce a series of values, suspending the function’s state between calls.
 
- Explain the concept of a lambda function in Python.
- Lambda functions are anonymous functions defined using the lambdakeyword. They are often used for short, one-time operations.
 
- Lambda functions are anonymous functions defined using the 
- How do you handle file exceptions in Python?
- File exceptions can be handled using try,except, andfinallyblocks. Common exceptions includeFileNotFoundErrorandPermissionError.
 
- File exceptions can be handled using 
- What are docstrings in Python?
- Docstrings are documentation strings enclosed in triple quotes that provide information about a module, function, class, or method.
 
- How do you remove duplicates from a list in Python?
- Convert the list to a set using set(), then back to a list to eliminate duplicates.
 
- Convert the list to a set using 
- What is the purpose of the __str__method in Python?- __str__is a method in Python classes that returns a human-readable string representation of an object when- str()is called.
 
- Explain the purpose of the withstatement in Python.- The withstatement simplifies resource management by ensuring that the__enter__and__exit__methods are called.
 
- The 
- How do you install external libraries in Python?
- Use pip install library_nameto install external libraries using the pip package manager.
 
- Use 
- What is the purpose of the super()function in Python?- super()is used to call a method from a parent class, enabling access to inherited methods and attributes.
 
- What is a Python decorator used for?
- A decorator is used to modify the behavior of a function or method. It is applied using the @decoratorsyntax.
 
- A decorator is used to modify the behavior of a function or method. It is applied using the 
- How do you reverse a list in Python?
- Use the [::-1]slicing notation or thereverse()method to reverse a list.
 
- Use the 
- **Explain the purpose of the enumerate()function in Python.**- enumerate()is used to iterate over a sequence, providing both the index and the value.
 
- What is the purpose of the map()function in Python?- map()applies a function to all items in an input list and returns an iterator of results.
 
- How is memory management handled in Python?
- Python uses automatic memory management through a garbage collector. Developers do not need to manually allocate or deallocate memory.
 
- Explain the purpose of the breakandcontinuestatements in Python loops.- breakis used to exit a loop prematurely, and- continueis used to skip the rest of the code and move to the next iteration.
 
- What is the purpose of the delstatement in Python?- delis used to delete objects or elements, such as variables, lists, or slices.
 
- How do you convert a string to lowercase in Python?
- Use the lower()method to convert a string to lowercase.
 
- Use the 
- What is the purpose of the jsonmodule in Python?- The jsonmodule is used for encoding and decoding JSON data.
 
- The 
- Explain the concept of a generator in Python.
- A generator is a special type of iterator created using a function with yieldstatements. It generates values one at a time, saving memory.
 
- A generator is a special type of iterator created using a function with 
- How do you handle an ImportError in Python?
- Use a tryandexcept ImportErrorblock to handle import errors.
 
- Use a 
- What is the purpose of the __dict__attribute in Python?- __dict__is a dictionary containing an object’s attributes.
 
- Explain the difference between deep copy and shallow copy in Python.
- A shallow copy creates a new object, but does not clone nested objects. A deep copy creates a new object and recursively clones all nested objects.
 
- How do you get the length of a string in Python?
- Use the len()function to get the length of a string.
 
- Use the 
- What is the purpose of the isoperator in Python?- isis used to check if two variables refer to the same object in memory.
 
- Explain the purpose of the assertstatement in Python.- assertis used for debugging and testing. It raises an- AssertionErrorif the given expression is- False.
 
- How do you find the maximum value in a list in Python?
- Use the max()function to find the maximum value in a list.
 
- Use the 
- What is the purpose of the any()andall()functions in Python?- any()returns- Trueif at least one element in an iterable is- True.- all()returns- Trueif all elements are- True.
 
- Explain the purpose of the zip()function in Python.- zip()combines two or more iterables element-wise, creating an iterator of tuples.
 
- How do you convert a string to an integer in Python?
- Use int()to convert a string to an integer.
 
- Use 
- What is the purpose of the repr()function in Python?- repr()returns a string representation of an object, suitable for debugging.
 
- How do you create a tuple in Python?
- Use parentheses ()to create a tuple. For example,my_tuple = (1, 2, 3).
 
- Use parentheses 
- What is the purpose of the set()function in Python?- set()creates an empty set or converts an iterable to a set, removing duplicates.
 
- How do you concatenate strings in Python?
- Strings can be concatenated using the +operator or thejoin()method.
 
- Strings can be concatenated using the 
- Explain the purpose of the staticmethoddecorator in Python.- staticmethodis used to define a static method in a class that does not access or modify class or instance state.
 
- What is the purpose of the format()method in Python strings?- format()is used for string formatting, replacing placeholders- {}with values.
 
- How do you check if a key is present in a dictionary in Python?
- Use the inoperator to check if a key is present in a dictionary. For example,if 'key' in my_dict:.
 
- Use the