Hello, my name is Stefan from Oxford Academy.
And in this section you will learn about lists.
What lists are and how to start working with the elements in a list.
So let's allow you to store sets of information in one place, whether you have just a few items or
millions of items.
So lists are one of the python's most powerful features, readily accessible to new programmers, and
they tie together many important concepts in programming.
So what is a list?
A list is a collection of items.
In a particular order.
You can make a list that includes the letter alphabets.
Digits here.
Letters.
A, B or.
A the C here ads here.
You can create a list of numbers here.
One two.
Three, four.
Five.
You can also create.
List of characters here.
Which you will learn all of this in this section so you can put anything you want into list and the
items in your list don't have to be related in a particular way, right?
So because a list is usually contains more than one element, it's a good idea to make the name of your
list plural.
Such as?
Uh, for example, instead of writing.
Oh, sorry.
So instead of writing your list variable name letter.
You can write letters because it's a more good practice here.
So in Python.
Square braces.
This square braces.
Indicator list and individual elements in the list are separated by commas.
Separated by commas.
So let's actually create a.
Example, a list that contains a few kinds of courses.
Right?
So.
So course topics here.
It's going to be here.
We have braces, brackets.
So now we're going to car.
As you can see, we are getting an error because we we need to enter a strings car.
So we use single quotes.
Car.
And here we will also add Android Kotlin.
We will also add Java.
Python.
Digital forensics.
Actually, let's decrease the font size a little bit maybe.
Yeah.
And digital fonts is okay for now.
So after that we will print the course topics list.
And as you can see here, this is the output.
So if you ask a python, print the list that's Python retains its representation of a list, including
the square braces here.
So because this is the output you want to use, you want users to see.
Let's learn how to access the individual items.
For example, Car, Android, Kotlin, Java, Python and digital forensics.
Let's try this now.
So lists are ordered collections so you can access any element in a list by telling Python the position
or index of the item desired.
So to access an element in a list, write the name of the list followed by the end index of the item.
So now there's an interesting thing I want to.
Sorry.
Firstly, it's actually get.
And so in order to access this list, we will write, print or actually instead of print, we can also
write.
Yeah, we will write print inside this list.
We will get course topics here.
Course.
Topics.
And here we will use.
Braces.
Brackets like this.
Let's actually use this different color so I can show you.
Here.
This is the braces.
So here we will add the nth element.
So.
And.
Element.
From list.
So in this case.
For example, if we want to access the cars, the car is going to be zero.
Android Kotlin is going to be remember, let's let's actually use different color.
So.
The car is going to be.
Zero.
Android Kotlin is going to be one.
Java is going to be two.
Python is going to be third element in the list.
Digital forensics is going to be fourth element in the list.
So if you want to access the cars, we will write zero.
If you want to access the Android Kotlin, we will write one here.
But if we want to access Java, we will write two.
If we want to access Python, as you know, we will write three because the python is the third element
of the list.
So list starts from the zero one, 234.
So if we want to access the digital forensics, we will write the fourth.
So.
So here we will write course, print course topics.
Zero.
And as you can see, it's a car.
Android.
Kotlin.
Java.
Python.
Now python and let's five ops and let's actually try Python now.
So.
I feel Bradford is going to be digital forensics.
As we know, as I showed you earlier in this lecture.
One, two, three, four, five.
Right.
Fifth element of the list in this case.
Zero, one, two, three, four from the index.
So here, if you write 5 or 10, in this case, we don't have that range or that index.
And as you can see, list index out of range because we don't have that much item.
So this is the result you want to users to see in this case, for example, Java, right.
So nearly formatted output.
So you can also use the string methods from previous lectures on any element.
This list, for example, course topics that we will write here.
So we have l strip lower upper let's upper and here, as you can see, we have uppercase Java.
So Python here considers the first item.
Actually, let me let me I want to explain this later because most new programmers.
Have problem with this index thing here.
So Python considers the first item in a list to be at a position zero.
In the end, most programming language is actually considered like this.
So here the car is worth zero, right?
Zero.
And this is not the first car is a zero at the index.
This is true of most programming languages.
And the reason has to do with how the list operations are implemented at a lower level.
So if you are receiving an unexpected results.
Ask yourself if you are making a simple but common off by one error.
So the second item in the list has an index of.
One of.
The second item in the list has an index of one.
Here.
So let's make this.
And the third item.
As an index of two again.
The fourth item.
As the index.
Of three.
And the fifth item has an index of four.
That's it.
So this code here.
For example.
Now let's.
Here.
So.
Now we will print here.
Zero.
Let's actually copy this again and again.
Four.
Five.
So.
Zero one, two, three, four.
And here, as you can see here, this is our output, right?
The 012 here, zero goes to here.
The one goes to here, the three goes to here, the second line index.
Of course, the fourth index goes to here.
And digital forensics, the last index fourth goes to here.
So the python has a special syntax for accessing the last element in a list if you ask for them at indexed
index Z minus negative one here.
Let's let's try this.
This is the weird, right?
And as you can see here, negative one.
If you ask for the item at the index negative one python will always returns the last item.
Negative one returns negative one.
Returns.
Returns the last item.
Yeah.
So here the the code returns the digital forensics and this syntax is quite useful because you will
often want to access the last items in a list without knowing exactly how long the list is.
So the convention extend other negative index values as well, and the index minus two returns the second
item from the end of the list.
Yeah.
And the index minus three returns the third item from the end and so forth.