Lists
You have seen how data can be stored in individual variables. However, what happens when you have a large number of related data?
Consider a school. The teacher needs to store the exam results for all of the pupils in a class. It might look like this:
mathsSusan = 67
mathsJohn = 83
mathsSam = 35
englishSusan = 92
englishJohn = 60
englishSam = 80
This starts to get unwieldy even with a class of three pupils studying only two subjects. If the teacher wanted to print these values, it would get very tedious:
print(mathsSusan)
print(mathsJohn)
…
and so on.
Fortunately, Python enables us to group related data using lists:
classMembers = ["Susan", "John", "Sam"]
mathsScore = [67, 83, 35]
englishScore = [92, 60, 80]
print(classMembers[0], mathsScore[0], englishScore[0])
print(classMembers[1], mathsScore[1], englishScore[1])
print(classMembers[2], mathsScore[2], englishScore[2])
This gives the following output:
Susan 67 92
John 83 60
Sam 35 80
Note that Python uses zero indexing.
These topics, and other collection types, are further explored in the next video.
When you’re ready, select the vertical Learning Path button to continue.
Python for beginners - Data types, Strings and lists
A world-leading tech and digital skills organization, we help many of the world’s leading companies to build their tech and digital capabilities via our range of world-class training courses, reskilling bootcamps, work-based learning programs, and apprenticeships. We also create bespoke solutions, blending elements to meet specific client needs.