Learning

Operators & Expressions

Operators & Expressions

Arithmetic Operators

python
a, b = 17, 5 print(a + b) # 22 Addition print(a - b) # 12 Subtraction print(a * b) # 85 Multiplication print(a / b) # 3.4 Division (ALWAYS float) print(a // b) # 3 Floor division print(a % b) # 2 Modulus (remainder) print(a ** b) # 1419857 Exponentiation # Division always returns float! print(10 / 2) # 5.0 (not 5) print(10 // 2) # 5 (use // for int division) # Negative floor division (tricky!) print(-7 // 2) # -4 (floors toward negative infinity) print(-7 % 3) # 2 (result has sign of divisor)

Comparison Operators

python
x, y = 10, 20 print(x == y) # False Equal print(x != y) # True Not equal print(x < y) # True Less than print(x > y) # False Greater than print(x <= y) # True Less or equal print(x >= y) # False Greater or equal # Chained comparisons (Pythonic!) age = 25 print(18 <= age <= 65) # True print(0 < x < 100 < y) # True # Equivalent to: (0 < x) and (x < 100) and (100 < y)

Logical Operators

python
a, b = True, False print(a and b) # False (both must be True) print(a or b) # True (at least one True) print(not a) # False (negation) # Short-circuit evaluation print(0 and print('never')) # 0 (print never runs!) print(1 or print('never')) # 1 (print never runs!) print(0 or print('runs')) # prints 'runs' then None # Returns the VALUE, not just True/False print(1 and 2) # 2 print(0 and 2) # 0 print(0 or 'hello') # 'hello' print('' or 'default') # 'default'

Assignment Operators

python
x = 10 x += 5 # x = x + 5 → 15 x -= 3 # x = x - 3 → 12 x *= 2 # x = x * 2 → 24 x /= 4 # x = x / 4 → 6.0 x //= 2 # x = x // 2 → 3.0 x **= 3 # x = x ** 3 → 27.0 x %= 5 # x = x % 5 → 2.0

Bitwise Operators

python
a, b = 12, 10 # 1100, 1010 print(a & b) # 8 (1000) AND print(a | b) # 14 (1110) OR print(a ^ b) # 6 (0110) XOR print(~a) # -13 NOT (two's complement) print(a << 2) # 48 (110000) Left shift print(a >> 1) # 6 (110) Right shift # Practical: check if number is even/odd print(12 & 1) # 0 → even print(13 & 1) # 1 → odd # Swap without temp a, b = 10, 20 a = a ^ b b = a ^ b a = a ^ b print(a, b) # 20, 10

Membership Operators

python
# in / not in — test containment print('h' in 'hello') # True print('x' not in 'hello') # True nums = [1, 2, 3, 4, 5] print(3 in nums) # True print(6 not in nums) # True print('name' in {'name': 'John', 'age': 25}) # True (checks keys)

Identity Operators

python
# is / is not — check if SAME object in memory a = [1, 2, 3] b = [1, 2, 3] c = a print(a == b) # True (same values) print(a is b) # False (different objects!) print(a is c) # True (same object) # Always use 'is' for None, True, False x = None print(x is None) # ✅ Correct print(x == None) # ⚠️ Works but not PEP 8 recommended # String interning a = 'hello' b = 'hello' print(a is b) # True (small strings are interned) a = 'a very long string that probably wont be interned' b = 'a very long string that probably wont be interned' print(a is b) # Might be False
Key Rules
  • •Division (/) ALWAYS returns float — use // for integer division
  • •Negative floor division floors toward negative infinity: -7 // 2 = -4 (not -3)
  • •Logical operators return VALUES, not booleans: 0 or 'hello' returns 'hello'
  • •Short-circuit evaluation: 'and' stops at first falsy, 'or' stops at first truthy
  • •Use 'is' for identity (None, True, False) — use '==' for value equality
  • •Chained comparisons (18 <= age <= 65) are evaluated correctly and Pythonically
Your Task

Create a function `classify_number(n)` that returns a dictionary with: 'even' (bool), 'positive' (bool), 'divisible_by_3' (bool), 'power_of_2' (bool), 'binary' (string of binary digits). Use bitwise & for even check, // and % for divisibility, and bit manipulation for power of 2 check (n & (n-1) == 0). Test with n=16, n=7, n=-4, n=0.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should define classify_number function
Should use bitwise & for even check
Should use n & (n-1) for power of 2
Should use modulo for divisibility check
Should handle edge case n=0
Should use bin() for binary conversion