Okay, big topic ahead. Let's talk about lists. So what exactly is a list? A list is actually a sequence consisting of elements separated by comma, the sequence of elements is enclosed by square brackets. You can have any data types as elements of a list. This means strings, numbers, topples, or even other lists.
And a list may have any number of elements. Similarly to strings, which can also be regarded as sequence of characters. Lists are indexed, meaning each element has a certain position inside the list. Starting at index zero. You can also use the Len function to see the number of elements in the list and slices to extract only a portion of a list. As opposed to strings.
Lists are immutable, meaning that you can modify your list by adding or removing elements and we will see and use that a lot throughout this course. Let's create our first list in this course, we will name it list one, and it will be initially empty. So list one equals open and close square brackets. So in order to have an empty list, you just have to type in the square brackets. And that's it, nothing else is needed. To check that this is indeed the list.
Let's use the old type function on list one. So type of list one. And indeed, we have class list. Now let's add some elements to our list because an empty list is really no fun. So list one with these elements is now a list with some strings and numbers. Now let's remember the Len function from the string section and use it on our list.
So Len of list one returns six. This means that our list has six elements, and that is correct. Now let's see how we can access elements inside the list. Well, the same way as we did with characters in a string using indexes. So let's say list one Zero, return Cisco because this is the first element in the list. list one of one will return of course, the second element in the list the string Juniper, list, one of minus one returns the last element in the list minus 11.
And list one of minus two will return 10.5. As with strings, if we enter an invalid index, we will get an index error in return, stating that the list index is out of range. So let's try this list one of 100. Okay, so Indeed, we got an index error. To check that lists are indeed mutable. Let's try to replace an element in this list.
So considering our list list one list, one of two will return a via this string in our list. Now let's replace it with another networking vendor. Let's say HP, we just have to use the equal sign to modify this. So list one of two equals and now the elements we want to replace a vial with So hp. Now if we check list one again, you can see that the result we were looking for has been achieved. So plain and simple.
This is how you can update a list, just type in the name of the variable. And in between square brackets, you have to insert the index at which you want the replacement to be made. Finally, following the equal sign, just enter the new element and that's it, you're good to go. Now as a short review, the first element of a list is always positioned at index zero, and the last element corresponds to index minus one. Also remember that lists are mutable, unlike strings or numbers. I hope this is clear and now we can move on to list methods.