Coding 101
Lesson 4: Lists
Objectives
Understand the data structures of:
And also learn about:
Lists and Arrays
Arrays = data structure that can hold a collection of values. It is a static data structure, which means that it has a fixed size. The size is defined when the array is created.
array1[5] # indicates that there are 5 slots for data to be saved
Lists = We will come back to lists in the future!
In Python, Lists and arrays are the same thing and differentiating the 2 is not really required at this point.
We also do not need to do the above in Python. Know it for pseudocode!
Working with arrays.
A basic list:
fav_books = [‘The Martian’, ‘Spot goes to school’, ‘The Gruffalo’]
Item 1 is fav_books[0]�print(fav_books[0]) → The Martian
To reassign an item:
fav_books[2] = “The BFG”
The len function
When we use the len function with a string it tells us how many chars there are.
With a list, it lets us know how many items there are.
fav_books = [‘The Martian’, ‘Spot goes to school’, ‘The Gruffalo’]
print(len(fav_books)) # → 3
in / not in
languages = ["Java", "C++", "Go", "Python", "JavaScript"]
if "Python" in languages:
print("Python is there!")
if 6 not in [1, 2, 3, 7]:
print("number 6 is not present")
Task 1
Create a loop that will cycle through each item in a list and state how many characters long each item is.
The list is
computer_brands = ["Apple", "Asus", "Dell", "Samsung"]
You can use the len() function. Google if help is needed!
Lists Are Mutable
A list in Python is mutable, meaning its elements can be changed after it's created. This allows you to add, remove, or modify elements within the list.
fruit = ["apple", "banana", "orange"]
# Modify the second element in the list
fruit[1] = "grape"
print(fruit) # Output → ['apple', 'grape', 'orange']
Adding items to a list
my_list = [1]
my_list.append("corned beef")
print(my_list) # –> [1, 'corned beef']
Remove items
This can be done in 2 ways.
fav_books = [‘The Martian’, ‘Spot goes to school’, ‘The Gruffalo’]
fav_books.remove(“The Gruffalo”)
Or
fav_books.pop(2)
Sorting a list
numbers = [8, 1, 6, 5, 10]
numbers.sort()
print(f"numbers: {numbers}") # → [1,5,6,8,10]
Sorted list?
While list.sort() sorts the list in-place, sorted(list) returns a new list and leaves the original untouched:
�numbers = [8, 1, 6, 5, 10]
sorted_numbers = sorted(numbers)
print(f"{numbers=}, {sorted_numbers=}")
Copying a list
Copy the original
Make a copy of the original
original = [1, 2, 3]
modified = original
modified[0] = 99
print(f"original: {original}, modified: {modified}")
> original: [99,2,3], modified: [99, 2, 3]
This way of copying allows you to edit the original list!!!!
original = [1, 2, 3]
modified = list(original) # Note list()
# Alternatively, you can use copy method
# modified = original.copy()
modified[0] = 99
print(f"original: {original}, modified: {modified}")��> original: [1,2,3], modified: [99, 2, 3]
1 | original |
2 | |
3 | |
RAM
1 | original |
2 | modified |
3 | |
RAM
original = [1, 2, 3]
modified = original
Both original and modified point to the same place in memory
original = [1, 2, 3]
modified = original
Modified now has it’s own place in memory
Extending a list
first = [1, 3, 2]
second = [4, 5]
first += second # same as: first = first + second
print(f"{first}") # → [1, 3, 2, 4, 5]
Reverse() vs sort(reversed=True)
Don’t confuse these 2 methods!
my_list = ["c", "o", "d", "e","f","e","z"]
my_list.reverse()
print(my_list) # → ['z', 'e', 'f', 'e', 'd', 'o', 'c']
my_list = ["c", "o", "d", "e","f","e","z"]
my_list.sort(reverse=True)
print(my_list) # → ['z', 'o', 'f', 'e', 'e', 'd', 'c']
Can you see the difference?
Tasks
The following slides have tasks.
They are part of a notebook here (with more tasks).
You can complete these using Google Colab or myBinder
Extension tasks - Try my challenges on codewars. (you need to be signed ni)
Task 2
The following task uses lists.
Try to create this program using the pseudocode guide.
Task 3
In a calendar year, it is exactly 365.25 days. But, eventually, this will lead to confusion because humans normally count by exact divisibility of 1 and not with decimal points.
To avoid the latter, it was decided to add up all 0.25 days every four-year cycle, make that year to sum up to 366 days (including February 29 as an intercalary day), thus, called a leap year and aside the other years of the four-year cycle to sum up to 365 days, not a leap year.
Write a program which asks a user for the year and returns whether it is a leap year or not.
You are not allowed to use the datetime function
EXTRA brownie points for not using an if statement!
RULES OF A LEAP YEAR Leap Year (timeanddate.com)
(Borrowed from edabit)