MASTER YOUR LOGIC BUILDING:Phase 1 - Level 3: Math and Number Logic
Mastering phase 1 - level 3: math and number logic concepts and implementation.
Phase 1 - Level 3: Math and Number Logic
Why This Matters
When you dial a phone number, each digit matters — and you might check if they're all different, or if the number falls in a valid range. Programs work with numbers the same way: they pull apart digits, check properties, and make decisions based on math.
This level connects the if statements you already know with number tricks you'll use again and again.
What You'll Learn
- How to extract individual digits from a number
- How to compare digits inside a number
- How to check number properties (perfect square, distinct digits)
- How to think through a math problem before writing code
Working with Digits — Two Key Tools
Every multi-digit number can be broken apart:
| Operation | What it does | Example with 1234 |
|---|---|---|
| `num % 10` | Gets the **last** digit | `1234 % 10` → 4 |
| `num // 10` | Removes the **last** digit | `1234 // 10` → 123 |
Repeat these steps to process every digit in a number.
Extracting digits from a 3-digit number
For the number 482:
digit1 = num // 100 # 482 // 100 = 4 (hundreds place)
digit2 = (num // 10) % 10 # (482 // 10) % 10 = 48 % 10 = 8 (tens)
digit3 = num % 10 # 482 % 10 = 2 (ones place)
Step-by-step: First figure out which place each digit is in. Then use // and % to isolate it.
Putting Logic and Math Together
Are all digits different?
if digit1 != digit2 and digit2 != digit3 and digit1 != digit3:
print("All digits are distinct")
Think: compare every pair of digits. If any two match, they're not all distinct.
Is it a perfect square?
A perfect square is a number like 1, 4, 9, 16, 25 — the result of some integer multiplied by itself. Instead of using sqrt, you can try squaring 1, 2, 3… until you find a match or go too far.
i = 1
while i * i <= num:
if i * i == num:
print("Perfect square!")
break
i += 1
Common Mistakes
- Mixing up `//` and `%` —
//divides and drops the remainder;%gives just the remainder - Forgetting edge cases — what about 0? A single-digit number? A negative number?
- Comparing only some digit pairs — for 3 digits, you need all three comparisons
- Extracting digits from the wrong place — draw it out: 482 → hundreds=4, tens=8, ones=2
Tips
- Write the number on paper and label each digit before coding
- Test with easy numbers: 100 (repeated digits), 123 (all distinct), 16 (perfect square)
- Break the problem into steps: extract → compare → decide
Practice Focus
Work through the examples below step by step:
- Extract digits from a 3-digit number and check if they're all different
- Check whether a number is a perfect square without using
sqrt - Practice saying the logic in English before writing a single line of Python
Hands-on Examples
Check if All Digits are Distinct
# Take a 3-digit number
num = int(input("Enter a 3-digit number: "))
# Extract digits
digit1 = num // 100
digit2 = (num // 10) % 10
digit3 = num % 10
# Check if all distinct
if digit1 != digit2 and digit2 != digit3 and digit1 != digit3:
print("All digits are distinct")
else:
print("Digits are not all distinct")Extract each digit using integer division and modulo, then compare all pairs to ensure they are different.
Related Page
🔗Related Content
- 💻
Phase 1 - Practice Problems
Practice Phase 1 concepts with hands-on coding problems
- 📝
Phase 1 - Quiz
Test your Phase 1 understanding with assessment questions
- ➡️
Phase 2 - Get Started
Continue to Phase 2 after mastering Phase 1
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum