Basic Python Questions
- What is Python?
- Answer: Python is a high-level, interpreted programming language known for its readability, simplicity, and flexibility. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
- What are the key features of Python?
- Answer: Key features include:
- Easy to read and write syntax.
- Interpreted language.
- Dynamically typed.
- Extensive standard library.
- Supports multiple programming paradigms.
- Open-source and cross-platform.
- What are Python's built-in data types?
- Answer: Built-in data types include:
- Numeric types: int, float, complex.
- Sequence types: list, tuple, range.
- Text type: str.
- Set types: set, frozenset.
- Mapping type: dict.
- Boolean type: bool.
- Explain the difference between lists and tuples in Python.
- Answer:
- Lists: Mutable, allow duplicates, can contain mixed data types.
- Tuples: Immutable, allow duplicates, can contain mixed data types.
- What is the difference between deep copy and shallow copy?
- Answer:
- Shallow Copy: Copies the reference pointers to the objects, not the objects themselves.
- Deep Copy: Copies the objects as well as the references to the objects.
Python Syntax and Semantics
- What is PEP 8?
- Answer: PEP 8 is the Python Enhancement Proposal which provides guidelines and best practices on how to write Python code. It covers topics like indentation, variable naming, and more to ensure the readability and consistency of Python code.
- Explain the use of decorators in Python.
- Answer: Decorators are a way to modify or extend the behavior of functions or methods without permanently modifying them. They are often used for logging, enforcing access control, instrumentation, and more.
- What are list comprehensions?
- Answer: List comprehensions provide a concise way to create lists. It consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses.
Object-Oriented Programming (OOP) in Python
- What is the difference between
__init__
and __new__
methods?
- Answer:
__new__
: Static method, responsible for creating a new instance of a class.
__init__
: Instance method, responsible for initializing the instance created by __new__
.
- Explain the concept of inheritance in Python.
- Answer: Inheritance allows a class to inherit attributes and methods from another class, promoting code reusability. The class that inherits is called the derived or child class, and the class being inherited from is called the base or parent class.
- What are magic methods in Python?
- Answer: Magic methods, also known as dunder methods, are special methods that start and end with double underscores. Examples include
__init__
, __str__
, __repr__
, __add__
, and more. They allow developers to define the behavior of various operations on objects.
Advanced Python Concepts
- What is the Global Interpreter Lock (GIL) in Python?
- Answer: The GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes simultaneously in a multi-threaded program. It ensures that only one thread executes Python code at a time, which can be a limitation for CPU-bound programs.
- Explain the difference between
@staticmethod
and @classmethod
.
- Answer:
@staticmethod
: A method that does not receive any reference to the instance or class. It behaves like a regular function but belongs to the class's namespace.
@classmethod
: A method that receives the class itself as its first argument (cls
). It can access class attributes and methods but not instance attributes.
- What are generators in Python?
- Answer: Generators are a type of iterable that generate items on-the-fly and only produce items as needed, using the
yield
keyword. They are memory efficient and can be used to work with large datasets.
Python Libraries and Frameworks
- What is the use of the
pandas
library in Python?
- Answer:
pandas
is a powerful data manipulation and analysis library. It provides data structures like Series and DataFrame for handling and analyzing structured data, along with tools for data reading, writing, merging, reshaping, and more.