Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

What is Python

Uploaded by

sahil fuck
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

What is Python

Uploaded by

sahil fuck
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1. What is Python?

 Answer: Python is a high-level, interpreted programming language known for its


simplicity and readability. It supports multiple programming paradigms, including
procedural, object-oriented, and functional programming.

2. What are the key features of Python?

 Answer: Key features of Python include:

 Simple and easy-to-learn syntax.

 Dynamic typing and automatic memory management.

 Extensive standard library.

 Support for multiple programming paradigms.

 Interpreted nature, making it suitable for rapid development.

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.

4. What are the different data types in Python?

 Answer: Python supports various data types, including:

 Integers (int).

 Floating-point numbers (float).

 Strings (str).

 Lists (list).

 Tuples (tuple).

 Dictionaries (dict).

 Sets (set).

5. How do you comment out multiple lines of code in Python?

 Answer: You can use triple quotes (''' or """) to comment out multiple lines of code:

python

Copy code

''' This is a multi-line comment '''

Advanced Python Questions:

6. Explain the difference between __str__ and __repr__ methods.


 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.

7. What are decorators in Python?

 Answer: Decorators are functions that wrap other functions or methods to modify their
behavior. They allow adding functionality to existing functions dynamically.

8. Explain the difference between == and is operators in Python.

 Answer:

 ==: Compares the values of two objects.

 is: Compares the identities of two objects (memory address). It returns True if
both objects point to the same memory location.

9. What is the difference between map() and filter() functions?

 Answer:

 map(): Applies a function to all elements in an iterable and returns an iterator of


the results.

 filter(): Filters elements from an iterable based on a function that returns True
or False.

10. What is a generator in Python?

 Answer: A generator is a function that returns an iterator object lazily. It allows


generating values one at a time and is memory-efficient.

11. What is a list comprehension?

 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.

12. What is the difference between a tuple and a list?

 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 (()).

 Lists have more built-in methods for manipulation compared to tuples.


13. Explain the use of if __name__ == "__main__": in Python scripts.

 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.

14. What is a dictionary comprehension?

 Answer: Similar to list comprehension, dictionary comprehension is a concise way to


create dictionaries in Python. It allows you to create a new dictionary by applying an
expression to each item in an existing iterable.

15. Explain the difference between append() and extend() methods for lists.

 Answer:

 append(): Adds a single element to the end of the list.

 extend(): Adds elements from an iterable (such as another list) to the end of the
list.

Advanced Python Questions:

16. What are lambda functions in Python?

 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:

 *args: Used to pass a variable number of non-keyword arguments to a function.


It collects the extra arguments as a tuple.

 **kwargs: Used to pass a variable number of keyword arguments to a function.


It collects the extra arguments as a dictionary.

18. What is a context manager in Python? Provide an example.

 Answer: A context manager is an object that manages resources within a with


statement. It ensures that resources are properly acquired and released. An example is
the open() function for file handling:

with open('file.txt', 'r') as f:

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.

20. What is a module in Python?

 Answer: A module is a file containing Python code, typically containing functions,


classes, and variables. It allows organizing code into reusable units and provides
namespaces to avoid naming conflicts.

21. What is the difference between == and is operators in Python?

 Answer:

 ==: Checks for equality of values between two objects.

 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.

23. What is a docstring in Python?

 Answer: A docstring is a string literal used as a comment at the beginning of a function,


module, class, or method to document its purpose, usage, and parameters. It is accessed
using the __doc__ attribute.

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.

 __getattribute__(): Called for every attribute access on an object, regardless of


whether the attribute exists or not. It allows customizing attribute access for all
attributes.

25. What is the purpose of the __init__() method in Python classes?

 Answer: The __init__() method is a special method (constructor) called automatically


when a new instance of a class is created. It initializes the object's state by setting initial
values for attributes.
Advanced Python Questions:

26. What are decorators used for in Python?

 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.

27. Explain the Global Interpreter Lock (GIL) in Python.

 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.

28. What are metaclasses in Python?

 Answer: Metaclasses are classes that define the behavior of classes. They allow
customization of class creation and modification of class attributes and methods.

29. Explain the difference between asyncio and threading in Python.

 Answer:

 asyncio: Provides asynchronous I/O support using coroutines, allowing non-


blocking I/O operations. It's suitable for I/O-bound tasks.

 threading: Provides multi-threading support, allowing concurrent execution of


multiple threads. It's suitable for CPU-bound tasks and I/O-bound tasks that
release the GIL.

30. What is a context manager in Python?

 Answer: A context manager is an object that manages resources within a with


statement. It ensures that resources are properly acquired and released, even in case of
exceptions.

1. Explain the four principles of OOP.

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.

What is method overloading and method overriding in Python?

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.

4. What is method chaining in Python?

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.

5. What is a dunder method in Python?

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.

6. Explain the purpose of super() in Python.

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.

7. What is multiple inheritance, and does Python support it?


Answer: Multiple inheritance is the ability of a class to inherit attributes and methods from multiple
parent classes. Python supports multiple inheritance, allowing a subclass to inherit from multiple
superclasses.

8. What is method resolution order (MRO) in Python?

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.

9. Explain the difference between __str__() and __repr__() methods in Python.

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.

You might also like