Table of contents
List
Lists are just like arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.
The Lists are written in square brackets and has values.
Syntax:
variable_name = [value, value, ...]
fruits = ['orange', 'apple', 'pear']
Check a few examples in the screenshot:
Dictionary
Dictionaries are used to store data values in key:value
pairs. A dictionary is a collection that is ordered, changeable and does not allow duplicates.
Dictionaries are written with curly brackets and have keys and values.
Syntax:
variable_name = { "key" : "value", "key" : "value", ... }
dict_of_cloud = {
"aws": "Amazon Web Services",
"azure": "Microsoft Azure",
"gcp": "Google Cloud Platform",
}
Check a few examples in the screenshot:
Tuple
Tuple is a collection of Python objects much like a list but Tuples are immutable i.e. the elements in the tuple cannot be added or removed once created. Just like a List, a Tuple can also contain elements of various types.
Tupples are written with round brackets containing values. If there is only one single value then the comma (,) has to be written followed by the value.
Syntax:
vari_name = (1,2,"hi")
vari_name2 = (1,)
tupNum = (1, 2, 3)
Check a few examples in the screenshot:
Set
Sets are used to store multiple items in a single variable. A set is an unordered collection. It cannot store duplicate values.
Sets are written with curly brackets.
Syntax:
Variable_name = {"value", "value", "value", ...}
set_of_cars = {"Audi", "BMW", "Volvo", "Audi"}
Check a few examples in the screenshot:
Difference between List, Tuple and Set
List | Tuple | Set |
Mutable | Immutable | Mutable |
Ordered | Ordered | Unordered |
Collection of uncategorized data (no data type) | Faster than list | No duplicate values |
Unpackable - put data in variables like: a, b, c = (10,20,30) a=10, b = 20, c=30 | Can perform union, intersection, give difference, subset etc. | |
Syntax: [value, value, …] | Syntax: (value, value, …) | syntax: {value, value, …} |