python quiz Flashcards
Which one is NOT a legal variable name?
Myvar
my_var
my-var
_myvar
my-var
How do you create a variable with the numeric value 5?
Both the other answers are correct
x = 5
x = int(5)
Both the other answers are correct
What is a correct syntax to return the first character in a string?
x = “Hello”.sub(0, 1)
x = “Hello”[0]
x = sub(“Hello”, 0, 1)
x = “Hello”[0]
Which method can be used to remove any whitespace from both the beginning and the end of a string?
trim()
ptrim()
len()
strip()
strip()
Which method can be used to replace parts of a string?
switch()
replace()
replaceString()
repl()
replace()
Which collection does not allow duplicate members?
TUPLE
SET
LIST
SET
How do you start writing a while loop in Python?
while x > y {
while (x > y)
x > y while {
while x > y:
while x > y:
for x in y:
for each x in y:
for x > y:
for x in y:
Consider the following function definition:
def hello_name(name: str) -> str:
return f”Hello {name}”
What does the -> str part of the function definition indicate?
The function is expected to return a string.
Where are docstrings stored within a Python object?
In the .__doc__ property
What are the three major categories of docstrings in Python?
Class, method, and function docstrings
Package and module docstrings
Script docstrings
What are the recommended parts to include in the documentation of a private project?
A README and an examples.py file
What’s a good approach to structuring the docs/ folder of a public or open-source Python project?
Tutorials
How-To Guides
References
Explanations
How would you express the hexadecimal value a5 as a base-16 integer constant in Python?
0xa5
How would you express the constant floating-point value 3.2 × 10-12 in Python:
3.2e-12
Which of the following are valid ways to specify the string literal foo’bar in Python:
”"”foo’bar”””
‘foo'bar’
“foo’bar”
Which of the following is not a Python built-in function:
diff()
repr()
isinstance()
round()
map()
diff()
What’s the primary purpose of decorators in Python?
To modify the behavior of a function
In Python, what does it mean that functions are considered first-class objects?
You can pass functions around and use them as arguments.
You can treat functions like any other object.
You can assign functions to variables.
Which of the following statements about functions in Python is true?
Functions in Python may have side effects.
The print() function is an example of a function with side effects.
Functions in Python may have side effects, meaning they can perform actions other than just returning a value based on the given arguments. The print() function is an example of a function with side effects, as it outputs something to the console while returning None.
What are the characteristics of inner functions in Python?
-Python only defines them when you call the parent function.
-You define them inside other functions.
-They’re locally scoped to the parent function.
Inner functions are defined inside other functions and are locally scoped to the parent function. This means they only exist inside the parent function as local variables, and you can’t access them outside the parent function.
The order in which you define them doesn’t affect their execution. They only get defined when you call their parent function.
If you want to diver deeper, then you can read more about what inner functions are good for.
def reference():
def inner():
return 42
return inner
def evaluation():
def inner():
return 42
return inner()
referenced = reference()
evaluated = evaluation()
Which statements about the the variables referenced and evaluated are true?
The variable evaluated will be the integer 42.
The variable referenced will be a reference to a function object.
What’s the purpose of the @ symbol when using decorators?
It simplifies applying a decorator to a function.
You’ve decorated a function that accepts an argument, but when running it, you get an error:
TypeError: wrapper() takes 0 positional arguments but 1 was given
What might be a good approach to try to fix this error?
Using *args and **kwargs in the inner wrapper function
To avoid this error when decorating a function that accepts arguments, use *args and **kwargs in the inner wrapper function. This allows the inner function to accept any number of positional and keyword arguments and then pass them on to the function that it decorates.
What happens to the return value of a decorated function if the wrapper function doesn’t explicitly return a value?
The decorated function will return none
What’s the purpose of using the @functools.wraps decorator in a decorator function?
To preserve the original function’s metadata
You’ll use the @functools.wraps decorator in a decorator function to preserve the original function’s metadata, such as its name and documentation. This helps maintain the original function’s identity after decoration.
Is it possible to define a decorator without an inner function?
yes .
You can create a decorator that takes a function as an argument and only accesses one of its attributes. The code logic in your decorator might save the attribute to another data structure. Then, the decorator returns the function object unmodified.