



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




A way of organising and storing data so a program can use it effectively. Different problems require different ways of storing information. Example 1: name = "Alex" age = 20 marks = [ 65 , 72 , 81 ]
A list stores multiple values in a specific order. fruits = ["apple", "banana", "orange"] Access an item using its index: print(fruits[ 0 ]) Output: apple Add an item: fruits.append("mango") Remove an item: fruits.remove("banana") Think: Use a list when you have a collection of related items that may change.
A string stores text and can also be treated as a sequence of characters. name = "Kingston" Access a character: print(name[ 0 ]) Output: K Find the length: print(len(name)) Output: 8 Think: Strings are useful for names, messages, addresses and other text.
A tuple stores multiple values that should generally remain unchanged. location = (51.5, -0.1) Access a value: print(location[ 0 ]) Output:
Think: Use a tuple when the collection represents fixed information.
A dictionary stores information using key-value pairs.
String Text "Hello" Tuple Fixed collection (10, 20) Dictionary Key-value information {"name": "Alex"} Set Unique values {1, 2, 3}
Before choosing a data structure, ask: What data am I storing? Does the data need to change? Do I need to access items by position? Do I need to find information using a key? Do I need to prevent duplicates? Example 3: If storing student names: students = ["Alex", "Sam", "Jordan"] A list makes sense because there are multiple names. If storing information about one student: student = { "name": "Alex", "age": 20 , "course": "Computing" } A dictionary makes more sense because each piece of information has a key.
Data structures organise data. Lists → collections of items.
Strings → text. Tuples → fixed collections. Dictionaries → key-value information. Sets → unique values. The best data structure depends on how the program needs to use the data.