1
00:00:00,620 --> 00:00:00,830
Hi.

2
00:00:00,830 --> 00:00:01,670
Welcome back.

3
00:00:01,700 --> 00:00:05,060
In this video, we will create a simple program.

4
00:00:05,480 --> 00:00:13,700
We will start with this list, and then we will create a program which outputs this three lines here.

5
00:00:13,700 --> 00:00:20,810
So the program will sort of the items alphabetically, as you see, Ben is the first one, John.

6
00:00:20,840 --> 00:00:22,230
And then comes son.

7
00:00:22,700 --> 00:00:28,550
And we will also place a number before each of the items here in the output.

8
00:00:29,570 --> 00:00:30,770
So let's do that.

9
00:00:31,820 --> 00:00:33,080
How can we do this?

10
00:00:33,860 --> 00:00:37,790
So you see that this is an iterative operation.

11
00:00:37,790 --> 00:00:40,280
So we're doing the same thing over and over again.

12
00:00:40,550 --> 00:00:45,230
So naturally you think of four loops on here.

13
00:00:45,230 --> 00:00:50,660
We we're not simply printing out the items, but also the indexes.

14
00:00:50,660 --> 00:00:56,960
So you start thinking about accessing the indexes with the enumerate function.

15
00:00:57,620 --> 00:01:07,520
Therefore we need something like four index item in enumerate waiting list.

16
00:01:11,190 --> 00:01:14,190
Then we want to construct the string, perhaps the row.

17
00:01:15,680 --> 00:01:17,030
So it's an F string.

18
00:01:18,780 --> 00:01:22,860
It's going to have a variable, which is the index.

19
00:01:23,130 --> 00:01:28,140
And then after the index we have a dot, dot.

20
00:01:28,290 --> 00:01:32,310
And then another variable, which is the item.

21
00:01:34,300 --> 00:01:37,150
Not that items should be capitalized.

22
00:01:37,150 --> 00:01:43,090
So let's add a dot capitalized method here.

23
00:01:44,550 --> 00:01:52,290
So it is possible to add expressions inside these curly brackets and not only variables.

24
00:01:53,670 --> 00:01:57,840
And then if you print out rho and you run the scripts.

25
00:01:58,750 --> 00:02:03,160
You're going to get this zero, send one band to John.

26
00:02:03,520 --> 00:02:06,730
So we need to change 0 to 1.

27
00:02:07,180 --> 00:02:08,080
To do that.

28
00:02:08,320 --> 00:02:10,000
You can just add one.

29
00:02:10,870 --> 00:02:13,480
And all the indices will be shifted by one.

30
00:02:13,480 --> 00:02:15,100
So now we have one, two, three.

31
00:02:15,520 --> 00:02:19,720
But the items are not sorted alphabetically.

32
00:02:19,720 --> 00:02:21,910
So how can we sort a list?

33
00:02:22,240 --> 00:02:24,040
How do we handle this?

34
00:02:24,040 --> 00:02:34,960
Where do we go to find a way to sort lists where the first thing to go to is to check their list to

35
00:02:34,960 --> 00:02:38,470
see if there is a method that modifies the list?

36
00:02:41,180 --> 00:02:45,650
So just scroll and it seems there exists a method.

37
00:02:45,890 --> 00:02:48,050
So perhaps salt is the answer here.

38
00:02:48,350 --> 00:02:58,040
So if you check help list that sort, you see that it's sort of the list in ascending order and returns

39
00:02:58,070 --> 00:02:58,850
none.

40
00:02:59,810 --> 00:03:06,770
So all we have to do is point to the variable that holds the list and then to the source method.

41
00:03:06,770 --> 00:03:08,690
And don't forget the parentheses.

42
00:03:10,760 --> 00:03:16,340
If I execute the script now, you'll see that Ben will be the first one, followed by John, followed

43
00:03:16,340 --> 00:03:17,060
by son.

44
00:03:17,510 --> 00:03:21,710
So the items are sorted alphabetically, successfully.

45
00:03:22,460 --> 00:03:25,460
As you can see, we didn't have to assign.

46
00:03:28,680 --> 00:03:36,130
And you variable to the list because as I explained you in the previous video lists are mutable.

47
00:03:36,150 --> 00:03:40,050
That means the methods will mutate the list.

48
00:03:41,330 --> 00:03:46,280
So the method itself doesn't return anything you see here.

49
00:03:47,800 --> 00:03:49,320
Return none.

50
00:03:49,330 --> 00:03:55,340
So the method doesn't return a list in the case of strings string methods.

51
00:03:55,360 --> 00:03:57,760
They return the new string.

52
00:03:58,030 --> 00:03:59,590
So just to.

53
00:04:01,130 --> 00:04:03,910
Refresh your memory if you had like.

54
00:04:04,510 --> 00:04:05,740
Hello, Dot.

55
00:04:06,490 --> 00:04:07,620
Replace.

56
00:04:07,630 --> 00:04:10,060
So that's a method of a string.

57
00:04:10,150 --> 00:04:19,839
And you replace perhaps O with x, you press enter, you get hell X in return.

58
00:04:19,839 --> 00:04:24,010
So the replace method returns a string of both.

59
00:04:24,220 --> 00:04:25,630
If you have a list.

60
00:04:27,630 --> 00:04:39,240
Such as C, D, and B, and you say dot sort, you press enter, you get nothing as output.

61
00:04:39,240 --> 00:04:41,610
So the third method doesn't return anything.

62
00:04:41,610 --> 00:04:46,770
Otherwise we would have seen that output here in the console.

63
00:04:48,210 --> 00:04:58,200
Now the list itself is solid and it is somewhere floating in this interpreter session and we cannot

64
00:04:58,200 --> 00:05:00,840
access it because we didn't assign it to a variable.

65
00:05:00,840 --> 00:05:07,560
So if you wanted this list to be accessible, you'd want to force assign it into a variable.

66
00:05:07,560 --> 00:05:08,190
Such as.

67
00:05:09,900 --> 00:05:12,120
My list is equal to that.

68
00:05:12,510 --> 00:05:14,610
And then you say my list.

69
00:05:14,950 --> 00:05:22,830
So again, no output is generated because the lists are mutable and the sole method doesn't return a

70
00:05:22,830 --> 00:05:28,130
note, which both my list has now been sorted.

71
00:05:28,140 --> 00:05:33,390
So B is the first letter, C is the second, and D is the last one.

72
00:05:33,840 --> 00:05:39,360
And lastly, how do you sort the list in descending order?

73
00:05:40,290 --> 00:05:47,760
Well, you can see the health file and you will see that there is a reverse equal to false argument.

74
00:05:47,940 --> 00:05:52,200
That means if you say reverse equals to.

75
00:05:53,520 --> 00:05:54,330
True.

76
00:05:56,600 --> 00:05:58,190
And you check my list.

77
00:05:59,760 --> 00:06:05,640
You're going to see that D is the first one, followed by C, followed by B.

78
00:06:06,990 --> 00:06:13,680
So by default the reverse is false, which means the list will be sorted in ascending order alphabetically.

79
00:06:16,020 --> 00:06:18,030
And that was the example for today.

80
00:06:18,420 --> 00:06:19,230
Thanks a lot.

