1. What is the output of bool("False") in Python?
Answer:
Explanation:
In Python, the bool() function converts a value to a Boolean. Any non-empty string, including the string "False", is converted to True.
2. Which of the following statements correctly checks if a variable x is equal to True in Python?
Answer:
Explanation:
In Python, the idiomatic way to check if a variable is True is simply "if x:". This implicitly checks for truthiness.
3. What will be the output of the following code?
print(True + True)
Answer:
Explanation:
In Python, the Boolean values True and False are equivalent to 1 and 0, respectively. Therefore, True + True is equivalent to 1 + 1, which is 2.
4. What is the type of the result returned by a logical operator in Python?
Answer:
Explanation:
Logical operators in Python (and, or, not) return Boolean values (True or False).
5. What does the following expression return: not(False or True)?
Answer:
Explanation:
The expression inside the parentheses evaluates to True, and the 'not' operator negates it, resulting in False.
6. Which of the following is a valid Boolean expression in Python?
Answer:
Explanation:
In Python, "==" is the equality operator, so 1 == 1 is a valid Boolean expression that evaluates to True.
7. What will be the output of the following code?
x = 10
print(x > 5 and x < 15)
Answer:
Explanation:
The expression checks if x is greater than 5 and less than 15, which is True for x = 10.
8. How do you check if a list is empty in a Pythonic way?
Answer:
Explanation:
The most Pythonic way to check if a list is empty is "if not my_list:", which utilizes the truthiness of the list.
9. What is the result of the expression bool(None)?
Answer:
Explanation:
None is considered false in a Boolean context in Python.
10. What is the output of the following code?
y = 0
print(bool(y))
Answer:
Explanation:
The number 0 is considered False in Python, so bool(0) returns False.