Printing Arrays

The contents of arrays can be printed using a loop.

1
2
3
4
# --- Display names and scores ---
for index in range(20):
    print(f"{name[index]} scored {score[index]}")
#

Index values start at 0, but people usually start counting at 1. If printing the index value, it can be adjusted (index+1) to make it more user friendly:

1
2
3
4
# --- Display names and scores ---
for index in range(len(name)):
    print(f"Student {index+1} = {name[index]} scored {score[index]}")
#

Printing Tables/Columns

Specifying field widths will give need tables/columns of data:

1
2
3
4
5
# --- Display names and scores ---
print("No.  Name               Score")
for index in range(len(name)):
    print(f"{index+1:3} {name[index]:20} {score[index]:5}")
#