SYNTAX TO SOLUTIONS // DAY 2
SCORE: 0 / 200
PYTHON STUDY JAM DAY 02 / 02 // ADVANCED LOGIC & STRUCTURE

LOGIC
UNLOCKED

Mastering Control Flow, Loops, Functional Decomposition, and the mind-bending mechanics of RECURSION. Includes hands-on workouts and interactive project builds.

snake_simulator.py ● RUNNING
CLICK + USE ARROWS SCORE: 0
DAY 02 ROADMAP

WHAT WE'LL COVER

06 MODULES · 04 QUIZZES · 03 PROJECTS · 200 PTS

USE ← → ARROW KEYS OR THE BUTTONS BELOW TO NAVIGATE

CONCEPT 01

DECISION MAKING: IF-ELSE & NESTED LOGIC

Programs need to make choices based on data. Python uses if, elif, and else keywords to construct decision paths.

Decision Tree Architecture
1. Condition True / False
2. Block Runs if True
3. Fallback elif / else

Short-Circuit Evaluation:

• In A and B, if A is False, B is never evaluated.

• In A or B, if A is True, B is skipped immediately.

// nested_decision.py PYTHON 3.11
age = 19
has_license = True

if age >= 18:
    # Nested condition inside primary block
    if has_license:
        print("Eligible to drive vehicle.")
    else:
        print("Age valid, but driver's license required!")
else:
    print("Underage. Driving restricted.")
Output: Eligible to drive vehicle.
QUIZ 01

DECISION LOGIC CHECK

+50 PTS

What will be the output of the following code snippet?

x = 10
if x > 15:
    print("A")
elif x > 5 and x < 12:
    print("B")
else:
    print("C")
CONCEPT 02

LOOPS & REPETITION MECHANICS

Loops allow executing a block of code repeatedly until a condition changes or a sequence is exhausted.

// FOR LOOP

Best for iterating over known sequences or predefined range limits.

for i in range(1, 5, 1): range(start, stop, step)
// WHILE LOOP

Executes indefinitely as long as condition evaluates to True.

while count < 5: Requires manual state updates!
CONTROL KEYWORDS:

• break: Immediately terminates loop execution.

• continue: Skips current iteration & moves to next.

• pass: Placeholder statement (does nothing).

// pattern_nested.py STAR PATTERN
rows = 4

for i in range(1, rows + 1):
    for j in range(i):
        print("*", end=" ")
    print() # Newline after inner loop completion
Output:
*
* *
* * *
* * * *
QUIZ 02

RANGE & LOOP PREDICTION

+50 PTS

What is printed by: list(range(2, 10, 3))?

CONCEPT 03

FUNCTIONS & VARIABLE SCOPE

Functions are reusable blocks of code defined using def. They reduce duplication and build modular architecture.

KEY FUNCTION CONCEPTS:

• Parameters vs Arguments: Placeholders in definition vs actual values passed during invocation.

• Return Value: Sends data back to caller. Without an explicit return, functions yield None.

• Scope Rules (LEGB): Local -> Enclosing -> Global -> Built-in.

*args & **kwargs:
*args accepts dynamic positional arguments as a tuple.
**kwargs accepts dynamic keyword arguments as a dictionary.
// function_demo.py SCOPES & ARGS
tax_rate = 0.18 # Global Variable

def calculate_total(price, discount=0):
    # Local variable calculation
    final_price = (price - discount) * (1 + tax_rate)
    return round(final_price, 2)

total = calculate_total(100, discount=10)
print(f"Final Amount: ${total}")
Output: Final Amount: $106.2
QUIZ 03

FUNCTION SCOPE

+50 PTS

What happens when attempting to print a local variable outside its function?

CRITICAL ⚠️

RECURSION: SELF-REFERENTIAL LOGIC

"To understand recursion, you must first understand recursion."

Recursion is a technique where a function calls itself to break down a problem into smaller instances.

THE 2 MANDATORY RULES OF RECURSION:
1. BASE CASE (Termination) Condition that stops the function from calling itself. Without this, you trigger a RecursionError: maximum recursion depth exceeded.
2. RECURSIVE STEP (Reduction) The function call moving closer toward the Base Case.
// CALL STACK SIMULATION: factorial(3) LIFO STACK
factorial(3) → 3 * factorial(2) [WAITING]
factorial(2) → 2 * factorial(1) [WAITING]
factorial(1) → Returns 1 [BASE CASE HIT!]
Unwinding Stack: 1 × 2 × 3 = 6
CONCEPT 04

RECURSION IN PRACTICE

// 1. Factorial Recursion
def factorial(n):
    # Base Case
    if n == 1:
        return 1
    # Recursive Step
    return n * factorial(n - 1)
// 2. Fibonacci Recursion
def fibonacci(n):
    # Base Cases
    if n <= 0: return 0
    if n == 1: return 1
    # Double Recursive Step
    return fibonacci(n - 1) + fibonacci(n - 2)
QUIZ 04: RECURSION PITFALLS +50 PTS

What error occurs if a recursive function lacks a base case?

MODULE 05

PROBLEMS WORKOUT & ALGORITHMS

#01 PRIME NUMBER CHECKER
#02 PALINDROME CHECKER (STRINGS / NUMBERS)
MODULE 06

DAY 2 MINI-PROJECT BUILD SESSIONS

PLAYABLE IMPLEMENTATION

Interactive Python Calculator Logic

Executes arithmetic operations using modular functional dispatching.

0
// calculator.py Source Logic
def add(a, b): return a + b
def subtract(a, b): return a - b
def multiply(a, b): return a * b
def divide(a, b): 
    return a / b if b != 0 else "Error: Division by Zero"

operations = {'+': add, '-': subtract, '*': multiply, '/': divide}

num1 = float(input("First number: "))
op = input("Operator (+,-,*,/): ")
num2 = float(input("Second number: "))

if op in operations:
    result = operations[op](num1, num2)
    print(f"Result: {result}")
SESSION COMPLETE

KEEP CODING

You've covered decisions, loops, functions and recursion. Now build the three projects, solve the workout problems, and push your code to GitHub.

0 PTS EARNED
06 MODULES
03 PROJECTS
100%