Stage 1 of 4, lesson 4 of 31. No coding experience needed
Lists and loops: work through many prices
Keep a run of prices in a list, pick values out of it, and repeat work over it. It introduces indexing, slices, for, while, range, enumerate and append.
Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.
You need: The lesson on if statements. No broker account.
Prices come in runs: a close for every day, a row for every trade. A list holds a run of values under one name, in order, and a loop does the same piece of work on each one.
Positions in a list count from zero, so the first close is closes[0]. A negative position counts back from the end, so closes[-1] is the last. A slice such as closes[:3] takes a part of the list.
There are two loops. A for loop visits every value and is the one you will use most. A while loop repeats until a test stops being true, which suits the question in the script: how many days passed before something happened? The closing prices are invented.
The code
# Lists and loops: keep many values under one name and do the same thing to each.
# No broker, no account, no internet. The closing prices are invented.
closes = [101.2, 102.8, 102.1, 103.9, 105.0, 104.4]
print("How many:", len(closes))
print("First:", closes[0], "Last:", closes[-1])
print("The first three:", closes[:3])
print("The last two:", closes[-2:])
# A for loop visits each value in turn.
highest = closes[0]
for close in closes:
if close > highest:
highest = close
print("Highest:", highest, "and max() agrees:", max(closes))
# range() gives positions, so each close can be compared with the one before it.
changes = []
for i in range(1, len(closes)):
changes.append(round(closes[i] - closes[i - 1], 2))
print("Day to day changes:", changes)
# enumerate() gives the position and the value together.
for day, change in enumerate(changes, start=2):
word = "up" if change > 0 else "down"
print("Day", day, word, abs(change))
# A while loop repeats until its test is False.
up_days = 0
position = 0
while position < len(changes) and changes[position] > 0:
up_days += 1
position += 1
print("Up days before the first fall:", up_days)
# A list can grow.
closes.append(106.3)
print("After append:", len(closes), "values, the last is", closes[-1])What you should see
How many: 6 First: 101.2 Last: 104.4 The first three: [101.2, 102.8, 102.1] The last two: [105.0, 104.4] Highest: 105.0 and max() agrees: 105.0 Day to day changes: [1.6, -0.7, 1.8, 1.1, -0.6] Day 2 up 1.6 Day 3 down 0.7 Day 4 up 1.8 Day 5 up 1.1 Day 6 down 0.6 Up days before the first fall: 1 After append: 7 values, the last is 106.3
Run with Python 3.12 on Windows. No network and no account.
Check yourself
Answer each question from the code above before you open it. The answers were written from the script on this page and what it printed.
The list holds six closes. How many day to day changes are there, and why?
Five. Each change needs a close and the one before it, and the first close has nothing before it.
Why does the loop that builds changes start range at 1 and not at 0?
With i at 0, closes[i - 1] would be closes[-1], which is the last close. Python would not complain. It would quietly compare the first day with the last.
Why does the while loop stop after one up day?
It runs while the change is above zero. The first change is 1.6, so it counts one. The second is -0.7, so the test is false and the loop ends.
Exercise
Add a for loop that counts the days on which the close fell, and print the count as Down days. Put it just before the line that appends a value.
What the changed script prints
How many: 6 First: 101.2 Last: 104.4 The first three: [101.2, 102.8, 102.1] The last two: [105.0, 104.4] Highest: 105.0 and max() agrees: 105.0 Day to day changes: [1.6, -0.7, 1.8, 1.1, -0.6] Day 2 up 1.6 Day 3 down 0.7 Day 4 up 1.8 Day 5 up 1.1 Day 6 down 0.6 Up days before the first fall: 1 Down days: 2 After append: 7 values, the last is 106.3
Captured from a run of the solution on . Run with Python 3.12 on Windows. No network and no account.
One solution
# Exercise solution: the lesson script with the one change the exercise asks for.
# Lists and loops: keep many values under one name and do the same thing to each.
# No broker, no account, no internet. The closing prices are invented.
closes = [101.2, 102.8, 102.1, 103.9, 105.0, 104.4]
print("How many:", len(closes))
print("First:", closes[0], "Last:", closes[-1])
print("The first three:", closes[:3])
print("The last two:", closes[-2:])
# A for loop visits each value in turn.
highest = closes[0]
for close in closes:
if close > highest:
highest = close
print("Highest:", highest, "and max() agrees:", max(closes))
# range() gives positions, so each close can be compared with the one before it.
changes = []
for i in range(1, len(closes)):
changes.append(round(closes[i] - closes[i - 1], 2))
print("Day to day changes:", changes)
# enumerate() gives the position and the value together.
for day, change in enumerate(changes, start=2):
word = "up" if change > 0 else "down"
print("Day", day, word, abs(change))
# A while loop repeats until its test is False.
up_days = 0
position = 0
while position < len(changes) and changes[position] > 0:
up_days += 1
position += 1
print("Up days before the first fall:", up_days)
down_days = 0
for change in changes:
if change < 0:
down_days += 1
print("Down days:", down_days)
# A list can grow.
closes.append(106.3)
print("After append:", len(closes), "values, the last is", closes[-1])Your progress
Your marks are kept in this browser only, in its local storage. They are not sent anywhere, there is no account, and they will not follow you to another browser or device. To remove them, use Clear my progress on the learning track page, or clear this site's data in your browser's settings. Marking needs JavaScript. With scripts off the track reads the same, without the ticks.
This browser refused to store anything for this page, so marks cannot be kept here.
Where this sits in the path
Stage 1 of 4: First steps. Values, decisions, loops and functions, and the habit of testing what you wrote. No files and no account.
- First Python scriptDone
- Variables and typesDone
- If and comparisonsDone
- Lists and loopsDoneYou are here
- Functions and sizingDone
- Test your functionsDone