Welcome to another video of our Python lessons.
Today, we're going to talk about loops in Python.
Let's start with a simple prologue that prints a range of numbers using the range function.
The range function can take two or three argument.
Let's start by printing the numbers from zero to nine.
Now let's use the same function.
But this time we need to increment our numbers by two by adding number two as a third argument.
Nice.
Let's look at the nested for loops, assuming we have two lists.
First one is one, two, three, four, and the second one is five, six, seven.
Let's look through both of them using nested loop and see how it works.
As you noticed in the loop, it will start with the first element in this one, which is number one,
then it will loop through old elements list.
And that's the first iteration of the loop, the second iteration.
It will start from number two in this one here.
Then again, all elements in list two and so on.
So it took No one from list one here.
And it went through whole numbers and a list to five, six and seven, and then it took number two from
list one, which is two, and then it went through again, old numbers.
It lists two, five, six and seven and then three from list one and then five, six and seven again.
But what if we have two lists like this one, three, five and list two to four six?
Let's create them.
How can we loop through both of them and print the numbers in order?
So basically you want to start from the first element in this one and then the first element in this,
too, in order for us to do that, we can use that zip function.
Let's see how it works.
As we see here, first number, an ex is one and first number in line is two, then three, then four
and finally five and six.
See, the full loop is very simple.
Now, let's switch to why loop the syntax in while loop starts with the word wild, followed by the
condition you want this while loop to run on for an infinite while loop.
We use the word true.
Or the number one,
let's first use while loop to print the numbers from zero to nine.
We will first create a variable X and assign it a value of zero.
Then we start our while loop and check if x less than 10.
And then print X, so if X is less than 10, we print X and then increment incremented by one.
Now, let's take a look at the infinite while loop, we will take an input from the user and print that
input unless the user provided the word exit.
We will check if the string equalled the word exit by using the statement.
If that's true, then we print something to the screen.
Oh, I always miss a double.
OK.
And then we break out of the wild loop
belts, we print the my string, which we got from the user.
Now, let's type exit.
Thank you for using our program tonight.
Let's take a look now at the continuing statement.
And while loop
by using the word continue, the loop will start over skipping all the other lines.
Let us use the if statement to check if the string we are getting from the user is empty.
If the user provided an empty string, the wire loop will start over asking the user again for another
input.
But if the user hasn't provided an empty string, we basically print it.
Now, let's provide an empty line by just hitting enter.
By hitting enter, we will never get to this line.
It will basically come here and go back again and provide the user and ask him for another input.
We sometimes use this continuous statement if we just want to move to an empty line without providing
an input to the program.
Do you remember where we did we see this before?
Let's see.
S..
We have reached the end of this lesson, thank you and see you in the next one.