Here are our solutions for the day 4 exercises in the 30 Days of Python series. Make sure you try the exercises yourself before checking out the solutions!

1) Create a movies list containing a single tuple. The tuple should contain a movie title, the director's name, the release year of the movie, and the movie's budget.

Let's start by making an empty list and assigning it to the name movies. We can do this using a pair of empty square brackets like so:

movies = []

Inside this movies list, we need to add a tuple. Because we're inside a list, we need to remember to use parentheses here to mark our tuple as a single value.

I'm going to use The Room as my starting movie. It doesn't really matter what movie you choose. We just need some data to work with.

movies = [
	("The Room", "Tommy Wiseau", "2003", "$6,000,000")
]

2) Use the input function to gather information about another movie. You need a title, director's name, release year, and budget.

To make things simple, we're going to use four separate input calls to get the movie information here. We've done this a few times now, so I don't think we need to linger on how to do this.

movies = [
	("The Room", "Tommy Wiseau", "2003", "$6,000,000")
]

title = input("Title: ")
director = input("Director: ")
year = input("Year of release: ")
budget = input("Budget: ")

3) Create a new tuple from the values you gathered using input. Make sure they're in the same order as the tuple you wrote in the movies list.

All we need to create a tuple is a series of comma separated values. If you want you can wrap these values in parentheses to make it clear that this is a tuple:

movies = [
	("The Room", "Tommy Wiseau", "2003", "$6,000,000")
]

title = input("Title: ")
director = input("Director: ")
year = input("Year of release: ")
budget = input("Budget: ")

new_movie = title, director, year, budget

I've assigned my new tuple to a variable called new_movie. Just make sure it's something descriptive.

4) Use an f-string to print the movie name and release year by accessing your new movie tuple.

When using an f-string, we can evaluate expressions directly inside another string, as long as we put them inside curly braces. This includes referencing variables, or using subscription expressions to access elements in a collection.

title = input("Title: ")
director = input("Director: ")
year = input("Year of release: ")
budget = input("Budget: ")

new_movie = title, director, year, budget

print(f"{new_movie[0]} ({new_movie[2]})")

Here I've chosen to print my movie data out in the following format:

The Room (2003)

If you want to go for something different, that's totally fine.

Don't forget that the indices for sequences start at 0. If we want the first item we need to access the item at index 0, not index 1.

Because we want the first and third item, we need the items at the indices 0 and 2.

This may trip you up for a little while, but very soon it will become second nature to you.

5) Add the new movie tuple to the movies collection using append.

Since we already created our new_movie variable, which refers to a tuple, this step is fairly straightforward. We just need to call append using the dot syntax, and we need to pass in new_movie when we call the method:

movies = [
	("The Room", "Tommy Wiseau", "2003", "$6,000,000")
]

title = input("Title: ")
director = input("Director: ")
year = input("Year of release: ")
budget = input("Budget: ")

new_movie = title, director, year, budget

print(f"{new_movie[0]} ({new_movie[2]})")

movies.append(new_movie)

If something is called a method, you're always going to be calling it in this same way in Python. There's something you're calling the method on, then a dot, then the method you want to call, followed by the usual parentheses.

Methods are functionality special to some type. For example, lists have an append method, while strings and tuples don't. print on the other hand is a function, and it's not associated with a given type.

6) Print both movies in the movies collection.

Here we just need to use subscription expressions again to access the items in movies. Don't forget the first item is at index 0!

movies = [
	("The Room", "Tommy Wiseau", "2003", "$6,000,000")
]

title = input("Title: ")
director = input("Director: ")
year = input("Year of release: ")
budget = input("Budget: ")

new_movie = title, director, year, budget

print(f"{new_movie[0]} ({new_movie[2]})")

movies.append(new_movie)

print(movies[0])
print(movies[1])

7) Remove the first movie from movies. Use any method you like.

We have a lot of options here.

First we could use del like so:

del movies[0]

Alternatively, we could use pop:

movies.pop(0)

pop is a method, so we need to use this dot syntax.

Alternatively we could use the remove method, passing in the item at index 0:

movies.remove(movies[0])

I think del is the cleanest option in this case, so my finished solution looks like this:

movies = [
	("The Room", "Tommy Wiseau", "2003", "$6,000,000")
]

title = input("Title: ")
director = input("Director: ")
year = input("Year of release: ")
budget = input("Budget: ")

new_movie = title, director, year, budget

print(f"{new_movie[0]} ({new_movie[2]})")

movies.append(new_movie)

print(movies[0])
print(movies[1])

del movies[0]

Hopefully you were able to complete all of these steps. If you're stuck on how something works, remember you can always get in touch with us on our Discord server.