Python for Beginners - 07 | Built-in Data Types | [Lists]

preview_player
Показать описание
Welcome to E-Skillation. [List in Python]

In our previous videos we went through Numbers and Strings from a data type perspective, understood different types of numbers and strings, all escape characters in Python, learnt immutable nature of both numbers and strings.

In this video, we will learn about Lists, one of the most used data types in Python.

A list is a group of multiple items separated by a comma where the group is enclosed with square brackets. In this example, we have created a simple list of numbers separated by a comma and enclosed in square brackets. Well, we may wonder how many items of these can we fit inside a list? Python does not have a built-in limit to the length of a list. However, it depends on our hardware like memory capacity, CPU architecture whether if its 32-bit or 64-bit etc.
The next question that comes to our mind is, can we include items with different data types in a list? The answer is yes, for example if I take just numbers, in a list we can include an integer, a floating point, a hexadecimal, binary all in the same group. Similarly, we can also include strings in this. Just for fun let me add a smiley using a Unicode string. That’s amazing! That’s not it, we can include another list inside a list, tuples, sets and all other data types that we will learn in our upcoming videos. So, we can consider that Python does not imply a data type restriction on a list.
How do we access these items individually for any operation? As we saw in strings, we can use index in list also. We did not talk much about indexes while exploring strings so let us take a quick look at how index works. FYI, this concept applies to string as well. I will consider both string and a list to explain and the functionality of indexes are same. I will take a string value Python for Beginners and I will take a list of some random alphabets. Each character in a string or each item in a list has an index. Which starts sequentially from 0 up to the count of the characters in these values. You could just consider these as some sequential addresses to our characters and items. That means, the address for us to get to letter f in this string is 7. Similarly, the address to get to letter G in this list is 0. Syntactically in our script if we create these variables, we can just use square brackets next to the name of the variable and mention the index or the address that we saw and we can get the item or character at that index.
At times we must have to access characters the other way round, like how we have our index starting from 0 when we are going from left to right, from right to left we have another index which is negative and starts from -1 and it goes -2, -3 etc up to the count of the characters or items in these variables. So now, if we need to access the letter f we can either use index 7 or -13 and it will lead us to the letter that we are looking for. It applies the same to list were to access this item I could either use the index 0 or -5.
Now let us talk about list’s mutability nature. But before, let us recap on numbers and strings. In Numbers we saw that when we are overwriting an existing variable, the memory location of the variable was different from the earlier. In other words, it was creating a new object instead of overwriting the existing. Similarly in strings we learnt that the string itself will have a separate memory location and each character in a string will be stored separately at different locations, whenever we are trying to update an existing string, the location of the updated string was new which means the object of the string was getting created again. However, internally the characters were pointing to the same location only if a character was also present in our variable before the update. Please do watch Strings Part 1 for detailed explanation on this.
Let us see how memory location works for a list. I will create a list of characters from the phrase Python for beginners. If you notice, I am including spaces also in a list. Let us check the memory location of the list itself and for some of the items from this list by their indexes. The functionality is same as strings where the whole list itself is pointing to 1 memory location and each item is stored at different locations. Let us also check if same item points to 1 location itself. When we see the locations of letter n we can make sure that till now everything is same as strings. Now let us see what happens when we are updating the list. Unlike in string where we cannot update a value at a specific index, in list we are allowed to do that. Let me update the space to an underscore using the index of the first space. Now if I look at the location of the list, we see that its location is same as before. Here is the difference between strings and list. And hence lists are called mutable, which means it will make updates to the same object rather than creating a new object.
Рекомендации по теме
welcome to shbcf.ru