Core Python Questions

Essential knowledge for every Python developer.

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

Answer:

2. Explain `*args` and `**kwargs`.

Answer:

def func(*args, **kwargs):
    print(args)   # (1, 2)
    print(kwargs) # {'a': 3}

func(1, 2, a=3)

3. How does Python handle memory management?

Answer: Python uses a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager.

4. What are Python decorators?

Answer: A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. It is usually a function that takes another function as an argument and extends its behavior.

5. What are Generators and the `yield` keyword?

Answer: Generators are functions that return an iterable set of items, one at a time, in a special way.

6. Explain Context Managers (`with` statement).

Answer: Context managers allow you to allocate and release resources precisely when you want to. The most widely used example is the with statement.

with open('file.txt', 'w') as f:
    f.write('hello')

It ensures that the file is closed automatically, even if an exception occurs. It uses the __enter__ and __exit__ magic methods.

7. What is the difference between `is` and `==`?

Answer: