In Python, a list is a mutable sequence of elements enclosed in square brackets. It can contain elements of different data types, such as numbers, strings, or even other lists. Lists are versatile and allow for dynamic modification, such as adding, removing, or changing elements. Here are a few examples of lists:
1. Empty list:
`empty_list = []`
2. List with elements:
`numbers = [1, 2, 3, 4, 5]`
3. List with mixed data types:
`mixed_list = [1, 'apple', True, 3.14]`
4. Accessing list elements:
`print(numbers[0]) # Output: 1`
5. Modifying list elements:
`numbers[0] = 10`
`print(numbers) # Output: [10, 2, 3, 4, 5]`
6. Adding elements to a list:
`numbers.append(6)`
`print(numbers) # Output: [10, 2, 3, 4, 5, 6]`
7. Removing elements from a list:
`numbers.remove(3)`
`print(numbers) # Output: [10, 2, 4, 5, 6]`
Lists are commonly used when you need to store and manipulate collections of data. They provide flexibility and functionality for various operations, such as sorting, searching, and iterating over elements.
# Tuples
In Python, a tuple is an immutable sequence of elements enclosed in parentheses. It can contain elements of different data types, such as numbers, strings, or even other tuples. Tuples are similar to lists, but unlike lists, they cannot be modified once created. Here are a few examples of tuples:
1. Empty tuple:
`empty_tuple = ()`
2. Tuple with single element:
`single_tuple = (42,)`
3. Tuple with multiple elements:
`fruits = ('apple', 'banana', 'orange')`
4. Accessing tuple elements:
`print(fruits[0]) # Output: 'apple'`
# Sets
In Python, a set is an unordered collection of unique elements. It is defined by enclosing the elements within curly braces `{}` or by using the `set()` function. Sets are useful when you want to store a collection of items without any duplicates and perform operations like union, intersection, and difference efficiently. Here are a few examples of sets:
1. Creating a set:
python
my_set = {1, 2, 3, 4, 5}
2. Adding elements to a set:
python
my_set.add(6)
my_set.update([7, 8, 9])
3. Removing elements from a set:
python
my_set.remove(3)
my_set.discard(4)
4. Set operations:
python
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2) # {1, 2, 3, 4, 5}
intersection_set = set1.intersection(set2) # {3}
difference_set = set1.difference(set2) # {1, 2}
5. Checking membership:
python
if 2 in my_set:
print("2 is in the set")
Sets are commonly used when you need to store a collection of unique elements and perform operations like checking membership, finding intersections, or removing duplicates efficiently.
Tuples and lists are existing abstractions that can help us structure data and apply generalised programming concepts in our code.
## 1.1
Use a tuple or a set to represent a delivery van which contains the name of the driver, the registration of the van and the number of packages it has to deliver. Make at least 4 concrete instances and then capture these in a list.
## 1.2
Write a function that simulates the delivery of one packages by subtracting one from the number of packages it contains. Use a for loop to apply this to every element in your list (created in section 1.1)
> Tip: you may need to re-evaluate your choices in task 1.2 in order to implement this function.
## 1.3
Abstract the concept from the work above into a class, creating a `DeliveryVan` class.
## 1.4
Make a member function called `deliver_package` that implements the logic above. Make some example instances of your class, store them in a list simulate the delivery of a packages for every instance using a for loop.
# Task 2 (Assessment work)
## 2.1
For the problem that you have been working on for your assessment, begin to build some abstractions to represent the entities from last weeks ERD. You may use classes and other containers as is appropriate.
## 2.2
Document your work in your `portfolio.md` including code snippets as examples in the abstraction and patterns section (approximately 150 words).
# Task 3
Based on your work from task 1, implement an code that implements the following features:
- if the drivers name is Ellis, assign them 100 packages
- if drivers name is Nathan assign them 20 packages
- Other wise, assign them 50 packages
- if the registration of a van is DF07 EXE then it should deliver 2 packages for each delivery
- if the registration of a van is DP08 SLO then it should deliver 1 package every other delivery
- Attempt to wrap the above in a loop that calls the deliver package function until all packages are delivered. You may add new functions to help with this.
> You may run into errors such as allowing the count of packages to become negative. If you reach this point, that is acceptable and you may save this solution for futures weeks (where we look at error handling)
# Task 4
Begin to implement an algorithm for your problem, focusing on using the ideas you have abstracted in task 2. You should ensure you are working with your tutor on this problem to get support.
> Tip: This will feel like a big step in your programming journey if you are new to programming. Focus on a small part of your decomposed problem and don't be afraid to continue refining all of the previous steps in order to address this task. By the time you are completing this section a substantial part of your report will be completed! Ensure you are spending time discussing your problem with your tutor in order to get the most out of the practical sessions.