What's the primary benefit of using tuples as keys in dictionaries over lists?
- Tuples are faster to access.
- Tuples are immutable.
- Tuples are more memory-efficient.
- Tuples guarantee order.
The primary benefit of using tuples as keys in dictionaries is that tuples are immutable, while lists are not. This immutability ensures that the keys won't change, making tuples suitable for use as dictionary keys. Lists can't be used as keys because they can change.
_______ is a built-in Python function that dynamically loads a module into the current namespace.
- import
- include
- load
- require
The import statement is used to dynamically load a module into the current Python namespace. It allows you to access functions, classes, and variables defined in the imported module. This dynamic loading is crucial for code modularity and reusability, enabling you to use code from different modules in your Python programs.
If you want to overload the less than (<) operator for a class, you will define the _______ method in your class.
- __compare__
- __less__
- __lessthan__
- __lt__
To overload the less than (<) operator for a class, you define the __lt__ (less than) method in your class. This method is called when the "<" operator is used with objects of the class.
What is the primary difference between a class attribute and an instance attribute in Python?
- Class attributes are private, while instance attributes are public.
- Class attributes are shared among all instances of a class, while instance attributes are specific to each instance.
- Class attributes are specific to each instance, while instance attributes are shared among all instances of a class.
- Class attributes can only be accessed within the class itself.
The primary difference is that class attributes are shared among all instances of a class, whereas instance attributes are specific to each instance. This affects how data is stored and accessed in Python classes.
What do you call a function defined inside another function in Python?
- Child function
- Inner function
- Nested function
- Sub function
A function defined inside another function in Python is called an 'Inner function' or 'Nested function.' They have access to the variables of the enclosing function.
Which keyword is used in Python to check a condition?
- check
- if
- then
- verify
In Python, the 'if' keyword is used to check a condition. It allows you to execute a block of code if the condition is true.
When working with large files, why might the readlines() method not be the most memory-efficient?
- It doesn't work with large files
- It lacks support for binary file formats
- It reads the entire file into memory
- It's not available in Python
The 'readlines()' method reads the entire file into memory, which can be inefficient for large files, as it can lead to memory consumption issues. It's better to use methods like 'readline()' or iterate over the file to process it line by line.
In a for-loop iterating through a list, if a specific condition is met and you want to skip the remaining code in the current iteration but not exit the loop, you would use the _______ statement.
- Break
- Continue
- Pass
- Skip
The 'continue' statement is used to skip the remaining code in the current iteration of a loop and move to the next iteration without exiting the loop. So, you would use 'Continue.'
For which of the following OS is Python NOT available by default?
- Android
- Linux
- Windows
- macOS
Python is available by default on Windows, macOS, and Linux. However, it is not available by default on Android operating systems.
What keyword is used to stop the execution of the current iteration and proceed to the next in a loop?
- break
- continue
- skip
- stop
The 'continue' keyword is used to stop the execution of the current iteration in a loop and proceed to the next iteration. It allows you to skip specific iterations.