Hi.
Welcome back.
In this video, we will create a simple program.
We will start with this list, and then we will create a program which outputs this three lines here.
So the program will sort of the items alphabetically, as you see, Ben is the first one, John.
And then comes son.
And we will also place a number before each of the items here in the output.
So let's do that.
How can we do this?
So you see that this is an iterative operation.
So we're doing the same thing over and over again.
So naturally you think of four loops on here.
We we're not simply printing out the items, but also the indexes.
So you start thinking about accessing the indexes with the enumerate function.
Therefore we need something like four index item in enumerate waiting list.
Then we want to construct the string, perhaps the row.
So it's an F string.
It's going to have a variable, which is the index.
And then after the index we have a dot, dot.
And then another variable, which is the item.
Not that items should be capitalized.
So let's add a dot capitalized method here.
So it is possible to add expressions inside these curly brackets and not only variables.
And then if you print out rho and you run the scripts.
You're going to get this zero, send one band to John.
So we need to change 0 to 1.
To do that.
You can just add one.
And all the indices will be shifted by one.
So now we have one, two, three.
But the items are not sorted alphabetically.
So how can we sort a list?
How do we handle this?
Where do we go to find a way to sort lists where the first thing to go to is to check their list to
see if there is a method that modifies the list?
So just scroll and it seems there exists a method.
So perhaps salt is the answer here.
So if you check help list that sort, you see that it's sort of the list in ascending order and returns
none.
So all we have to do is point to the variable that holds the list and then to the source method.
And don't forget the parentheses.
If I execute the script now, you'll see that Ben will be the first one, followed by John, followed
by son.
So the items are sorted alphabetically, successfully.
As you can see, we didn't have to assign.
And you variable to the list because as I explained you in the previous video lists are mutable.
That means the methods will mutate the list.
So the method itself doesn't return anything you see here.
Return none.
So the method doesn't return a list in the case of strings string methods.
They return the new string.
So just to.
Refresh your memory if you had like.
Hello, Dot.
Replace.
So that's a method of a string.
And you replace perhaps O with x, you press enter, you get hell X in return.
So the replace method returns a string of both.
If you have a list.
Such as C, D, and B, and you say dot sort, you press enter, you get nothing as output.
So the third method doesn't return anything.
Otherwise we would have seen that output here in the console.
Now the list itself is solid and it is somewhere floating in this interpreter session and we cannot
access it because we didn't assign it to a variable.
So if you wanted this list to be accessible, you'd want to force assign it into a variable.
Such as.
My list is equal to that.
And then you say my list.
So again, no output is generated because the lists are mutable and the sole method doesn't return a
note, which both my list has now been sorted.
So B is the first letter, C is the second, and D is the last one.
And lastly, how do you sort the list in descending order?
Well, you can see the health file and you will see that there is a reverse equal to false argument.
That means if you say reverse equals to.
True.
And you check my list.
You're going to see that D is the first one, followed by C, followed by B.
So by default the reverse is false, which means the list will be sorted in ascending order alphabetically.
And that was the example for today.
Thanks a lot.