1
00:00:01,330 --> 00:00:10,210
When looping in Python, one of the most common tools is this range function that we get out of the

2
00:00:10,210 --> 00:00:17,920
box with Python and you see here, that range, well, is a range object, a special type of object

3
00:00:17,920 --> 00:00:24,880
in Python that returns an object that produces a sequence of integers from start to stop.

4
00:00:26,460 --> 00:00:28,050
Let's explore that are a little bit more.

5
00:00:29,820 --> 00:00:36,510
If I print here a range and I say range one hundred and I click Run.

6
00:00:38,070 --> 00:00:42,210
I get range of zero to 100.

7
00:00:43,920 --> 00:00:49,080
Hmm, so it looks like a top, but it's not it's a range object.

8
00:00:49,110 --> 00:00:50,820
It's a special type of object.

9
00:00:51,730 --> 00:00:57,880
And arrange you can give it like this, which gives you a default of zero to whatever you give it,

10
00:00:58,300 --> 00:01:01,900
which is one hundred, or you can just simply type zero.

11
00:01:02,080 --> 00:01:02,680
One hundred.

12
00:01:03,130 --> 00:01:05,920
And if I click run, it's the same thing.

13
00:01:06,730 --> 00:01:14,650
But where range becomes really useful and you'll see this in 99 percent of cases in Python is in for

14
00:01:14,680 --> 00:01:15,190
loops.

15
00:01:15,910 --> 00:01:22,260
You see, besides tuples and lists and sets and dictionaries and strings, you can iterate over a range.

16
00:01:22,750 --> 00:01:32,740
So for example, if I do for I in range and let's say item or in our case is the number in range.

17
00:01:33,790 --> 00:01:37,030
And in here I do print number.

18
00:01:38,140 --> 00:01:39,130
If I run this.

19
00:01:40,320 --> 00:01:45,190
You'll see that I'll get numbers one to one hundred and why is this useful?

20
00:01:46,050 --> 00:01:50,130
Well, using range, I was able to loop a hundred times.

21
00:01:51,400 --> 00:01:58,420
As you can see all the way till 99, because our count starts at zero and we're able to loop as many

22
00:01:58,420 --> 00:02:03,910
times as I want so I can tell the loop, maybe we want to loop a certain number of times.

23
00:02:04,300 --> 00:02:09,400
Maybe you won't want to loop 10 times where I can tell the loop how many times to run.

24
00:02:09,550 --> 00:02:11,080
It's extremely useful.

25
00:02:12,450 --> 00:02:16,410
Maybe here were performing some action where it says.

26
00:02:18,140 --> 00:02:18,650
I don't know.

27
00:02:19,400 --> 00:02:25,950
Email, email list, so we want to send 10 emails to an email list, for example.

28
00:02:26,660 --> 00:02:33,740
Well, again, I can just click run and I sent the email while a bunch of times, ten times.

29
00:02:34,490 --> 00:02:38,930
Another neat trick is that, well, you might not even need no.

30
00:02:39,140 --> 00:02:39,530
Right.

31
00:02:39,530 --> 00:02:41,450
We're not really using it in our code.

32
00:02:41,930 --> 00:02:48,560
And often what you'll find in Python programs is if a programmer doesn't need this variable, you just

33
00:02:48,560 --> 00:02:49,670
do an underscore.

34
00:02:50,090 --> 00:02:52,000
And this is just a variable.

35
00:02:52,040 --> 00:02:54,890
I mean, you could use it like this.

36
00:02:56,880 --> 00:03:02,940
And you'll still get these numbers, but it just indicates to other people that, hey, I don't really

37
00:03:02,940 --> 00:03:08,070
care what this variable is, I'm just trying to use range so that we can loop over things, let's say,

38
00:03:08,070 --> 00:03:08,640
10 times.

39
00:03:09,880 --> 00:03:13,360
Now, range also comes with a third parameter.

40
00:03:14,300 --> 00:03:20,150
And what that is, is they step over, so if I do, too, and by the way, default is one here.

41
00:03:20,330 --> 00:03:21,290
If I do two.

42
00:03:22,570 --> 00:03:26,500
It jumps from zero to 10 by two.

43
00:03:28,020 --> 00:03:29,870
Which is very, very handy.

44
00:03:31,170 --> 00:03:32,410
So what is a range?

45
00:03:32,640 --> 00:03:37,110
What range creates a special kind of object that we can iterate over?

46
00:03:38,290 --> 00:03:43,510
OK, what if I do minus, oh, right here, what if I do minus one?

47
00:03:44,290 --> 00:03:45,490
Well if I run this.

48
00:03:46,950 --> 00:03:55,350
Hmm, that's not going to work because, well, let me show you why if I changed this to zero and 10

49
00:03:55,350 --> 00:03:56,580
and I do minus one.

50
00:04:01,000 --> 00:04:07,810
It's going to go in the opposite direction, but if I did just 10 and zero.

51
00:04:09,680 --> 00:04:14,600
That's not going to work if we want to do something in reverse, we just have to make sure that we add

52
00:04:14,600 --> 00:04:21,020
that minus one or if we want to step back twice, then we click run.

53
00:04:21,230 --> 00:04:26,750
So you have a lot of options to play with the range you can do.

54
00:04:26,750 --> 00:04:27,980
Ascending, descending.

55
00:04:28,850 --> 00:04:32,660
One final thing, another place that you might see ranges.

56
00:04:32,660 --> 00:04:35,600
And like I said, most of the time, they're in for loops.

57
00:04:35,960 --> 00:04:40,640
But he can also do something interesting like let's say a range.

58
00:04:42,800 --> 00:04:53,360
Of ten, and then if we wrap this in a list, remember, a list function allows us to convert something

59
00:04:53,360 --> 00:04:54,020
to a list.

60
00:04:54,260 --> 00:04:57,350
If I run this, did you see that?

61
00:04:58,440 --> 00:05:04,860
Let's simplify this so that this range is going to just be, too, if I click, run.

62
00:05:06,390 --> 00:05:13,200
You see that I loop two times and in here I'm going to print, first of all, I'm going to create a

63
00:05:13,200 --> 00:05:21,000
range and iterable object that has 10 items, and then I'm going to convert that into a list so you

64
00:05:21,000 --> 00:05:24,250
can see here zero to nine into a list.

65
00:05:24,540 --> 00:05:27,240
So this is a quick way to create a list.

66
00:05:27,720 --> 00:05:30,510
That wall has integers.

67
00:05:31,530 --> 00:05:38,310
A neat trick that you'll definitely use a longer program or journey I'll see in the next one, bye bye.
