Welcome to another post about python for machine learning. In this post we will see Collections in python. There are four collections in python
- List
- Tuple
- Set
- Dictionary
Let us discuss about them one by one
List
A list in python is a collection which is ordered and changeable. A basic list is shown below. Lists are defined by square brackets ” [ ] “
thislist = ["apple", "banana", "cherry"] print(thislist)
The output of the program is
['apple', 'banana', 'cherry']
It is interesting to know how to retrieve the data in python
index and range of indexes
see how to access the element using index
thislist = ["apple", "banana", "cherry"] print(thislist[1])
The output of the above program will be banana. list is indexed from 0
let us see range of indexes by using the same program
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(thislist[2:5])
The output of the above program will be [‘cherry’, ‘orange’, ‘kiwi’]
Since we are only covering some important topics in the post,You refer other websites for learning python completely. I am just focusing on the aspects that are needed for machine learning
Tuple
The second collection we are going to see is tuple. The major difference between list and tuple is that list is changeable and tuple is unchangeable but both are ordered.
In python,Tuples are defined by Curve brackets ” ( ) ” as shown below
thistuple = ("apple", "banana", "cherry") print(thistuple)
The output of the program is same as the list
(‘apple’, ‘banana’, ‘cherry’)
We can use the same method as in list to access the elements in tuple.
Sets
Set is a collection in python which is unordered and unindexed. sets are defined by curly brackets ” { } “
Let us see a basic set program
thisset = {"apple", "banana", "cherry"} print(thisset)
The output is the same as the tuple and list. Since sets are unindexed we cannot access the elements by index. So, we need to loop through the set to retrieve the elements. We will see a program to access the data in the set.
thisset = {"apple", "banana", "cherry"} for x in thisset: print(x)
The output is
cherry
apple
banana
Dictionary
A dictionary is a unordered and indexed and changeable. Dictionary is written in python by same curly brackets ” { } ” but has keys and values.
We will see a basic program about dictionary
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict)
The output of the program is
{‘brand’: ‘Ford’, ‘model’: ‘Mustang’, ‘year’: 1964}
Now we shall see how to access data from dictionary
we can access the data in a dictionary using the associated key. The program below will demonstrate how to access the data.
x = thisdict["model"]
The output of the above program will be “Mustang”. How to change the data in a dictionary is as shown below:
thisdict["year"] = 2018
now the data in the dictionary will be changed and year value is changed to 2018.
We are not focusing on this much because we will be using numPy array and Pandas Dataframe for most of our tasks. So we won’t be using python list, tuple, sets, dictionaries. Since it is important to know these basics I’ve posted it.
In the next post we will see about numPy and what we can do with numPy.
Happy Coding