0:00

In this video we will cover Loops in particular for loops and while loops.

Â We will use many visual examples in this video.

Â See the labs for examples with data.

Â Before we talk about loops,

Â let's go over the range function.

Â The range function outputs and ordered sequence as a list I.

Â If the input is a positive integer,

Â the output is a sequence.

Â The sequence contains the same number of elements as the input but starts at zero.

Â For example if the input is three the output is the sequence zero, one, two.

Â If the range function has two inputs where

Â the first input is larger than the second input,

Â the output is a sequence that starts at the first input.

Â Then the sequence iterates up to but not including the second number.

Â For the input 10 and 15 we get the following sequence.

Â See the labs for more capabilities of the range function.

Â Please note if you use Python three

Â the range function will not generate a list explicitly like in Python two.

Â In this section. We will cover for loops.

Â We will focus on lists but many of the procedures can be used on tupples.

Â Loops perform a task over and over.

Â Consider the group of colored squares.

Â Let's say we would like to replace each colored square with a white square.

Â Let's give each square a number to make things a little

Â easier and refer to all the group of squares as squares.

Â If we wanted to tell someone to replace squares zero with

Â a white square we would say equals replace square

Â zero with a white square or we can say four squares

Â zero in squares square zero equals white square.

Â Similarly for the next square we can say for square one in squares,

Â square one equals white square.

Â For the next square we can say for square two in squares,

Â square two equals white square.

Â We repeat the process for each square.

Â The only thing that changes is the index of the square we are referring to.

Â If we're going to perform a similar task in Python we cannot use actual squares.

Â So let's use a list to represent the boxes.

Â Each element in the list is a string representing the color.

Â We want to change the name of the color in each element to white.

Â Each element in the list has the following index.

Â This is a syntax to perform a loop in Python.

Â Notice the indent, the range function generates a list.

Â The code will simply repeat everything in the indent five times.

Â If you were to change the value to six it would do it 6 times.

Â However, the value of I is incremented by one each time.

Â In this segment we change the I element of the list to the string white.

Â The value of I is set to zero.

Â Each iteration of the loop starts at the beginning of the indent.

Â We then run everything in the indent.

Â The first element in the list is set to white.

Â We then go to the start of the indent,

Â we progress down each line.

Â When we reach the line to change the value of the list,

Â we set the value of index one to white.

Â The value of I increases by one.

Â We repeat the process for index two.

Â The process continues for the next index,

Â until we've reached the final element.

Â We can also iterate through a list or tupple directly in python,

Â we do not even need to use indices.

Â Here is the list squares.

Â Each iteration of the list we pass

Â one element of the list squares to the variable square.

Â Lets display the value of the variable square on this section.

Â For the first iteration,

Â the value of square is red,

Â we then start the second iteration.

Â For the second iteration,

Â the value of square is yellow.

Â We then start the third iteration for the final iteration the value of Square is Green,

Â a useful function for iterating data is enumerate.

Â It can be used to obtain the index and the element in the list.

Â Let's use the box analogy with the numbers representing the index of each square.

Â This is the syntax to iterate through a list and provide the index of each element.

Â We use the list squares and use the names of the colors to represent the colored squares.

Â The argument of the function enumerate is the list.

Â In this case squares the variable I is

Â the index and the variable Square is the corresponding element in the list.

Â Let's use the left part of the screen to

Â display the different values of the variable square.

Â And I for the various iterations of the loop for the first iteration.

Â The value of the variable is red corresponding to

Â the zeroth index and the value for I is zero for the second iteration.

Â The value of the variable square is yellow and

Â the value of eye corresponds to its index i.e.

Â 1. We repeat the process for the last index.

Â While loops are similar to for loops but instead of executing

Â a statement a set number of times a while loop will only run if a condition is met.

Â Let's say we would like to copy

Â all the orange squares from the list squares to the list New squares.

Â But we would like to stop if we encounter a non-orange square.

Â We don't know the value of the squares beforehand.

Â We would simply continue the process while the square is

Â orange or see if the square equals orange.

Â If not we would stop for the first example.

Â We would check if the square was orange.

Â It satisfies the conditions so we would copy the square.

Â We repeat the process for the second square. The condition is met.

Â So we copy the square in the next iteration we encounter a purple square.

Â The condition is not met.

Â So we start the process.

Â This is essentially what a while loop does.

Â Let's use the figure on the left to represent the code we will

Â use a list with the names of the color to represent the different squares.

Â We create an empty list of new squares.

Â In reality the list is of indeterminate size.

Â We start the index at zero the while statement will repeatedly

Â execute the statements within the indent until the condition inside the bracket is false.

Â We append the value of the first element of the list squares to the list, new squares.

Â We increase the value of I by one.

Â We append the value of the second element of the list squares to the list, new squares.

Â We increment the value of I,

Â now the value in the array squares is purple,

Â therefore the condition for the while statement is false and we exit the loop.

Â Check out the labs for more examples of loop many with real data.

Â