You are writing a nested function, and you want to modify a variable from the outer function. However, you don't want this change to affect the global scope. How can you achieve this?
- Use the global keyword
- Use the local keyword
- Use the nonlocal keyword
- Use the private keyword
To modify a variable from an outer function without affecting the global scope, use the 'nonlocal' keyword. It allows you to access and modify the variable in the closest outer scope.
Which of the following statements is true about private attributes in a Python class?
- Private attributes are accessible and modifiable outside the class, just like public attributes.
- Private attributes are denoted by a double underscore prefix, such as "__private". They can be accessed and modified outside the class.
- Private attributes are denoted by a single underscore prefix, such as "_private". They are intended for internal use within the class.
- Private attributes are inaccessible from outside the class, making them more secure.
Private attributes in Python are typically denoted by a single underscore prefix (e.g., "_private"). While they can still be accessed from outside the class, the convention is to treat them as non-public and for internal use.
What happens when a function doesn't have a return statement?
- It raises an error
- It returns 0
- It returns None
- It returns a blank string
When a function lacks a return statement, it automatically returns None. This indicates that the function doesn't return a specific value.
Which of the following tuple methods returns the number of times a specified value appears in the tuple?
- count()
- find()
- index()
- length()
The count() method is used to determine how many times a specified value appears in a tuple. It's a useful tool for counting occurrences of elements in tuples.
To create a module in Python, you need to write your functions, classes, and variables in a _______ file.
- .module
- .pack
- .py
- .txt
To create a module in Python, you need to write your functions, classes, and variables in a '.py' file, which is a Python source code file.
Which of the following is a modulus operator in Python?
- %
- &
- *
- ~
The '%' symbol in Python is the modulus operator. It returns the remainder when the first operand is divided by the second operand. For example, 10 % 3 returns 1 because 10 divided by 3 leaves a remainder of 1.
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.
Which of the following syntaxes is used to define an empty dictionary in Python?
- empty_dict = ()
- empty_dict = []
- empty_dict = {}
- empty_dict = {}{}
The correct syntax to define an empty dictionary in Python is 'empty_dict = {}'. An empty dictionary consists of a pair of curly braces with nothing inside.
Which of the following best describes the difference between break and continue in Python loops?
- 'break' and 'continue' are functionally equivalent.
- 'break' and 'continue' are used for the same purpose.
- 'break' continues the loop, 'continue' ends the loop entirely.
- 'break' ends the loop entirely, 'continue' skips the current iteration and continues with the next.
In Python, the 'break' statement is used to exit the current loop entirely, while 'continue' skips the current iteration and proceeds to the next iteration.