Objectives

Students will be able to:

  • understand that data of the same datatype can be stored in a data-structure such as a list.
  • process a data structure using a for loop

Review Code

def assignData():

global tempValues

tempValues = [0]*365

for i in range(365):

tempValues[i] = random.randint(10,30)

def displayData():

for i in range(365):

print(f"The value at index {i} is {tempValues{i}")

def main():

assignData()

displayData()

#call main

main()

Data structures

 

Scenario

An environmental scientist wants to store the average temperature for each day in 1 week in a computer program.

His first idea is to use 5 global variables to store each data sample:

Here is the .

Copy it into your IDE. Does it work as expected?

Too many variables :(

But now the scientist wants to analyse a temperature value for every day of the year?

That is 365 different values (and 366 in each leap year!).

It will be difficult to analyse 365 different variables.

For this kind of problem it is easier to create a collection.

In Python, we can use a collection known as a list.

Python Lists

Complete the worksheet, coding each activity as you progress.

Review

Let's populate tempValues:

0123456

The total is 0

Python code sample

Review Code Sample

is some review code to consider.

Tags

arrayauto-indent data structurecompile linear index fixed-length iterate listsyntax