# Chapter 5: Decisions, Loops, and Iteration
# Fundamentals of Python for Financial Econometrics - Decisions, Loops, and Iteration
# Dataset: Ceteris Lab teaching sample

# Decisions, Loops, and Iteration
#
# **Opening question:** How can a script apply a rule repeatedly while remaining easy to inspect?

# %% Cell 2
returns = [0.02, -0.01, 0.0]
for r in returns:
    if r > 0:
        label = "gain"
    elif r < 0:
        label = "loss"
    else:
        label = "flat"
    print(label)

# **Interpretation check:** Interpretation. The zero case is handled explicitly rather than being absorbed into gain or loss.

# %% Cell 4
months = ["Jan", "Feb", "Mar"]
inflation = [2.9, 2.8, 2.7]
for position, (month, rate) in enumerate(zip(months, inflation), start=1):
    print(position, month, rate)

# **Interpretation check:** Interpretation. zip assumes the sequences align. In real data, joins based on keys are usually safer than positional alignment.

# %% Cell 6
print(label)
print(position, month, rate)

# Verified source output
#
# ```text
# gain loss flat
# ```
#
# ```text
# 1 Jan 2.9 2 Feb 2.8 3 Mar 2.7
# ```
#
# ```text
# gain
# loss
# flat
# ```
#
# ```text
# 1 Jan 2.9
# 2 Feb 2.8
# 3 Mar 2.7
# ```
