Lists

All right then, we are quite sure on how to use the servo and have a controlled motion as per our needs. Now it's time to move forward and understand another concept that we would be using greatly. It's named arrays. If you have programmed in any other language, you must be familiar with it. But we need to understand a few basics concepts of it, which will make our lives a lot easier. So, let's get started.

First things, first. Arrays in Python are not named arrays, rather it is named as listsList is basically a data structure that can store multiple elements at the same time. The only limitation being is that the elements must be of the same data type. Such as if you are storing integers, then all the values should be int. Similarly, if you are storing a character, then every element of the list should be char. To define a list, all you need to do is name the list such as we have done by doing myList; the name of the list could be anything next we need to tell the compiler that it is actually a list. To do that, we need to put values inside square brackets. It would look like:

myList = [14,35,108,64,9]

One thing to keep in mind is that every value should be separated with commas. Whenever we want to address any single element of the list, we can simply use it by calling their index number. This is based on the position of the element in the list. The index value in Python list starts from 0. So as per the preceding declaration at the index 0, the value would be 14, and at the address 4, the value would be 9. Now when we need to print these elements in between our program, we need to write the following code:

print myList[2] 

Once we write this, the program will print the value of the second value in the list. In our case, it would be 35

Now, this is one way to access the elements of the list; we can however access it in reverse order as well. So, let's say you want to access the last item of the array. Then, we can write the following code: 

print myList[-1] 

This code will return the value of the last element of the array. Now whenever we use the negative values in the lists, then it would start the indexing in the reverse order. So, let's say if we type in print myList[-2], this will give us the value of the second last value in the array. One thing to remember in this whole schematic is that the numbering would start from 0, whereas when we start it in the reverse order, then the numbering would start from -1.

Python is really interesting and quite simple if you know the right tools. The developers of Python have included some really helpful functions that can be used over lists. So, let's go and explore them a bit. 

The first one is to add elements to the array. For this, we use a function named append(). What the append() function does is that it adds the value, which would want at the end of the array. So, write the following: 

myList.append(45)

What this would do is that it would add the element 45 at the end of myList. So now the list would be as follows: 

myList = [14,35,108,64,9, 45]

Easy, isn't it ? But what if you want to add an element in between the list? Obviously, the developer won't leave you dry. They have included a function for that as well; it's named insert(index, element). Now whenever you are using this function, you need to make sure that you mention the index where you want this element to be and second, the element that you want to put. So it looks something like this: 

myList.insert(3,23)

When you have used this function, the array will look as follows: 

myList = [14,35,108,23,64,9,45]

Obviously, whenever the developer has given the function to add an element, then they would have certainly given a function to remove the elements as well. But the trick is that you can do it two ways. First, the common way. We simply select the index number and delete it. We are going to do it now:

del myList[2]

Now what this will do is that it would delete the second element of the array, so after doing this operation, the array will look like this:

myList = [14,35,108,64,9,45]

But now here comes the real trick; you can also delete the element by simply specifying the element. This is how it's done:

myList.remove(9)

Now the moment you do this, it will find wherever the element 9 is in your list and delete it from the positions. So you don't have to care about where the element is; this function will say, I will find you and I will kill you! 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.37.68