Post

Study Guide Module 2

Study Guide Module 2 Graded Quiz

Study Guide Module 2

🧠 Knowledge Checklist

βœ… Variables & Expressions

  • Assign values to variables using =
  • Use arithmetic operators: +, -, *, /, //, %, **
  • Predict the output of expressions

βœ… Functions

  • Define a function using def
  • Pass arguments (parameters) to functions
  • Use return to send values back
  • Call functions inside print() or other functions
1
2
3
4
def add(a, b):
    return a + b

print(add(3, 4))  # Output: 7

βœ… Conditionals

  • Use if, elif, and else to control program flow
  • Each if/elif must end with a colon (:)
  • Code under each block must be indented
  • Use comparison operators to test values

πŸ”Έ Comparison Operators:

Symbol Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 6 > 2 True
< Less than 1 < 5 True
>= Greater or equal 7 >= 7 True
<= Less or equal 3 <= 8 True

πŸ”Έ Logical Operators:

Operator Meaning Example Result
and True if both are true 5 > 3 and 6 < 10 True
or True if either is true 5 > 10 or 6 < 10 True
not Inverts a Boolean not 5 > 10 True

πŸ”Έ Alphabetizing Strings

  • Uses Unicode values for comparison
  • "a" < "b" β†’ True
  • "Z" < "a" β†’ True (because uppercase letters have smaller Unicode)

πŸ”Έ Floor Division // and Modulo %

Operator Meaning Example Result
// Floor division (drops decimal) 7 // 2 3
% Modulo (gives the remainder) 7 % 2 1

βœ… Complex Conditionals in if-elif-else

You can use multiple conditions with logical operators:

1
2
3
4
5
6
if x > 5 and x < 10:
    print("Between 5 and 10")
elif x == 5 or x == 10:
    print("Exactly 5 or 10")
else:
    print("Outside range")

πŸ›  Coding Skills

πŸ§ͺ Skill Group 1 – Functions + Conditionals

  • Use def to define a function
  • Use comparison operators to check conditions
  • Use return to send back a result
1
2
3
4
5
6
7
def task_reminder(time_as_string):
    if time_as_string == "08:00":
        return "Time to eat!"
    elif time_as_string == "12:00":
        return "Time for lunch!"
    else:
        return "Keep working."

πŸ§ͺ Skill Group 2 – Predict Output

  • Understand what functions return
  • Understand how nested functions evaluate
1
2
3
4
5
def product(a, b):
    return a * b

print(product(product(2,4), product(3,5)))
# 2*4 = 8, 3*5 = 15, then 8*15 = 120

πŸ§ͺ Skill Group 3 – Complex Conditions

1
2
3
4
5
6
7
8
def get_remainder(x, y):
    if x == 0 or y == 0 or x == y:
        remainder = 0
    else:
        remainder = (x % y) / y
    return remainder

print(get_remainder(10, 3))  # 10 % 3 = 1 β†’ 1 / 3 = 0.333...

⚠️ Syntax Reminders

Common Mistakes to Avoid:

  • ❌ Misspelled keywords: (retrun, fucntion, etc.)
  • ❌ Missing or wrong colons: (if x == 5 ← needs :)
  • ❌ Wrong indentation
  • ❌ Wrong quote types (β€˜smart quotes’ instead of ' or ")
  • ❌ Case sensitivity (Print vs print)
  • ❌ Using parentheses or brackets incorrectly

βœ… Best Practices:

  • Use meaningful variable names
  • Add comments for clarity
  • Keep code readable and consistent
  • Write self-documenting code: code that explains itself without extra comments

This post is licensed under CC BY 4.0 by the author.