Courses/Python Mastery/Module 2: The Building Blocks (Tokens & Syntax)
Module 2 ยท Lesson 720 minBeginner๐Ÿงช Practice Session

๐Ÿงช Practice: Spot the Tokens

Lesson goal
Identify keywords vs identifiers, legal vs illegal names, and fix broken snippets.

๐Ÿงช Practice: Spot the Tokens

Module 2 gave you the atoms: keywords, identifiers, literals, operators, delimiters โ€” plus comments, docstrings, and the indentation rule. Time to prove it. Attempt everything before peeking at the solutions.

Exercise 1 โ€” Token autopsy

List every token in this line, and name each one's family:

code
total = price + 50

Mark each identifier as legal or illegal. If illegal, say why:

code
1. my_score
2. 2nd_player
3. for
4. For
5. user-name
6. _temp
7. class
8. Class2

Exercise 3 โ€” Keyword or identifier?

code
True, true, while, While, none, None, is, is_not

Which are keywords?

Exercise 4 โ€” Comment or docstring?

Which of these does Python store for help() to display?

code
# A: keeps the app alive
""" B: Keeps the app alive """

def keep_alive():
    # C: keeps the app alive
    """ D: Keeps the app alive """
    pass

Exercise 5 โ€” The indentation detective

This code has three indentation problems. Find and fix them:

code
score = 75
if score >= 50:
print("Passed!")
  print("Well done")
else:
print("Failed")

Exercise 6 โ€” Predict the output

*Before* running, write down what this prints:

code
# count = 10
count = 5
print(count)

Solutions

Exercise 1: total (identifier) = (operator) price (identifier) + (operator) 50 (literal).

Exercise 2:

  • my_score โœ…
  • 2nd_player โŒ starts with a digit
  • for โŒ keyword
  • For โœ… โ€” case-sensitive, For โ‰  for
  • user-name โŒ hyphen reads as minus
  • _temp โœ… underscore is legal (and means "internal use" by convention)
  • class โŒ keyword
  • Class2 โœ… โ€” contains the keyword but isn't one
  • Exercise 3: True, while, None, is are keywords. true, While, none, is_not are identifiers โ€” keywords are case-sensitive.

    Exercise 4: Only D โ€” a docstring placed first inside the function is stored and shown by help(). A is a comment, B is a floating string at module level (stored as __doc__ of the module, but the classic answer: D is the function's docstring), C is a comment.

    Exercise 5:

    code
    score = 75
    if score >= 50:
        print("Passed!")
        print("Well done")
    else:
        print("Failed")

    Problems were: (1) print("Passed!") needed indenting under the if, (2) print("Well done") was indented 2 spaces โ€” must match the block's 4, (3) print("Failed") needed indenting under the else.

    Exercise 6: It prints 5. The first line is a comment โ€” Python never saw it. Comments are for humans; only real code runs.


    โœ… Checkpoint

    Six exercises done from your own keyboard? Module 2 complete. You now speak Python's *alphabet*: tokens, names, comments, and indentation.

    Next module: Variables & Data Types โ€” where your programs finally start remembering things.