What is Python
What is Python
3. What is PEP 8?
Answer: PEP 8 is the official style guide for Python code. It provides guidelines and best
practices for writing Python code to improve its readability and maintainability.
Integers (int).
Strings (str).
Lists (list).
Tuples (tuple).
Dictionaries (dict).
Sets (set).
Answer: You can use triple quotes (''' or """) to comment out multiple lines of code:
python
Copy code
Answer: Decorators are functions that wrap other functions or methods to modify their
behavior. They allow adding functionality to existing functions dynamically.
Answer:
is: Compares the identities of two objects (memory address). It returns True if
both objects point to the same memory location.
Answer:
filter(): Filters elements from an iterable based on a function that returns True
or False.
Answer: List comprehension is a concise way to create lists in Python. It allows you to
create a new list by applying an expression to each item in an existing iterable.
Answer:
Lists are mutable, meaning their elements can be changed after creation. Tuples
are immutable; once created, their elements cannot be changed.
Lists are defined with square brackets ([]), while tuples are defined with
parentheses (()).
Answer: This construct allows a Python file to be both used as a standalone script and
imported as a module. The code inside the if __name__ == "__main__": block will only
run if the script is executed directly, not when imported as a module.
15. Explain the difference between append() and extend() methods for lists.
Answer:
extend(): Adds elements from an iterable (such as another list) to the end of the
list.
Answer: Lambda functions, also known as anonymous functions, are small, one-line
functions defined using the lambda keyword. They are used for short, simple operations
where defining a separate function is unnecessary.
17. **Explain the concept of *args and kwargs in Python function definitions.
Answer:
data = f.read()
19. Explain the concept of inheritance in Python.
Answer: Inheritance allows a class (subclass or child class) to inherit properties and
behavior from another class (superclass or parent class). It promotes code reuse and
supports the creation of a hierarchy of classes.
Answer:
is: Checks for identity, i.e., whether two objects refer to the same memory
location.
22. Explain the difference between deepcopy() and copy() in Python's copy module.
Answer:
copy(): Creates a shallow copy of the object, i.e., a new object is created, but the
content itself is not duplicated.
deepcopy(): Creates a deep copy of the object, i.e., a new object is created, and
all of its contents are duplicated recursively.
24. Explain the difference between __getattr__() and __getattribute__() methods in Python.
Answer:
__getattr__(): Called when an attribute lookup fails. It's invoked only if the
attribute is not found through the normal lookup process.
Answer: Decorators are used to modify or extend the behavior of functions or methods.
They allow adding functionality to existing functions dynamically, without modifying
their code.
Answer: The Global Interpreter Lock (GIL) is a mutex that allows only one thread to
execute Python bytecode at a time, even in a multi-threaded environment. It prevents
multiple threads from executing Python bytecode concurrently, impacting parallelism in
CPU-bound tasks.
Answer: Metaclasses are classes that define the behavior of classes. They allow
customization of class creation and modification of class attributes and methods.
Answer:
Answer:
Encapsulation: Bundling data and methods that operate on the data into a single unit (class). It hides the
internal state of objects and restricts access to them through methods.
Inheritance: Allows a class (subclass) to inherit properties and behavior from another class (superclass).
It promotes code reuse and supports the creation of a hierarchy of classes.
Polymorphism: Refers to the ability of objects to take on multiple forms. It allows objects of different
classes to be treated as objects of a common superclass.
Abstraction: The process of simplifying complex systems by modeling classes based on the essential
characteristics, hiding unnecessary details.
2. What is a class and object in Python?
Answer:
Class: A blueprint for creating objects. It defines attributes (variables) and methods (functions) that
describe the behavior of objects created from the class.
Object: An instance of a class. It represents a specific entity and encapsulates its data and behavior.
Answer:
Method Overloading: The ability to define multiple methods with the same name but different
parameters in a class. Python does not support method overloading directly.
Method Overriding: The process of redefining a method in a subclass that is already defined in the
superclass. It allows the subclass to provide a specific implementation of the method.
3. Explain the difference between instance, class, and static methods in Python.
Answer:
Instance Method: A method that operates on an instance of a class and can access and modify instance
attributes. It takes self as the first parameter.
Class Method: A method that operates on the class itself and can access and modify class-level
attributes. It takes cls as the first parameter and is decorated with @classmethod.
Static Method: A method that does not operate on either the instance or the class and does not have
access to instance or class attributes. It is defined using the @staticmethod decorator.
Answer: Method chaining, also known as fluent interface, is a programming style where multiple
methods are called on the same object in a single statement. Each method call returns the modified
object, allowing for concise and readable code.
Answer: Dunder methods (double underscore methods) are special methods in Python identified by
their names surrounded by double underscores (e.g., __init__, __str__). They provide functionality to
classes and are invoked by specific Python operators or built-in functions.
Answer: super() is used to call methods and access attributes from the superclass within a subclass. It
allows for method overriding while maintaining access to the superclass's behavior.
Answer: Method Resolution Order (MRO) is the order in which Python looks for methods and attributes
in a class hierarchy. It follows the C3 linearization algorithm to determine the sequence in which base
classes are searched.
Answer:
__str__(): Returns a string representation of an object for end-users. It's called by the str() function and
the print() function.
__repr__(): Returns a string representation of an object for developers. It's called by the repr() function
and the interpreter.