Day 5 — Python Mastery: Lists, Tuples, and Loops 🐍
Welcome to our Python journey! 😊 In the previous lessons, we’ve covered essential Python concepts, and today, we’re going deeper into lists, loops, and numerical sequences. Let’s jump right in!!
Agenda 🚀
Today, we’ll be focusing on loops and numerical lists. Here’s what’s on our agenda:
- Looping Through an Entire List 📜
- Making Numerical Lists 📊
- Simple Statistics with Lists of Numbers 📈
- List Comprehensions: Condensing Workflows 🧹
- Working with Part of a List: Slicing & Dicing 🔪
- Copying Lists and Introducing Tuples 📦
Let’s explore these topics one by one in our Python adventure! 🐍
1. How to loop through an entire list?😊
1.1 Loop through an entire list:🔄
- Looping allows us to take the same action, or set of actions, with every item in a list. As a result, we will be able to work efficiently with lists of any length.
- We often need to run through all the entries in a list, performing the same task with each item, so whenever we want to do the same action with every item in a list, we can use Python’s for loop.
- Say we have a list of movies, and we want to go through that list and print out each name in the list. We could do this by retrieving each name from the list individually, but this approach could cause several problems. For one, doing this with a long list of movie names would be repetitive. Using a for loop avoids this issue by letting Python manage this internally.
- Let’s use a for loop to print out each name from the list of movies:
movies = ['Bajirao Mastani', 'Iron Man 1', 'Jack Reacher', 'Prestigious', 'RRR']
for movie in movies:
print(movie)
We begin by defining a list
- Then we define for loop: this line tells Python to pull a name/element from the list of Movies and associate it with the variable movie.
- Next, we tell Python to print the name that’s just been assigned to the variable movie.
- Python then repeats these last two lines, once for each name/element in the list.
- It might help to read this code: “For every movie in the list of movies, print the movie name.
1.2 A closer look at the looping
- Looping is important because it is one of the most common ways a computer automates repetitive tasks. For example, in a simple loop like we used in the earlier example, Python initially reads the first line of the loop:
for movie in movies:
- This line tells Python to retrieve the first value from the list of movies and associate it with the variable movie.
print(movie)
- Python then prints the current value of the variable movie.
- Once all the elements from the lists are accessed and there are no more values in the list, Python exits the loop and moves on to the next line in the program.
- When we are using the loops for the first time, it is important to note that the set of steps is repeated once for each item in the list.
- Also, when writing our own for loops, we can choose any name we want for a temporary variable that will be associated with each value in the list.
- Example:
for car in cars:
for employee in employees:
for user in users:
for a in teachers:
However, it’s helpful to choose a meaningful name that represents a single item from the list, so for teachers
loop we can use:
for teacher in teachers:
1.3 Doing More work within a for loop:💼
We can do just about anything with each item in a for loop.
movies = ['Bajirao Mastani', 'Iron Man 1', 'Jack Reacher', 'Prestigious', 'RRR']
for movie in movies:
print(f"{movie.title()}, was a good movie.")
- We can also write as many lines of code as we like in the for loop.
- Every indented line following the line for movie in movies is considered inside the loop, and each indented line is executed once for each value in the list.
- Therefore, we can do as much work as we like with each value in the list.
1.4 Doing something after a for loop:🏁
- What happens once a for loop has finished executing? Usually, we want to summarize a block of output or move on to other work that your program must accomplish.
- Any lines of code after the for loop that are not indented are executed once without repetition.
movies = ['Bajirao Mastani', 'Iron Man 1', 'Jack Reacher', 'Prestigious', 'RRR']
for movie in movies:
print(f"{movie.title()}, was a good movie.")
print("\nThese are some of good movies to watch.")
The first two lines are part of for loop and once we come out of indentation we print the next print line once.
2. Making the Numerical Lists🔢
- Many reasons exist to store a set of Numbers. For example, we will need to keep track of runs on every ball, and we might want to keep track of a bowler’s wickets as well. In data visualizations, we will almost always work with sets of numbers, such as temperatures, distances, population sizes, or latitude and longitude values, among other types of numerical sets.
- Lists are ideal for storing sets of numbers, and Python provides a variety of tools to help us work efficiently with lists of numbers.
2.1 Using the range() function: 🔢
- The
range()
function returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and stops before a specified number.
range(start, stop, step)
- Python’s
range()
function makes it easy to generate a series of numbers. For example, we can use therange()
function to print a series of numbers like the following:
for value in range(1, 10):
print(value)
- Although this code looks like it should print the numbers from 1 to 10, it doesn’t print the number 10.
range()
prints only the numbers 1 through 4. This is another result of the off-by-one behavior we will see often in programming languages. The range() functions cause Python to start counting at the first value we give it, and it stops when it reaches the second value we provide, The output never contains the end value, which would have been 10 in this case.- To print the numbers from 1 to 10, we would use range(1,11)
for value in range(1,11):
print(value)
- Let’s see different arguments that we can pass to the
range()
method:
print("First range(end) method:")
for value in range(5):
print(value)
print("Second range(start, end) method:")
for value in range(1,5):
print(value)
print("Third range(start, end, step) method:")
for value in range(1,5, 2):
print(value)
reversed() Variation🔃
- Probably the second most common problem is to go through the standard index numbers but in reverse order. The
reversed()
function takes in a linear collection and returns a reversed form of it. This works nicely withrange()
to go over the regular numbers in reverse order:
print("First range(end) method:")
for value in reversed(range(5)):
print(value)
print("Second range(start, end) method:")
for value in reversed(range(1, 5)):
print(value)
print("Third range(start, end, step) method:")
for value in reversed(range(1, 5, 2)):
print(value)
2.2 Using range() to make a list of numbers📊
- If we want to make a list of numbers, we can convert the results of
range()
directly into a list using thelist()
function. When we wraplist()
around a call torange()
function, the output will be a list of numbers.
numbers = list(range(10))
print(numbers)
- We can also use the range() method to tell Python to skip numbers in a given range. If we pass a third argument to range(), Python uses that value as a step size when generating numbers:
numbers = list(range(1,10))
print(numbers)
- We can create any set of numbers we want using the range() method, We can also use the
reversed()
method to create a reverse list:
print("\nFirst Numeric List with range(end) method:")
numbers = list(range(10))
print(numbers)
print("\nSecond Numeric List with range(start, end) method:")
numbers = list(range(1,10))
print(numbers)
print("\nThird Numeric List with range(start, end, step) method:")
numbers = list(range(1,10, 2))
print(numbers)
print("--------------------------------------------------------------------")
print("\nFirst Reversed Numeric List with range(end) method:")
numbers = list(reversed(range(10)))
print(numbers)
print("\nSecond Reversed Numeric List with range(start, end) method:")
numbers = list(reversed(range(1,10)))
print(numbers)
print("\nThird Reversed Numeric List with range(start, end, step) method:")
numbers = list(reversed(range(1,10, 2)))
print(numbers)
- Let us try one example to create a list of squares of each integer:
numbers = list(range(10))
squares = []
for number in numbers:
square = number ** 2
squares.append(square)
print(f"List of Numbers: {numbers}")
print(f"List of Squares: {squares}")
- Here, let’s try to explain what the above code does on each line:
numbers = list(range(10)) #Create list of numbers using range() method
squares = [] #Create an empty list with name squares
for number in numbers: #Loop through numbers lists and temporarily store element in number variable
square = number ** 2 #Calculate the Square of each element once
squares.append(square) #Append calculated square of a number to empty spares list
print(f"List of Numbers: {numbers}") #Print numbers from the list
print(f"List of Squares: {squares}") #Print squares of each element from the list.
Simple Statistics with a List of numbers:📈
- A few Python methods are helpful when working with lists of numbers. For example, we can easily find the minimum, maximum, and sum of a list of numbers:
numbers = list(range(1, 11))
print(f"Numbers Lists: {numbers}")
print(f"Min of Number: \t{min(numbers)}")
print(f"Max of Number: \t{max(numbers)}")
print(f"Sum of Number: \t{sum(numbers)}")
average = sum(numbers)/len(numbers)
print(f"Avg of Number: \t{average}")
3. List Comprehension:🧹
- The approach described earlier for generating the list squares consisted of using three or four lines of code. A list comprehension allows you to generate this same list in just one line of code. A list comprehension combines the for loop and the creation of new elements into one line, and automatically appends each new element.
- The following example builds the same list of square numbers you saw earlier but uses a list comprehension:
squares = [value**2 for value in range(1, 10)]
print(squares)
To use this syntax,
- Begin with a descriptive name for the list, such as squares.
- Next, open a set of square brackets
3. Define the expression for the values we want to store in the new list. Here, the expression is value ** 2
, which calculates the squares.
4. Then, write a for loop to generate the numbers we want to FEED into the expression.
5. Close the square brackets.
4. Working with Part of a List🧹
- Earlier we learned how to access single elements in a list, and now we will be looking into how to work through all the elements in a list. We can also work with a specific group of items in a list, called a slice in Python.
4.1 Slicing a List🍰
- To make a slice, we specify the index of the first and last elements we want to work with. As with the
range()
functions, Python stops one item before the second index we specify. To output the first 5 elements in a list, we would request indices 0 through 5, which would return elements 0, 1, 2, 3, and 4. - The following example involves a list of players on a team:
players = ['M. S. Dhoni', 'Virat Kohli', 'Sachin Tendulkar', 'Anil Kumble', 'Zaheer Khan']
print(players[0:3])
- This code prints a slice of the list. The output retains the structure of the list, and includes the first three players in the list:
- We can generate any subset of a list. For example, if we want to get the second, third, and fourth items in a list, we would start the slice at index 1 and end it at index 4:
players = ['M. S. Dhoni', 'Virat Kohli', 'Sachin Tendulkar', 'Anil Kumble', 'Zaheer Khan']
print(players[1:4])
- If we omit the first index in a slice, Python automatically starts our slice at the beginning of the list:
players = ['M. S. Dhoni', 'Virat Kohli', 'Sachin Tendulkar', 'Anil Kumble', 'Zaheer Khan']
print(players[:4])
- A similar syntax works if we want to slice the list to the very last element in the list:
players = ['M. S. Dhoni', 'Virat Kohli', 'Sachin Tendulkar', 'Anil Kumble', 'Zaheer Khan']
print(players[2:])
- We had seen the negative index return an element from the end of a list; therefore, we can output any slice from the end of a list.
- For example, if we want to output the last three players on the roaster, we can use the slice
players[-3:]
players = ['M. S. Dhoni', 'Virat Kohli', 'Sachin Tendulkar', 'Anil Kumble', 'Zaheer Khan']
print(players[-3:])
- We can also add a third parameter and ask Python to skip those many items in between.
players = ['M. S. Dhoni', 'Virat Kohli', 'Sachin Tendulkar', 'Anil Kumble', 'Zaheer Khan']
print(players[::2])
4.2 Looping through a Slice 🍽️
We can use a slice in a for loop if we want to loop through a subset of the elements in a list. In the next example, we loop through the first three players and print their names as part of a simple roster:
players = ['M. S. Dhoni', 'Virat Kohli', 'Sachin Tendulkar', 'Anil Kumble', 'Zaheer Khan']
for player in players[:3]:
print(player)
5. Copy📋
5.1 Copying a List:📝
- Often, we might need to copy one list into another, or start with an original list and make an entirely new list based on the first one.
- To copy a list, we can make a slice that includes the entire original list by omitting the first index and the second index ([:]). This tells Python to make a slice that starts at the first item and ends with the last item, producing a copy of the entire list.
- For example, imagine we have a list of our favorite actors and want to make a separate list of actors that a friend likes. This friend likes everything on our list so far, We can create their list by copying ours:
actors = ['Irrfan Khan', 'SRK', 'RDJ', 'Vijay Sethupathi']
friends_actors = actors[:]
print(f"Our List: {actors}")
print(f"Friends List: {friends_actors}")
These two will be different lists and we can append different items to each of them.
actors = ['Irrfan Khan', 'SRK', 'RDJ', 'Vijay Sethupathi']
friends_actors = actors[:]
print(f"Our List: {actors}")
print(f"Friends List: {friends_actors}")
actors.append('Ranbir Kapoor')
friends_actors.append('Ranveer Singh')
print("\n")
print(f"Our List: {actors}")
print(f"Friends List: {friends_actors}")
5.2 Why use slicing for copy?🧩
- Instead of assigning a copy of
actors
tofriends_actors
, we setactors
equals tofriends_actors
. - This syntax tells Python to associate the new variable f
riends_actors
with the list that is already associated withactors
, so now both variables point to the same list. - As a result, when we add Ranbir Kapoor to
actors
, it will also appear infriends_actors
. Likewise, if ‘Ranveer Singh’ will appear in both lists, even though it appears to be added only tofriends_actors
. - Let’s check this by the example.
actors = ['Irrfan Khan', 'SRK', 'RDJ', 'Vijay Sethupathi']
friends_actors = actors
print(f"Our List: {actors}")
print(f"Friends List: {friends_actors}")actors.append('Ranbir Kapoor')
friends_actors.append('Ranveer Singh')print("\n")
print(f"Our List: {actors}")
print(f"Friends List: {friends_actors}")
6. Tuples:🍇
- Lists work well for storing collections of items that can change throughout the life of a program.
- The ability to modify lists is particularly important when we are working with a list of users on a website or a list of characters in a game.
- However, sometimes we will want to create a list of items that cannot change. Tuples allow you to do just that.
- Python refers to values that cannot change as immutable, and an immutable list is called a tuple.
6.1 Defining a Tuple:🆕
- A tuple looks just like a list, except we use parenthesis instead of square brackets. Once you define a tuple, you can access individual elements by using each item’s index, just as we would for a list.
- For example, if we have a rectangle that should always be a certain size, we can ensure that its size doesn’t change by putting the dimensions into a tuple:
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
- We define the tuple dimensions, using parentheses instead of square brackets. Then we print each element in the tuple individually, using the same syntax we have been using to access elements in a list:
- Let’s see what happens if we try to change one of the items in the tuple dimensions:
dimensions = (200, 20)
dimensions[0] = 25
- This code tries to change the value of the first dimension, but Python returns a type error. Because we are trying to alter a tuple, which cannot be done. Thus, we get a Python error telling us that we cannot assign a new value to an item in a tuple.
6.2 Looping through all values in a Tuple:🔄
- We can loop over all the values in a tuple using a for loop, like lists:
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
6.3 Writing over a Tuple :
- Although we cannot modify a tuple, we can assign a new value to a variable that represents a tuple.
- For example, if we wanted to change the dimensions of this rectangle, we could redefine the entire tuple:
dimensions = (200, 50)
print("Original Dimensions")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
Here we are not changing/modifying the values in the tuple instead we are totally reassigning a variable that is valid.
Notes:🗒️
1. It takes practice to write the list comprehensions, but they are useful.
2. Tuples are technically defined by the presence of a comma; the parentheses make them look more readable.
3. When compared with lists, tuples are simple data structures. We can use them when we need to store a set of values that should not be changed throughout the life of a program.
Summary:📚
1. We learned how to work efficiently with the elements in a List.
2. How to work through a list using a for loop.
3. How to make simple numerical lists.
4. How to slice a list?
5. What is tuple?
6. How and when to use tuple.