Python Operator Questions
Normal Questions (5)
- What is the difference between the `/` and `//` operators in Python? Give an example.
- Explain the behavior of the `%` operator in Python with both positive and negative
numbers.
- What will be the output of `print(5 | 3)` and `print(5 & 3)`? Explain how these bitwise
operations work.
- How does the `is` operator differ from the `==` operator in Python? Provide an example.
- What will be the output of `print(2 ** -3)`? Explain why.
Precedence Questions (10)
- Evaluate the following expression step by step:
```python
result = 10 + 3 * 2 ** 2
print(result)
```
- What will be the output of the following code? Explain the order of execution.
```python
print(10 - 4 + 2 * 3)
```
- Consider the following expression:
```python
print(20 / 5 * 2 + 3)
```
What is the output, and why?
- Compute the result of the expression:
```python
print(5 + 2 ** 3 * 3)
```
Explain how operator precedence affects the calculation.
- Predict and explain the output of:
```python
print(2 ** 3 ** 2)
```
- Evaluate the following and explain:
```python
print(4 * 2 // 3 + 5)
```
- What will be the output of the following expression?
```python
print(True and False or True)
```
- Explain how the following expression is evaluated:
```python
print(5 & 3 | 2)
```
- Evaluate the following and explain:
```python
print(6 + 3 > 8 and 2 < 5 or 4 == 4)
```
- Analyze the result of the following code:
```python
print(3 * (4 + 2) / 2)
```
Associative Rule Questions (5)
- Given the left-associative nature of the `-` operator, evaluate:
```python
print(10 - 5 - 2)
```
How would the result differ if the operation were right-associative?
- Explain why the following two expressions give different results:
```python
print(8 / 2 / 2)
print(8 / (2 / 2))
```
- Demonstrate how associativity affects the following bitwise operation:
```python
print(10 | 5 & 3)
```
- Given that exponentiation (`**`) is **right-associative**, evaluate:
```python
print(2 ** 3 ** 2)
```
Explain why it is not evaluated as `(2 ** 3) ** 2`.
- Compare the results of these two expressions and explain the difference:
```python
print((5 - 2) - 1)
print(5 - (2 - 1))
```