Advanced Python Operator Lab Questions
1. Arithmetic Expression Challenge
Evaluate the following expression step-by-step considering operator precedence:
result = 25 - 4 * 3 ** 2 + 8 // 3 % 2
- Modify the expression using parentheses to change the output to exactly 10.
- Explain the role of each operator in the final output.
2. Bitwise Magic with Shift Operators
Given two numbers A = 57 and B = 13:
- Apply left shift (<<) on A by 3 bits.
- Apply right shift (>>) on B by 2 bits.
- Perform A & B and A | B after the shifts.
- Explain the binary representation changes after each operation.
3. Membership Logic Puzzle (Without 'in' Keyword)
Given the list:
data = [10, 20, 30, 40, 50, 60]
- Write a program to check if 35 exists in the list without using 'in' or 'not in'.
- Use only loops and comparison operators.
4. Nested Ternary for Conditional Assignment
Given three numbers a, b, c, write a one-liner using nested ternary operators to determine:
- Which number is the second largest.
Example:
a = 14, b = 9, c = 22
# Output: Second Largest = 14
5. Logical Operator Trap
Without using if statements, evaluate the following conditions using logical operators:
- Given x = 5, y = 15, z = 10
- Check if either (x + y > z) AND (z * 2 < y) OR (x - z == y).
- Print 'Condition Met' if true, otherwise print 'Condition Failed'.
6. XOR-Based Swapping Puzzle
Given two numbers A = 45 and B = 78:
- Swap their values using only XOR (^) operator without using a temporary variable.
- Verify the swap by printing the values before and after.
7. Assignment Operator Chain Reaction
Given the number n = 500:
- Apply a sequence of compound assignment operators (+=, -=, *=, /=, %=, **=) to transform
n into 1 in exactly 8 steps.
- You cannot use loops or functions—only assignment operators.
8. Identity Mystery
Consider the following code:
a = [4, 5, 6]
b = [4, 5, 6]
c=a
- Print the results of (a is b), (a == b), and (a is c).
- Modify the code to make (a is b) return True without directly assigning 'c = a'.
9. Manual Expression Evaluation
Write a program to manually evaluate the following mathematical expression without using
eval():
'8 + 3 * 2 - 4 // 2 + 7 ** 2 % 5'
- Respect operator precedence while calculating step-by-step.
- Show intermediate results for each step.
10. Bitwise Lock Puzzle (Without Loops)
You are given a number n = 37:
- Reduce it to 1 using only bitwise operators (&, |, ^, ~, <<, >>).
- Each operation counts as one move.
- Minimize the number of moves required and explain your strategy.