Decisions II

CSD1010: Week 1B
Wiedemann • Fall 2025

elif (Else–If)

  • Combines an else and a new if into one statement.
  • Conditions are checked top-down, and only the first branch that is True is executed.
  • Improves readability and avoids redundant evaluation.
if hour < 12:
    greeting = "Good morning!"
elif hour < 17:
    greeting = "Good afternoon!"
elif hour < 21:
    greeting = "Good evening!"
else:
    greeting = "Good night!"

pass Placeholder

  • Every if/elif/else statement must be followed by a suite of code.
  • The pass keyword lets us write syntactically complete code before the logic is ready.
  • Useful for adding placeholders and sketching program structure.
choice = input("Which path do you want to take? (left or right) ")

if choice == "left":
    print("You walk down the left path and find a treasure chest!")
elif choice == "right":
    print("You walk down the right path and find a monster!")
else:
    pass  # TODO: Handle what happens if player makes an invalid choice

Delimiters & Escaping

  • Delimiter: Both ' and " define strings in Python (just be consistent).
  • Escape your chosen delimiter with \ if you want it to appear inside the string.
print('Hello, World!')
print("She said, \"Don't forget the homework!\"")

Long Strings

  • Improve readability by placing long strings in parentheses and breaking them across lines.
  • Python implicitly concatenates adjacent string literals inside parentheses.
  • Will print as one continuous line (no linebreaks).
print(
    "This is a really long sentence that we want to keep readable "
    "in the editor, but still print as one continuous line."
)

Multi-line Strings

  • Triple quotes (""" or ''') define strings which preserve line breaks.
  • \n inserts a newline in a normal string.
quote = """Long quotes for which you want to preserve linebreaks,
for whatever reason,
can be made with triple quotes."""
print(quote)

print("Line 1\nLine 2\nLine 3")

ASCII Art & Raw Strings

  • Prefix with r for a raw string, which does not treat \ as an escape character. Useful for ASCII art.
  • Store long strings in a separate file for readability.
# This string is stored in a file called art_collection.py
cat_art = r"""
    /\_/\
   ( o.o )
    > ^ <
"""
from art_collection import cat_art
print(cat_art)