-: When looping in Python, one
of the most common tools is this "range" function
that we get out of the box with Python.
And you see here that range, well, is a range object
a special type of object in Python that returns an object
that produces a sequence of integers
from start to stop.
Let's explore that a little bit more.
If I print here a range, and I say "range 100"
and I click "run,"
I get range of 0 to 100.
Hmm. So it looks like a topple, but it's not.
It's a range object.
It's a special type of object. And range,
you can give it like this, which gives you a default
of 0 to whatever you give it, which is 100
Or you can just simply type 0, 100.
And if I click "run," it's the same thing.
But where "range" becomes really useful, and you'll see this
in 99% of cases in Python, is in 4 loops.
You see,
besides "topples and lists and sets and dictionaries"
"and strengths," you can iterate over a range.
So for example, if I do "for i"
in "range," and let's just
say "item," or in our case, let's just do "number in range."
And in here I do "print number."
If I run this, you'll see that I'll get numbers 1
to 100. And why is this useful?
Well, using "range," I was able to loop 100 times,
as you can see, all the way till 99 because our account
starts at 0, and we're able to loop as many times
as I want. So I can tell the loop,
maybe we wanna loop a certain number of times.
Maybe you wanna loop 10 times where I can tell the loop how
many times to run.
It's extremely useful.
Maybe here we're performing some action where it says
I don't know, email, "email list."
So we wanna send 10 emails to an email list, for example.
Well, again, I can just click "run" and I sent that email
well a bunch of times, 10 times.
Another neat trick is that, well, you might not
even need "number," right?
We're not really using it in our code.
And often while you'll find in Python programs is,
if a programmer doesn't need this variable, you just
do an underscore.
And this is just a variable.
I mean, you could use it like this
and you'll still get
these numbers, but it just indicates
to other people that, "hey,
I don't really care what this variable is.
I'm just trying to use 'range'
so that we can loop over things."
Let's say 10 times.
Now, "range" also comes with a third parameter.
And what that is, is the step over.
So if I do two, and by the way
default is one here, if I do two
it jumps from 0 to 10 by 2,
which is very, very handy.
So what is "range?"
Well, "range" creates a special kind
of object that we can iterate over.
Okay, what if I do minus oh, right here?
What if I do -1?
Well, if I run this, hmm,
that's not going to work because, well, let me show you why.
If I change this to 0
and 10 and I do -1
it's going to go in the opposite direction.
But if I did just 10 and 0,
that's not going to work.
If we wanna do something in reverse,
we just have to make sure that we add that -1
or, if we wanna step back twice, then we click "run."
So you have a lot of options to play with the range.
You can do ascending, descending.
One final thing, another place that you might see ranges.
And, like I said, most of the time, they're in four loops
but he can also do something interesting
like, let's say, a range
of 10.
And then if we wrap this in a list,
remember a "list function" allows us to convert something
to a list.
If I run this, did you see that?
Let's simplify this so that this range is
going to just be 2.
If I click "run," you see that I
loop two times
and in here I'm going to print, First of all
I'm going to create a range,
an Iterable object that has 10 items
and then I'm going to convert that into a list.
So you can see here 0 to 9 into a list.
So this is a quick way to create a list that well,
has integers.
A neat trick that you'll definitely use along your
programmer journey.
I'll see you in the next one. Bye-bye!
-: -Captions done by Rev. Visit Rev.com for details.