-
[MUSIC PLAYING]
-
-
MOSH HAMEDANI: There
are times that we
-
may want to repeat a
task a number of times.
-
For example, let's say we
send a message to a user.
-
If that message
cannot be delivered,
-
perhaps we want to
retry three times.
-
Now, for simplicity,
let's imagine
-
this print statement is
equivalent to sending a message.
-
In a real-world program, to
send a message to a user,
-
we have to write 5
to 10 lines of code.
-
Now, if you want to
retry three times,
-
we don't want to
repeat all that code.
-
That is ugly.
-
That's when we use a loop.
-
We use loops to
create repetition.
-
So here is how it works.
-
We start with for, number, in.
-
We have a built-in
function called range.
-
Now how many times do we
want to repeat this task?
-
Let's say three times.
-
So we call range and
pass 3 as an argument.
-
Now, similar to
our if statements,
-
we need to terminate
this line with a colon.
-
Enter-- we get indentation.
-
So in this block, we can
write all the statements that
-
should be repeated three times.
-
Let's do a print, a
message like attempt.
-
Save the changes.
-
Run the program.
-
So we have attempt
printed three times.
-
Beautiful.
-
Now, what is this number?
-
Let's take a look.
-
It's a variable of type integer.
-
So let's pass it as the second
argument to the print function.
-
Number.
-
Run the program.
-
This is what we get--
-
0, 1, 2.
-
So here we have a for loop.
-
This for loop is
executed three times.
-
In each iteration, number
will have a different value.
-
Initially it will be 0.
-
In the second
iteration, it will be 1.
-
And finally, in the last
iteration, it will be 2.
-
Now here we can
do something fun.
-
We can add 1 to this.
-
Run the program.
-
And now the messages
that we print
-
are more meaningful
or more user-friendly,
-
like attempting number 1,
attempting number 2, and so on.
-
We can take this
to the next level.
-
So we can pass another argument.
-
Here, I'm going to
add an expression.
-
One more time.
-
Number plus 1.
-
So we'll get 1, 2, 3.
-
Now I want to put this
expression in parentheses.
-
So let's select this.
-
Put it in parentheses, and
then multiply it by a dot.
-
So here we have a string that
is multiplied by a number.
-
The result will be that string
repeated that number of times.
-
Let's take a look.
-
So run the program.
-
See.
-
That's pretty cool, isn't it?
-
Now, let me show
you one more thing
-
before we finish this lecture.
-
As you saw, this range
function generates
-
numbers starting from 0 all
the way up to this number here,
-
but it doesn't
include this number.
-
Here we can pass another
argument, say, start from 1
-
and finish before 4.
-
With this change, we don't
need to add 1 to number
-
every time, because in
the first iteration,
-
this number variable
will be set to 1.
-
So we can simplify our
code and make it cleaner.
-
Let's run it one more time.
-
We get the exact same result. We
can also pass a third argument
-
as a step.
-
So I'm going to change
the second argument to 10
-
and pass 2 as a step.
-
Look at the result. These
are the numbers we get--
-
1, 3, 5, and so on.
-
So pretty useful.
-
You're going to use
this function a lot
-
in real-world applications.
-
-
[MUSIC PLAYING]
-
-
Continuing with the example
from the last lecture,
-
let's imagine the scenario
where after the first attempt
-
we can successfully
send the message.
-
In that case, we want to
jump out of this loop.
-
We don't want to repeat this
task of sending a message three
-
times.
-
Let me show you how
to implement this.
-
So in this demo, I'm going to
simulate the scenario where
-
we can successfully
send a message.
-
So we define a variable,
successful, and set it to true.
-
Now here after this
print statement,
-
we'll have an if statement.
-
If successful, colon, then
perhaps we can print successful.
-
Now here we want to
jump out of this loop.
-
For that, we use
the break statement.
-
Let's run this program
and see what happens.
-
So there you go.
-
After the first attempt,
we are successful,
-
and there are no more attempts.
-
So once again, I want you
to pay great attention
-
to the indentation here because
that's one of the common issues
-
amongst beginners.
-
So here's for loop.
-
These two lines are
indented with four spaces,
-
and they belong to a for loop.
-
In every iteration, these
two lines will be executed.
-
Now when we get to line 4,
if this condition is true,
-
then these two lines
will be executed
-
because both these lines
are indented below this if
-
statement.
-
Now let's take this
program to the next level.
-
What if we attempt three
times and we still cannot send
-
an email?
-
Perhaps we want to display a
different message to the user.
-
We say, hey, we tried three
times, but it didn't work.
-
So I'm going to change
successful to false.
-
Now at the end, here we
can add an else statement.
-
This is what we call a
false else statement.
-
What we put under
this else statement
-
will only be executed
if this loop completes
-
without an early termination.
-
So if we never break
out of this loop,
-
then the else statement
will be executed.
-
So here we can print a message
like attempted three times
-
and failed.
-
So run the program.
-
See what we get.
-
Three attempts followed by this
message-- attempted three times
-
and failed.
-
In contrast, if we change
successful to true,
-
because we terminate this loop
using this break statement,
-
what we have in the else
block will not be executed.
-
Take a look.
-
Run the program.
-
We have one attempt.
-
Successful.
-
Done.
-
-
[MUSIC PLAYING]
-
-
In programming, we have this
concept called nested loops.
-
So we can put one loop
inside of another loop.
-
And with this, we can get
some interesting results.
-
Let me show you.
-
So I'm going to start with this
loop-- for x in range 5 colon.
-
Now inside of this loop, I'm
going to add another loop.
-
So for y in range 3 colon.
-
And then in our second loop, I'm
going to add a print statement.
-
Here we can use formatted
strings to display coordinates.
-
Remember formatted string, so
we have f followed by quotes.
-
Now here we add parentheses
for our coordinate.
-
First, we want to display x
and then comma followed by y.
-
Let's run this program
and see what happens.
-
There you go.
-
Pretty cool, isn't it.
-
So we get 0 and 0,
0 and 1, 0 and 2.
-
Then we get 1 and 0, 1
and 1, 1 and 2, and so on.
-
Now let me explain how exactly
Python interpreter executes
-
this code.
-
So here we have two loops.
-
This is what we
call the outer loop,
-
and this is the inner loop.
-
So the execution of our
program starts here.
-
In the first iteration
of this loop, x is 0.
-
Now we get to this
statement, which
-
is a child of this for
statement because it's
-
indented four times.
-
This statement itself is a loop.
-
So what we have
inside of this loop
-
will be executed three times.
-
In the first iteration, x
is 0 because we're still
-
in the first iteration
of the outer loop.
-
And y is also 0 because we
are in the first iteration
-
of the inner loop.
-
That is why we get 0 and 0.
-
Now we go to the second
iteration of this inner loop.
-
In this iteration, y will
be 1, whereas x is still 0.
-
That is why we get 0 and 1.
-
And similarly, in the third
iteration of our inner loop,
-
we'll get 0 and 2
in the terminal.
-
Now we're done with the
execution of the inner loop.
-
So the control moves
back to our outer loop.
-
Here will be in the
second iteration.
-
So x will be 1.
-
And then we start here again.
-
So we have to execute this
inner loop three times.
-
In the first iteration,
y will be 0, and x is 1.
-
So here we have 1 and 0.
-
Then we'll get 1
and 1, and 1 and 2.
-
You got the point.
-
So this is all
about nested loops.
-
-
[MUSIC PLAYING]
-
-
So you have learned
how to use for loops
-
to repeat one or more
statements in your programs.
-
Now let's dive
deeper and see what
-
this range function returns.
-
So earlier, you learned about
the built-in type function.
-
With this function, we can
get the type of an object.
-
So if I pass 5 here and run this
program, this is what we get.
-
So the type of this number or
this object is int or integer.
-
Now let's look at
the type of the value
-
that we get from
the range function.
-
So as an argument, we
pass range of a number.
-
Let's run this program.
-
So this range function returns
an object of type range.
-
So in Python, we have primitive
types like numbers, strings,
-
and Booleans.
-
But we also have complex types.
-
Range is an example of one
of those complex types.
-
Throughout this
course, you're going
-
to learn about a lot
of other complex types.
-
Now what is interesting
about this range object
-
is that it's iterable, which
means we can iterate over
-
it or use it in a for loop.
-
That is why we can
write code like this.
-
So this range function
returns a range object
-
which is iterable, which
means we can iterate over it.
-
In each iteration, x will
have a different value.
-
Now, range objects are not the
only iterable objects in Python.
-
Strings are also iterable.
-
So here we can add a
string like Python.
-
Now in each
iteration, x will hold
-
one character in this string.
-
Let me show you.
-
So print, x, and I'm going to
delete these two lines here.
-
Let's run this program.
-
So in each iteration, we'll
get one character and print it.
-
We have another complex
type called list which we
-
use to store a list of objects.
-
So we add square brackets.
-
This indicates a list.
-
Now we can add a list of
numbers or a list of strings,
-
like a list of names.
-
You will learn about
lists later in the course.
-
So let's run this one more time.
-
As you can see, we can
iterate over lists.
-
In each iteration, we'll
get one object in this list.
-
Now, later in the
course, I will show you
-
how to create your own custom
objects that are iterable.
-
For example, you will learn
how to write code like this--
-
for item in shopping
cart, print item.
-
So shopping cart is
going to be a custom
-
object that you will create.
-
It's not going to be an
integer or a string or Boolean.
-
It's a custom object.
-
It has a different
structure, and we
-
will make it iterable so we
can use it in a for loop.
-
And in each
iteration, we can get
-
one item in the shopping cart
and print it on the terminal.
-
-
[MUSIC PLAYING]
-
-
All right, time for an exercise.
-
I want you to write a program
to display the even numbers
-
between 1 to 10.
-
So when you run this program,
you should see 2, 4, 6, and 8.
-
And after these, I want
you to print this message--
-
We have four even numbers.
-
Now here is a quick hint
before you get started.
-
You should call the range
function with 1 and 10.
-
Do not use the third argument,
which is called step.
-
So basically, I want you to
iterate over all the numbers
-
between 1 to 10.
-
Check if each number
is an even number
-
and then print it
on the terminal.
-
So pause the video.
-
Spend two minutes
on this exercise.
-
When you're done, come back.
-
Continue watching.
-
-
So we start with a for loop--
-
for number in range
1 to 10 colon.
-
We check to see if the remainder
of division of this number by 2
-
equals 0.
-
So if number modulus 2 equals
0, then we print this number.
-
Now let's run this program.
-
So we get 2, 4, 6, 8.
-
Beautiful!
-
Now to count the even numbers,
we need a separate variable.
-
So let's call that count.
-
Initially, we set it to 0.
-
Now in this if block, every
time we find an even number,
-
we need to increment count.
-
So we set count plus equals 1.
-
And finally, after for loop, we
can print a formatted string.
-
We have count even numbers.
-
Let's run the program.
-
And here's the result.
-
Hi, guys.
-
Thank you for watching
this tutorial.
-
My name is Mosh
Hamedani, and I have
-
tons of tutorials like
this for you on my channel.
-
So be sure to subscribe!
-
And also please like
and share this video.
-
If you want to learn
Python properly
-
from scratch with depth, I have
a comprehensive Python tutorial
-
for you.
-
The link is below this video.
-
So click the link
to get started.
-
Thank you, and have
a fantastic day!
-