As we know, Python has variables. Variables can store one piece of data, no matter what data type that is. It can store numbers, strings, booleans, and now, lists. However a variable which carries a list carries multiple values with it, as lists (just like how the name implies) contain multiple elements. They are formed with square brackets and the elements they contain are seperated by commas.
x = ["A", "B", "C", 1, 2, 3, True, False]
An important aspect of lists is the fact that they are indexible. This means that all the elemenst of a list have an index number, so a number which specifies where they are in the list. Indexing starts at 0, so they first element of the list has an index number of 0.
x = ["A", "B", "C", 1, 2, 3, True]
one = x[0]
two = x[6]
print(one)
print(two)
Output:
"A" # the first element of the list has an index number of 0
True # the seventh element of the list has an index number of 7
Just like how we can retrieve just one element from a list, we can retrieve many through slicing. First, we would write the list and then open square brackets. We'd first write where we want to start slicing, where we want to end (which doesn't include the element with that index number) and by how many steps we wish to proceed. The syntax is below:
list[start:end:step]
For example:
x = ["A", "B", "C", 1, 2, 3, True, False]
print(x[0:3])
print(x[0:4:2])
print(x[2:])
Output:
['A', 'B', 'C']
['A', 'C']
['C', 1, 2, 3, True, False]
Lists can be placed inside other lists and sliced accordingly:
our_list = [[1,2,3], [4,5,6], [7,8,9]]
print(our_list[1][2])
Output:
6
Lists can of course have elements added to them and removed from them. Removing an element from a list is as simple as using the .remove() method.
users = ["Alice", "Bob", "Charlie", "Dan"]
users.remove("Bob")
print(users)
Output:
users = ["Alice", "Charlie", "Dan"]
Another method for removing elements is by using the del keyword with an index number.
del users[0]
print(users)
Output:
users = ["Charlie", "Dan"]
As for adding elements to a list, there are several methods. The most popular method is using the .append() method, which adds a new element to the end of the list.
users.append("Noah")
print(users)
Output:
users = ["Charlie", "Dan", "Noah"]
Although this is a method used consistently to add new elements to lists, the following code includes some other common methods:
A = []
A = A + [1] # this adds 1 to the end of the list
A = A + ["BCD"]
print(A)
A = A + [5, 6, 7, 8] # adds all of the elements in here individually
print(A)
A = A + [[5, 6, 7, 8]] # adds [5, 6, 7, 8] as one element
print(A)
A.insert(2, 100) # inserts 100 as the second index item
print(A) # the five moves one space
A[0] = 2 # makes the 0 index as 2, gets rid of one
print(A)
Here is the official documentation made by Python regarding adding and removing items from lists.
Tuples are one of Python's immutable data types, just like stringsIn Python, an immutable data type is a data type which cannot be changed once it has been created. Although you may add a letter or a piece of text onto a string, you cannot randomize or shuffle it’s letters just like how you would shuffle the elements of a list. You cannot add an extra element to a tuple after creating it.
Tuples can be formed with our without parentheses, though using parentheses make it easier to understand that what you defined is a tuple.
the_tuple = 1, 2, 3 # tuples can be defined without parentheses
the_tuple2 = ("A", "B", "C")
Tuples being immutable is an important attribute of them that can get overlooked. Since tuples are immutable and cannot be changed once assigned, they can be used in programmes where data needs to be protected and cannot be accidentally changed.
At the mall but don't know where to shop, what to eat or what to see at the cinema? You can make an incredibly easy and sweet solution to this using very few lines of code. You are at a mall with 6 of your favourite shops, restaurants and the mall cinema is showing 6 different unique movies. Your job is to write the code which will randomly select one of these 6 shops, restaurants and movies for you to have fun with your friends. Once you randomly select these 3 elements (the shop, restaurant and movie), display them all in a sentence for you and all of your friends to see. You can play around with and adjust the code once you write the correct code in this circumstance.
Step 1: Import the random module
Step 2: Make 3 separate lists of 6 shops, 6 restaurants and 6 movies.
Step 3: Index a random shop and store it in a variable.
Step 4: Index a random restaurant and store it in a variable.
Step 5: Index a random movie and store it in a variable.
Step 6: Display all of them in one sentence using formatted strings.
This is only a suggested answer
import random
shops = ["Phoebe's Guitars", "Zara", "H&M", "Monica's Cutlery", "UNIQLO", "Berhska"]
restaurants = ["Joey's Pizza", "Monica's Restaurant", "Dominos", "KFC", "McDonalds", "Subway"]
movies = ["Joey's Big Break", "Scream", "The Purge", "Friends The Movie", "Annabelle", "Chucky"]
shops_random = random.randint(0, 5)
random_shop = shops[shops_random]
restaurants_random = random.randint(0, 5)
random_restaurant = restaurants[restaurants_random]
movies_random = random.randint(0, 5)
random_movie = movies[movies_random]
print(f"Shop: {random_shop}, restaurant: {random_restaurant}, movie: {random_movie}")
You have reached the end of Section 5: Lists and Tuples!