Question d’entretien chez Softeon

What is Python, and what are its key characteristics? Answer: Python is a high-level, interpreted programming language known for its simplicity and readability. Key characteristics include dynamic typing, automatic memory management, and an extensive standard library. Explain the differences between Python 2 and Python 3. Answer: Python 3 is the latest version of the language and has some key differences, such as print statements requiring parentheses, integer division returning a float, and the range() function returning an iterable object rather than a list. What is PEP 8, and why is it important? Answer: PEP 8 is Python Enhancement Proposal 8, a style guide for writing clean and readable Python code. It's essential for maintaining code consistency and making it easier for developers to collaborate on projects. How do you comment your code in Python, and what's the difference between single-line and multi-line comments? Answer: In Python, you can use the # symbol for single-line comments and triple-quotes (''' or """) for multi-line comments. Multi-line comments are often used for docstrings to provide documentation for functions, classes, or modules. What are the differences between a list and a tuple in Python? Answer: Lists are mutable, meaning their elements can be modified after creation, while tuples are immutable and cannot be changed. Lists are defined with square brackets [ ], and tuples with parentheses ( ). Explain the concept of a Python dictionary. Answer: A dictionary is an unordered collection of key-value pairs. It allows you to store and retrieve values using a unique key. Dictionaries are defined using curly braces { }. What is the Global Interpreter Lock (GIL) in Python, and how does it affect multi-threading? Answer: The GIL is a mutex that allows only one thread to execute in the Python interpreter at a time. This limitation can impact multi-threaded performance, particularly for CPU-bound tasks. However, it doesn't affect multi-processing. How do you handle exceptions in Python, and what are common built-in exceptions? Answer: Exceptions are handled using try, except blocks. Common built-in exceptions include SyntaxError, TypeError, and ZeroDivisionError. You can also create custom exceptions by subclassing Exception. What is a virtual environment in Python, and why is it useful? Answer: A virtual environment is an isolated Python environment that allows you to install and manage packages independently of the system-wide Python installation. It's useful for avoiding package conflicts and maintaining project-specific dependencies. Explain list comprehensions in Python and provide an example. Answer: List comprehensions are a concise way to create lists. For example, to create a list of squares from 1 to 10: python Copy code squares = [x ** 2 for x in range(1, 11)] What are decorators in Python, and how are they used? Answer: Decorators are functions that modify the behavior of other functions or methods. They are often used to add functionality to functions or methods, such as logging or authentication. Explain the difference between __init__ and __call__ methods in a Python class. Answer: The __init__ method is called when an instance of a class is created to initialize its attributes. The __call__ method allows instances of a class to be called like a function, and it's defined in the class to customize its behavior. What is a lambda function in Python? Provide an example. Answer: A lambda function is a small, anonymous function defined using the lambda keyword. It's often used for short, simple operations. For example, a lambda function to add two numbers: