In the previous tutorial, we discussed if-else, for and while loop. We are back to cover the range as specified in the previous tutorial, list, its operation, and functions. So let’s begin.
DO CHECK THIS VIDEO!
The range function creates a sequential list of numbers. For example,
num = range(10)
Here all the integers are printed up to 9. If the range is called with one argument say range(5), then it produces an object with values from 0 to that argument 5. However, if there are two arguments say range(3,8), then it produces values from the first (3) to second (8). Finally, if the range has 3 arguments say (1,20,2), then the 3rd one (2) determines the interval of the sequence. Also, it must be an integer.
Next one is a pretty important topic which is List. It is a type of object used to store an indexed list of items. It is created using square or 3rd brackets , with commas separating each item. Can you obtain an empty list? Of course, simply with an empty pair of square brackets. One cool feature is that list can contain items of the same type, different type, and even lists nested within it. You can use this feature to perform 2D grids as Python lacks multidimensional arrays. Indexing strings behave as though you are indexing a list containing each character in the string, but it is not possible with integers.
Suppose you want to print the first 4 elements in a list, then write print(list_name[:4]) because indexing starts with 0 from left to right. Now if you start indexing from right to left, then it starts with -1. So from this same list, if we write print(list_name[-3:]), then we will be getting the last 3 items.
In the operations of the list, you can reassign an item by selecting the required index. A list can be added and multiplied in the same way as strings. To check if an item is present in the list, the in operator is used. It returns True if the item is present one or more times in the list, else False. The in operator is also used to determine whether or not, a string is a substring of another string.
Time for some list functions. If you want to add an item, then write list_name.append(8) and 8 is added at the end of the list. The dot (.) before append is because it is a method of the list class. The len gives the length of the list. Now, you can see that I haven’t used dot because unlike append, len is a normal function and not a method. Again, insert method is similar to append except that you can add a new item at any position in the list.
Time for some honorable mentions in list functions:
1. max(list_name) : Returns the list item with maximum value
2. min(list_name) : Returns the list item with minimum value
3. list_name.count(item): Returns a count of how many times an item occurs in the list
4. list_name.index(item): Returns the index of the item from the list
5. list_name.remove(item): Removes an item from a list
6. list_name.reverse(): Reverses the objects in the list.
So I hope you have enjoyed this tutorial. Don’t forget to subscribe to our channel if you have found this tutorial interesting. Share this and I will catch up with you guys in the next one. Peace! 🙂