Week 5 Tutorial
Week 5 Tutorial
1. What is a List?
o A list in Python is an ordered collection of items that can be of different types
(e.g., integers, strings, objects).
o Lists are mutable, meaning you can change their contents after creation.
2. Creating a List:
o You can create a list by placing items inside square brackets []
4. Modifying Lists:
o Lists are mutable, so you can update elements:
6. List Comprehensions:
o A concise way to create or modify lists:
squared_numbers = [x**2 for x in range(5)] # Output: [0, 1, 4, 9, 16]
1. What is a Dictionary?
o A dictionary in Python is a collection of key-value pairs. Each key must be
unique, but values can be duplicated.
o Unlike lists, dictionaries are unordered and accessed via keys, not indices.
2. Creating a Dictionary:
o Use curly braces {} to create a dictionary.
Create a dictionary called book with the following keys: title, author, and pages.
Perform the following operations:
o Add a new key-value pair for publisher.
o Update the number of pages.
o Print out the dictionary’s keys and values.