Collections in Python are containers that are used to store collections of data, for example,
list, dict, set, tuple etc. These are built-in collections. Several modules have been developed
that provide additional data structures to store collections of data.
One such module is the Python collections module.
Some of the important collections in python
mylist = ["red", "green", "blue"] mylist = ["red", "blue", 1 , True, 3, False] print(mylist) #Output: ['red', 'blue', 1, True, 3, False] print(type(mylist)) #Output: < class 'list'> |
Lists are created using square brackets [ ] and have different data types
lists are like an array in other programming languages
Lists are ordered, mutable & indexed
For more detail, please refer Python Lists
myset = {"red", "green", "blue"} |
Sets are written with curly brackets { }
A set is an unordered, immutable & unindexed and does not allow duplicates
mytuple = ("red", "green", "blue") |
Tuples are written with round brackets ( )
A tuple is an ordered, immutable & indexed and allow duplicates
thisdict = { "name": "John", "state": "New York", "dob": 1990 |
Dictionaries are written with curly brackets { }, and have keys and values
A dictionary is an insertion ordered (from python version 3.7), changeable and does not allow duplicates
A dictionary also called as dict and dict value has any data type but the key should accept immutable data types like str, bool, None...
If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!