Loops



 we'll delve into loops, particularly for loops and while loops. We'll use visual examples throughout. But before we dive into loops, let's discuss the range function. The range function generates an ordered sequence, typically used as a list. If given a positive integer as input, it outputs a sequence starting from zero and containing the same number of elements as the input. For instance, if the input is three, the output sequence would be 0, 1, 2. If the range function is provided with two inputs where the first is smaller than the second, it generates a sequence starting from the first input and ending just before the second input. For example, range(10, 15) would produce a sequence starting from 10 up to, but not including, 15. Refer to the labs for more on the capabilities of the range function. It's worth noting that in Python 3, the range function doesn't explicitly generate a list as it did in Python 2.

Now, let's move on to for loops. Loops are used to repeat a task multiple times. Consider a group of colored squares, where we want to replace each colored square with a white one. If we assign a number to each square and refer to the group collectively as "squares," we can instruct someone to replace each square individually. In Python, we represent this with a list where each element is a string representing the color. We want to change each color to white, iterating through the list. Here's the syntax for a for loop in Python: for i in range(5): squares[i] = "white"

In this loop, the range function generates a list, and the loop repeats the indented block of code five times. Within each iteration, the variable "i" increments by one, allowing us to access and modify each element of the list "squares" sequentially.

We can also iterate through a list directly without using indices. For instance: for square in squares: print(square)

In this loop, during each iteration, the variable "square" takes on the value of each element in the list "squares" successively.

Another useful function for iterating through data is enumerate(), which provides both the index and the element in the list. For example: for i, square in enumerate(squares): print(i, square)

This loop iterates through the list "squares," with "i" representing the index and "square" representing the corresponding element in the list.

Now, let's discuss while loops. While loops are similar to for loops but instead of executing a statement a set number of times, they continue executing as long as a condition is true. For example, if we want to copy all the orange squares from one list to another until we encounter a non-orange square, we can use a while loop. The loop continues as long as the condition (i.e., the square being orange) is true. Once the condition becomes false (i.e., we encounter a non-orange square), the loop stops.

Here's the syntax for a while loop:
while condition: # execute statements

The loop continues executing the statements within the indented block until the condition becomes false.

Comments

Popular posts from this blog

Lila's Journey to Becoming a Data Scientist: Her Working Approach on the First Task

Notes on Hiring for Data Science Teams

switch functions