Python For Loops

PythonPythonBeginner
Practice Now

Introduction

In a distant future where the climate has shifted and ice covers much of the planet's surface, you find yourself in the role of an Ice Age Chronicler. Your mission is to document the patterns in the ice, cataloging the varying thickness of the layers that reveal the history of the world's climate. To carry out this crucial task, you will need to harness the power of Python and the iteration capabilities provided by the for loop construct - your digital ice core drill. This will not only allow us to understand the past but also prepare us for the future by analyzing and predicting ice growth patterns. Can you rise to the occasion and help unlock the secrets frozen in time?


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python/ControlFlowGroup -.-> python/for_loops("`For Loops`") subgraph Lab Skills python/for_loops -.-> lab-271556{{"`Python For Loops`"}} end

Exploring Ice Thickness

In this step, you will learn how to use for loops to process a list of ice layer thickness measurements. Imagine you've been given a list that represents the thickness of ice at different layers in a core sample. Let's calculate the average thickness of these layers.

Before coding, make sure that you are in the working directory /home/labex/project.

Now, open ice_thickness.py and write the following Python code to calculate the average thickness.

## ice_thickness.py

## Sample data representing ice layer thicknesses in millimeters
ice_layers = [120, 150, 90, 200, 180, 75, 140]

## Initialize the total thickness to 0
total_thickness = 0

## Use a for loop to sum up the thicknesses
for layer in ice_layers:
    total_thickness += layer

## Calculate the average thickness
average_thickness = total_thickness / len(ice_layers)

## Print out the average thickness
print(f"The average ice layer thickness is {average_thickness} millimeters.")

To execute the code, run this command in your terminal:

python ice_thickness.py

You should see the average ice layer thickness printed as a result:

The average ice layer thickness is 136.42857142857142 millimeters.

Predicting Future Ice Growth

Continuing our journey as an ice chronicler, let's predict the growth of ice layers using a for loop.

Previously, you calculated the average thickness of ice layers. Now, use that data to project future ice growth. We will assume ice thickness increases by a certain percentage each year.

Next, write the following code to predict the ice thickness over the next five years in /home/labex/project/predict_growth.py:

## predict_growth.py

## Average thickness of ice layers (from Step 1)
average_thickness = 136.42857142857142  ## Replace with the value you calculated

## Prediction model assuming a 4% growth in ice thickness per year
growth_rate = 0.04

## Predict ice thickness over the next five years
for year in range(1, 6):
    future_thickness = average_thickness * ((1 + growth_rate) ** year)
    print(f"Year {year}: {future_thickness:.2f} millimeters")

Execute this code with the following command:

python ~/project/predict_growth.py

You will see the predicted thicknesses for each of the next five years:

Year 1: 141.89 millimeters
Year 2: 147.56 millimeters
Year 3: 153.46 millimeters
Year 4: 159.60 millimeters
Year 5: 165.99 millimeters

Summary

In this lab, we dove into the frozen world of Python for loops by becoming an Ice Age Chronicler. We began by processing real-world data representing ice layers to calculate the average thickness. With this foundational skill established, we then used Python's power to predict future changes in the ice, simulating growth patterns over time. By mastering the for loop, we not only simulated the past conditions but also glimpsed into the future, a methodology crucial for understanding climate change.

Other Python Tutorials you may like