Explanation with Diagram and Examples of For Loop and For else

6 minutes
Share the link to this page
Copied
  Completed
This video explains with examples and diagrams; for loop and for loop else.

Transcript

Hi, this is the second video of series flow control in Python. One of the most important question that comes to mind is why I have chosen Python, then the answer is it is one of the fastest growing programming languages in the world today. And beside, it is also the most commonly used programming language for data science, machine learning and artificial intelligence. Now let's come to the topic of flow control, which also includes for loops, it is using Python to iterate over a sequence or other iterable objects. iteration over a sequence of object is also called traversal. Now, the syntax of the for loop is for this is a variable in sequence.

Now, the sequence could be any iterable object, that could be a list could be a tupple, or any other iterable object. and the value is this is the variable that takes the value of the sequence within the iterable object and the Finally the body of called loop. Now let's see this in the form of diagram. Okay for loop starts with iteration, it checks the item. And it trades over it unless, as long as the condition is true or as long as there's the last last object within the iterable object is reached. So you can see here within the diagram when the last object is reached, but as soon as it is reached, it will exit.

Now we see four different programs within using for loop in Python. First one is just a simple call Do you have taken a variable num that has a value of six. And here is a trait over the object for AI is a variable that takes in the value of the iterable object and ranges the object itself. So ranges function within Python. And it's syntax is like, here it is written. It is The first parameter is a start value.

The second is this stop when you are the end value. And the third parameter is the step size. So here in this program, we have a start value that is two and the end value, or the stop value taters six. So it is going to continue iterating over this range object, as long as this sixth speech, so from starting from two, it is going to continue printing up to six. Let's run this program, see 2345 written and when the six is read, it stopped printing it started reading over the body of the loop. The second program, I've taken a list with the name fruit, start trading over this and what it does here, it's using the index of this list to iterate over this object of fruit.

Again, I'm using the range function but I am also used to land function that counts the number of elements within this list. So, here it is 03 punches three, so it is going to print I like that is a constant, and then the variable fruit or the list fruit with its index 123 but this could be 012 and when I run this it is going to print I like then the first object within this list is mango, banana and orange. The third is this is the for loop with else it can also not not the for loop can also have an optional s block as well. This part is executed if the items in the sequence using the loop are finished. So, when the for loop items are finished, or the last item is raised, this else is printed out. Let's see how it works.

So, if we analyze, this is going to print 012 and then it To interpret noise from left, so, it exactly does the same thing for program is we are going to check if a number given by the user is a prime number or not. Let's see the number given is 27 we take actually 33 and then we start with taking the number if the number is greater than one or not, because the prime number cannot be a one number. So, we start by checking the prime number, if the number if it is greater than one we need to continue program as they get different numbers. So if it is one or smaller than one, then we can just write down we have used the else statement to print that the number is not a prime number. But if it is greater than one then we are going to take this is a prime number or not for that we are using the for loop and we are using the range function to start from two and continue iterating up to the number that is given here to here we have given 33 So now the for loop starts to check, we modulus the number by the iterating value, and here it is two.

So 33 divided by two, it is meant to get the remainder, that is not equal to zero, it could be one, because 32 divided by two would be like 16. And there would be one remainder, one remainder, and that one remainder means that it is not equal to zero, it will go to block and see and write down that the number is a prime number because it is not divisible by two or any other number. But actually, this iteration is going to continue to it, it checks up to that number. And also, if it is not a prime number, it is also going to check like for example, when when it is zero. For example, if the number is 32, and it becomes equal to zero, then it is going to print the number is not a prime number and then it's going to take that this number 32 when divided by n They trading value that is the first value is 200 times it gives that number and then it prints that out.

So let's run this program. 33 is not a prime number because three times 11 is 33. So it was not divisible by two, but the code it traded again and from three it came to know that he has it is divisible by three and therefore it is not a prime number. I hope you liked this video. You enjoyed it. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.