Docsity

Kingston University Topic 4 – Data Structures: Thinking Like a Programmer | Revision Note, Lecture notes of Computer Science

Toggle sidebar
bg1
Topic 4 – Data Structures: Thinking Like a
Programmer (Python)
1. What Are Data Structures?
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]
2. Lists
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.
pf3
pf4
pf5

Partial preview of the text

Download Kingston University Topic 4 – Data Structures: Thinking Like a Programmer | Revision Note and more Lecture notes Computer Science in PDF only on Docsity!

Topic 4 – Data Structures: Thinking Like a

Programmer (Python)

1. What Are Data Structures?

 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 ]

2. Lists

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.

3. Strings

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.

4. Tuples

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.

5. Dictionaries

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}

8. Thinking Like a Programmer

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.

9. Key Takeaways

 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.