Python: Lists 5/12
The main difference between tuples and lists is that it allows you to modify your data once created.
The tuples are of immutable data.
The lists can also contain any type of data, text strings, number, polians, they can even have lists, that's right; lists can have lists within them, in fact also tuples can store lists.
To declare a list we choose a name.
As the tuples originate different types of data, the difference is that the data were in brackets and not in parentheses as we did with the tuples, then in parentheses we started adding items.
Example.
My_list = [index zero, 24, 13.14, true, [1,2]]
The elements are accessed in the same way as in the tuples, by means of their index, we simply need the variable and the index that we want to show us.
Variable + index that we want to show.
This way we can access our lists.
For example:
My_list [Index zero, 24, 13.14, True, [1,2]]
Print (My_list [True])
True.
We can put negative values in the indexes and it works the same as in the tuples, from the end to the beginning, and the -1 corresponds to the last element.
My_list = [Index zero, 24, 13.14, True, [1,2]]
Print (My_list [3])
True
Print (My_list [-1])
[1,2]
It corresponds to the last element as we can see here.
We can also use the slice, as we use it with the tuple and it shows us our slice.
Print (My_list [1: 3])
[24, 13.14]
It shows us our slice cutting from 1 to 3, therefore it only shows us the 1 and the 2, and the 3 is no longer taken into account.
But the lists are not immutable compared to the tuples, with the lists we can modify the data that is already created. The only thing that we have to use is the variable, and in square brackets enter the index that we want to modify, for example if we want to modify our whole value 24 we place the index 1 that is the one that corresponds and we place the new assigned value.
I will assign false since it is not necessary to assign values of the same type, we can enter a different value.
My_list [1] = False, in this case we take a whole number, but we change it to a different one. We observe if the 24 was modified by the false:
Print (My_list)
My_list = [index zero, 24, 13.14, true, [1,2]]
We can even modify several data using the slice by slicing.
We have to modify the three values of means, false, 13.14 and True for other values, we simply use the variable and in the slice we need a start index and an end index, therefore we want that it covers us from index 1 to 4, since the 4 is not taken into account we assign new values in brackets.
My_list = [index zero, 24, 13.14, true, [1,2]]
My_list = My_list = [1: 4] = [8, 9, 10]
Print (My_list)
8, 9, 10 [1,2]]
Another very interesting operation that we can do with lists is to add new items at the end of the list.
For this we add a point and write append to our variable, this writing after the point is known as a method.
My_list.append
This method allows us to place a new method to the final item of our list, in parentheses we add the new item that we want, it can be a number, it can be a boolean, a real number, a string.
I will be doing a new post, to explain in more detail what is the Append, because I do not want to extend the publication much, I hope you have loved the tutorial.
Good information post