- 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 myenv
creates 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
, andfinally
blocks.except
catches specific exceptions, andelse
executes if no exception occurs.
- What is the difference between
append()
andextend()
methods for lists? append()
adds an element to the end of a list, whileextend()
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
@decorator
syntax.
- 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
yield
keyword in Python?yield
is 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
lambda
keyword. 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
, andfinally
blocks. Common exceptions includeFileNotFoundError
andPermissionError
.
- 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 whenstr()
is called.
- Explain the purpose of the
with
statement in Python.- The
with
statement 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_name
to 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
@decorator
syntax.
- 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
break
andcontinue
statements in Python loops.break
is used to exit a loop prematurely, andcontinue
is used to skip the rest of the code and move to the next iteration.
- What is the purpose of the
del
statement in Python?del
is 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
json
module in Python?- The
json
module 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
yield
statements. 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
try
andexcept ImportError
block 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
is
operator in Python?is
is used to check if two variables refer to the same object in memory.
- Explain the purpose of the
assert
statement in Python.assert
is used for debugging and testing. It raises anAssertionError
if the given expression isFalse
.
- 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()
returnsTrue
if at least one element in an iterable isTrue
.all()
returnsTrue
if all elements areTrue
.
- 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
staticmethod
decorator in Python.staticmethod
is 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
in
operator to check if a key is present in a dictionary. For example,if 'key' in my_dict:
.
- Use the