African Agility Week 2 assignment
Difference between the Four collection Data types in the Python programming Language.
The Four collection Data types in the Python programming Language are List, Tuple, Set and Dictionary. The differences between them are explained below.
1. List
Lists are used to store multiple items in a single variable. They are created using the square brackets []. List items are ordered, changeable and allow duplicate values. When we say the items are changeable, it means we can change, add and remove items in a list after it has been created and when they are ordered it means they are indexed.
List items can be of any data type or contain different data types. Indexing in list, the first item is indexed [0] while the second is indexed [1]. From example 1 below, orange would be indexed as number 0, mango is number 1 and apple is number 2.
Example 1
list1 = ["orange", "mango", "apple"]
list2 = [1,8,3,7,4,9]
list3 = [False, True, False]
print(list3)
#Result [False, True, False]
Example 2 - Different data types
list1 = [55, False, "xyz", 10, "banana"]
It is also possible to use the list() constructor to make a list as shown below in example 3 with the double round brackets.
Example 3
thislist = list(("orange", "mango", "apple"))
print(thislist)
#Result ['orange', 'mango', 'apple']
From Python's perspective, lists are defined as objects with the data type 'list':
From example 1 above:
print(type)(list1))
#Result <class 'list'>
2. Tuple
Tuples are used to store multiple items in a single variable. They are written with round brackets (). Tuple items are ordered, allow duplicate values but are unchangeable. Tuple items are indexed, this is why they can have items with the same value. The first item is indexed [0] and the second is indexed [1] just like the list items but they are unchangeable; we cannot change, add and remove items in tuple after it has been created.
Example 1 - Different data types
tuple1 = ("orange", "mango", "apple")
tuple2 = (1,8,3,7,4,9)
tuple3 = ("False", "True", "False")
Example 2
mytuple = ("orange", "mango", "apple")
print(mytuple)
#Result ('orange', 'mango', 'apple')
To create a tuple with only one item, a comma has to be added after the item, otherwise Python will not recognise it as a tuple.
Example 3
thistuple = ("mango",)
print(thistuple)
#Result mango
#thistuple NOT a tuple
It is also possible to use the tuple() constructor to make a tuple as shown below in example 4 with the double round brackets.
Example 4
thistuple = tuple (("orange", "mango", "apple"))
print(thistuple)
#Result ('orange', 'mango', 'apple')
From Python's perspective, tuples are defined as objects with the data type 'tuple':
#Result <class 'tuple'>
3. Set
Sets are also used to store multiple data just like List and Tuple but items in Sets are unordered (they are not indexed), unchangeable and do not allow duplicate values. They are written with curly brackets {}. When we have items in set with the same value, the duplicate value will be ignored. For set, True and 1 are seen as the same value while False and '0' are the same . Set items can be of any data type and can also contain different data types e.g a set with strings, integers and boolean values.
Example 1 - Different data types
set1 = {55, True, "xyz", 1, "banana"}
print(set1)
#Result {true, 55, 'banana', 'xyz'}
Example 2 - Ignores duplicate value
myset = {False, True, True}
print(myset)
#Result {False,True}
It is also possible to use the set() constructor to make a set as shown in example 3 below with the double round brackets.
Example 3
thisset = set(("orange", "mango", "apple"))
print(thisset)
#Result {'orange', 'mango', 'apple'}
From Python's perspective, sets are defined as objects with the data type 'set':
#Result <class 'set'>.
4. Dictionary
Dictionaries are used to store data values in key:value pairs and as such they are referred to using the key name. They are written with curly brackets and the items are ordered, changeable just like List. When we say ordered, it means that the items have a defined order, and that order will not change as such, they are indexed. In dictionaries, we can change, add and remove items after the dictionary has been created so they are changeable but ordered. Dictionaries do not allow duplicates even though they are indexed by keys.
Example 1 - Different data types
thisdict = {"brand" : "itel", "colour" : "red", "year" : 2000}
print(thisdict["brand"])
#Result ford
#Note that there must be quotation marks when printing 'brand'.
Example 2 - Ignores duplicate values
thisdict = {"brand" : "itel", "colour" : "red", "year" : 2000, "year" : 2022}
print(thisdict)
#Result {'brand' : 'itel', 'colour' : 'red', 'year' : 2022}
From the above duplicate value overrides the existing value.
The values in dictionary items can be of any data type.
Example 3
thisdict = {"brand" : "itel", "colour" : ["red", "pink, "purple"], "plastic": false, "year" : 2000}
It is also possible to use the dict() constructor to make a dict as shown in example 4 below with the round brackets.
Example 4
thisdict = dict(name = "Peter", age = 25, region = "North")
print(thisdict)
#Result {'name' = 'Peter', 'age' = 25, 'region' = 'North'}
From Python's perspective, dictionaries are defined as objects with the data type 'dict':
#Result <class 'dict'>
In conclusion, the Four built-in data types used to store collections of data can be summarised as follows:
List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable, Allows duplicate members.
Set is a collection which is unordered, unchangeable, and unindexed. Do not allow duplicate members.
Dictionary is a collection which is ordered and changeable, Do not allow duplicate members.
Please like, comment and share. Thank you!