Challenge your understanding of essential Python concepts commonly featured in interviews. Improve your grasp on programming fundamentals and data structures with these carefully selected questions.
In Python, what is the purpose of the __init__ method within a class?
Explanation: The __init__ method is called automatically when a new instance of a class is made and is used to initialize that object's attributes. It does not delete objects (that is typically handled by other methods), nor is it solely used for inheritance. It also has no relation to defining global variables.
What is the main difference between a Python list and a Python tuple?
Explanation: Lists can have their contents changed after creation, making them mutable, whereas tuples cannot be changed (immutable). Both lists and tuples can store any data type, tuples do not always require more memory, and both types can be iterated over.
Which keyword in Python is used when you want to exit a loop immediately?
Explanation: The 'break' keyword is used to exit a loop before it would normally finish. 'continue' skips to the next loop iteration, 'pass' does nothing and acts as a placeholder, and 'exit' is not a loop control keyword.
In Python, what is a docstring?
Explanation: Docstrings are special strings at the beginning of modules, functions, classes, or methods that explain their purpose. They are not variable names, they are not ignored comments (they can be accessed), and they are not related to string formatting errors.
How can you make a Python script executable on Unix systems?
Explanation: To make a Python script executable on Unix, you must add a shebang line (e.g., #!/usr/bin/env python3) and set execute permissions. The .py extension alone and uppercase text do nothing for executability, and compilation to bytecode is not required for execution as a script.