[008] Introduction to Python Basics: Matrix, List Methods 1

Koay Yong Cett
6 min readJun 27, 2020

--

Photo by Kelly Sikkema on Unsplash

Let’s talk about the matrix. It is a way to describe 2D lists or multidimensional lists.

  • Matrix looks something like this. It’s an array or list with another array or list inside of it. What happening here? Well it’s a two dimensional array.
  • We have the main array and then the three sub arrays. This is a two dimensional and we also can have multiple ones. For example, we may also have another array inside this array.
  • Why is these important? Well matrices appears a lot in topics like machine learning or image processing. For instance, a computer doesn’t really know and understand what a photo of dog is. The only things that machine or computer can understand is zeros and ones.
  • Let’s say we have a photograph of letters X. Well, a computer can understand an image based on pixels on screen. If this figure represented a tiny little pixel here, it will show a X in the screen.
  • Matrices allow the computer to perform heavy calculations and how computer work under the hood when it try to display something.
  • You can access the a multi dimensional list by using the brackets. The first bracket is the row and the second bracket is column. This will yield a 5 in the result.

List Methods:

  • Let’s learn some actions that we can perform on lists which is the list methods. This an interesting part as you can perform a lot of actions on lists.
  • We learned about built in functions that come with Python. The len function is used to calculate the length of a list. Remember that a length is the actual length and does not start counting from zero.

List get really powerful when it comes to methods:

  • Thus, instead of built in function, a method is a new action that is owned by something and specified to a data type.
source: https://www.w3schools.com/python/python_ref_list.asp
  • Here, we can see that there are actually few methods that we can use in Python.

Append:

  • The ways that we use methods is by adding dot after a list. The first method that we will be talking is append.
  • Here, I want to append the 100 to the end of the list. We create a new list and do append on the original list. Then, we print the new list. Why we get none?
  • We still able to get the list when we print basket. It’s look like the list basket was appended with 100. However, the result is none when we assigned to the new list.
  • This is because append changes the list in place. This means that append doesn’t produce a value. It is just going to append a hundred to this basket variable list and not producing result.
  • We just need to change few things in the line and now we get the list that we wanted. After we appended to the basket, then we can assign so that new list points to basket list which we modified.

Insert:

  • Well, we have something called insert and it gives an index and object. Thus, we can insert something anywhere we want in the index.
  • In the figure above, I add 100 to the index of 3.
  • As you can see here, we have something happen(result:none) that is similar to length function. It inserts and modifies the list in place where it doesn’t create a new copy of the list at all.

Extend:

  • There is another method called extend. Instead of an actual item or object, extend takes something that we called as iterable. It is something that you can loop over or can iterate over which is a list.
  • Let’s extend the list with a 100 or 101. Once again, there is no output in our new list. It just modifies the list in place and extends our list.

Pop:

Let’s talk about the remove method in the list. There are few interesting that we can do.

  • The pop method will pop off/remove the last number in the list which is 101 since we add 100 and 101 to the list.
  • With two pop(), both the 100 and 101 is removed from the list.
  • When we do pop(0), it removes the item in the index. Therefore, the pop can remove an item according to the list index.
  • When we pop(4), we get 5 and why? Pop will return whatever that you have just removed in our case which is 5.
  • There is some other method that gets none. Why? That is because when a method doesn’t return anything it returns None.

Remove:

  • There is also remove method, we give the value that we want to remove.
  • The remove(4) will remove the 4 in the list. Here, we notice that difference between the remove and pop. The remove will remove the value that we give it, while we will give the pop the index of list where we want to remove.
  • we still get none for the new list. Thus, remove doesn’t return a value and it just simply changes whatever list we give it.

Clear:

  • The clear method also doesn’t return any value and it yielded none for the new list.
  • Clear yields a empty bracket when we print basket.The clear is used to remove whatever in the list and completely clear it just as the name suggests.

Thanks for reading my stories 😃

--

--

Koay Yong Cett
Koay Yong Cett

Written by Koay Yong Cett

Every stories I shared is based on my personal opinion. Interest in ethical hacking and penetration testing. Thank you.

Responses (1)