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 are a little bit more.
If I print here a range and I say range one hundred and I click Run.
I get range of zero to 100.
Hmm, so it looks like a top, but it's not it's a range object.
It's a special type of object.
And arrange you can give it like this, which gives you a default of zero to whatever you give it,
which is one hundred, or you can just simply type zero.
One hundred.
And if I click run, it's the same thing.
But where range becomes really useful and you'll see this in 99 percent of cases in Python is in for
loops.
You see, besides tuples and lists and sets and dictionaries and strings, you can iterate over a range.
So for example, if I do for I in range and let's say item or in our case is the number in range.
And in here I do print number.
If I run this.
You'll see that I'll get numbers one to one hundred and why is this useful?
Well, using range, I was able to loop a hundred times.
As you can see all the way till 99, because our count starts at zero and we're able to loop as many
times as I want so I can tell the loop, maybe we want to loop a certain number of times.
Maybe you won't want to loop 10 times where I can tell the loop how many times to run.
It's extremely useful.
Maybe here were performing some action where it says.
I don't know.
Email, email list, so we want to send 10 emails to an email list, for example.
Well, again, I can just click run and I sent the email while a bunch of times, ten times.
Another neat trick is that, well, you might not even need no.
Right.
We're not really using it in our code.
And often what 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 they step over, so if I do, too, and by the way, default is one here.
If I do two.
It jumps from zero to 10 by two.
Which is very, very handy.
So what is a range?
What range creates a special kind of object that we can iterate over?
OK, what if I do minus, oh, right here, what if I do minus one?
Well if I run this.
Hmm, that's not going to work because, well, let me show you why if I changed this to zero and 10
and I do minus one.
It's going to go in the opposite direction, but if I did just 10 and zero.
That's not going to work if we want to do something in reverse, we just have to make sure that we add
that minus one or if we want to 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 for loops.
But he can also do something interesting like let's say a range.
Of ten, 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, too, 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 and iterable object that has 10 items, and then I'm going to convert that into a list so you
can see here zero to nine into a list.
So this is a quick way to create a list.
That wall has integers.
A neat trick that you'll definitely use a longer program or journey I'll see in the next one, bye bye.