Round 4, Multimeadia “How to Use a (Python List)”

Disclaimer: This is base on what I understand during the Class and it’ not covered all that I have learned. Just meant to give a short quick tip.

“Can someone help me put this apple, banana, pineapple, mango, longans, and grapes in this basket for me please?” It is an easy and simple task to do in the physical world but how does someone put that fruit list into a python language, let it reads and use it for their action? Well, you are have come to the right spot!

First of all, list in python is a code that just contains any data types: strings, integers in a pair of square bracket [….] and stored them in a variable as a database. It is in order; not messy and we able to change what should be inside the list.

Code Form:
The code form of a list looks like this:

1. refrigerator = [“apple”, “banana”, “pineapple”, “mango”, “longans”, “grapes”]

Just like that!
All of the fruits are the data types, the basket is the parentheses and the list is stored in the refrigerator which is the variable.

How to use the list:
Let’s think! It turns out that the uses of it are just the same as using any type of variable in python. You can print it out like this:

1. refrigerator = [“apple”, “banana”, “pineapple”, “mango”, “longans”, “grapes”]
2. print(refrigerator)

Computer: ('apple', 'banana', 'pineapple', 'mango', 'longans', 'grapes')

Like that!

How to add an item in a list:
Do also want to add more items into your list and use it later. You just have to add a new line of code like this:

refrigerator.append ….
1. refrigerator = [“apple”, “banana”, “pineapple”, “mango”, “longans”, “grapes”]
2. refrigerator.append “durian”
3. print(refrigerator)

Computer: ['apple', 'banana', 'pineapple', 'mango', 'longans', 'grapes', 'durian']

Remember: This type of code can only add one item at a time! If you have a lot of items that you want to add you might need to use another type of code.

How to take out an item in a list:
How about one time that you want to remove an item from your list when you don’t need it. Again, you just have to add a new line of code like this:

refrigerator.remove ….
1. refrigerator = [“apple”, “banana”, “pineapple”, “mango”, “longans”, “grapes”]
2. refrigerator.remove(“durian”)
3. print(refrigerator)

Computer: ['apple', 'banana', 'pineapple', 'mango', 'longans', 'grapes']

Remember: This code also has the same rule as the adding item into a list!

Leave a Reply

Your email address will not be published. Required fields are marked *