Python Operators
1. Explain the use-case of logical operators. Provide examples of logical operators.
Logical operators are used on boolean values (either True or False). These operators allow you to combine and manipulate boolean values.
Examples: and, or, not
2. Explain the use-case of membership operators. Provide examples of membership operators.
Membership operators are used to test whether a value or variable is found in a sequence or collection.
Example: in,not in
3. Explain the use-case of identity operators.Provide examples of identity operators.
Identity operators are used to compare the memory location (identity) of two objects.
Example: is, is not
4. Explain the operator and use case.
and operator is an logical operator and it returns True if both of its operands are true; otherwise, it returns False.
5. Explain the operator or use case.
or operator is an logical operator and it returns True if at least one of its operands are true; otherwise, it returns False.
6. Explain the operator not use case.
and operator is an logical operator and it returns the opposite boolean value of its operand. If the operand is True, not True returns False; if the operand is False, not False returns True.
7. Explain the operator in use case.
in operator is a membership operator and it checks if a value is present in a sequence, such as a list, tuple, string, or set. It returns True if the value is found, and False otherwise.
8. Explain the operator is use case.
is operator is an identity operator and it checks if two variables refer to the same object in memory. It returns True if the objects have the same identity, and False otherwise.
9. Given X or Y, how does python efficiently evaluates the expression? Explain your answer.
- If X is
truthy, return X, otherwise evaluates Y and returns it. - We only need one to be true to pass. That’s why if
XisTrue, the program should short-circuit and return X.
10. Given X and Y, how does python efficiently evaluates the expression? Explain your answer.
- If X is
falsy, return X, otherwise evaluates Y and returns it. - We need both to be true to pass. That’s why if
XisFalse, the program should short-circuit and return X.
11. Explain floor division operator //. What does 10 // 3 returns?
The floor division operator returns the whole number part of the result, rounded down to the nearest whole number.
10 // 3 = 3
12. What is a common use case for the floor division operator //?
13. Explain modulus operator %. What does 10 % 3 returns?
The modulus operator returns the remainder of the division of the left operand by the right operand.
10 % 3 = 1
14. What is a common use case for apply the modulus operator %?
Modulus operator is commonly used to check divisibility. For example, it can be used to check whether a number is odd or even.
10 % 2 = 0(even)11 % 2 = 1(odd)