For this round we studied about new theme are list and dictionary. What do you think list mean? Do you have any example? List is the collection of item that have prepare in order and changeable. In the “list” we use the [ ] square bracket and we could also duplicated, which mean that we can repeat it again and again. For example:
Change item value
thislist = [“mango”, “jackfruit”, “coconut”]
print (thislist [1] )
jackfruit
The computer will print “ jackfruit” because if we count would start from one, two, and continues, but for computer begin with zero, one, two, ect. In the variable “print (thislist [1] ), which mean that we need a computer to print the fruit in the list is the first index. So the computer begin with zero with mango, first of jackfruit, and second is coconut.
Check if item exists
thislist = [“mango”, “jackfruit”, “coconut”]
if “mango” in thislist:
print ( “Yes, ‘mango’ is in the fruit list”)
For the check if item exists use to make sure does the stuff in fruit is the same as “if” variable ask like if “mango” in thislist:
print (“Yes, ‘mango’ is in the fruit list”)
So if the mango is in the fruit list the computer would print Yes, mango is in the fruit list. It could show us more that mango is already exist in the fruit list. Moreover, there is also the way to add more item into the list, which is call…
Add items insert
thislist = [“mango”, “coconut”, “orange”]
thislist.insert(2, “banana”)
print(thislist)
As you can see if we aim to add more item into the list of variable. First, we list down all of fruit list name, then in the thislist.insert (2, “ banana”) is show that you want to add banana into the variable fruit list, also we want the banana is in the second place. So we write number two in the parentheses near to banana to make it’s into the second place.
After we could get some idea about list and now I will share with you some information about the dictionary. Dictionary is different from list because it is the collection item that unordered and changeable, but there isn’t allow to duplicate and it use curly bracket { }. For example
In the box above is an example of dictionary. This is dissimilar to the list because in the dictionary we use the curly bracket beside parentheses. There is also brand, model, and year, which is called Key. Moreover, the Toyota, Prius, and 2017 is called Value. Mostly, we use dictionary to gather items that are unordered and changeable.