Secondary Data Types
Sequence
List
Written as a list of comma-separated values (items) between square brackets ([ ])
Items in a list need not be of the same type
sample_list = [] # declaring a list
sample_list = [1,2,3] # same type values in list
sample_list = [1, 'mixed', True] # mixed list
sample_list = [1, 'mixed', True, [1, False], {'dictionary': 'this is an example'}]
# even other object types like tuple, list, dictionary or even function,
# class instance etc can also be stored within a listAccess an element of a list
sample_list = [1, 'mixed', True, [1, False]]
# can access the content using index, index starts from 0
sample[2] # it gives True
sample[-2] # it also gives True (2nd element from the end)modifying elements of the list
Operations on the list:
Tuple
A tuple is a collection of objects which ordered and immutable
Tuples are sequences, just like lists.
The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses (), whereas lists use square brackets
Range
Generally used for loop
Set
Sets use parentheses {}
Can have any number of elements, and any type of element except mutable one
A set cannot have mutable elements like lists, sets, or dictionaries as its elements
Have only unique values, no duplicates allowed
set is unordered (order is not important and is not preserved), hence indexing is not important
Set operations such as union, intersection, and difference are also possible
Mapping
Dictionary
A dictionary is made up of different keys and values
Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces
keys can not be a duplicate in a dictionary
Keys are not ordered in the dictionary (this is an important point to note)
Nested Dictionary has a lot of applications
Accessing Values in Dictionary
Listing all keys (first level) in a dictionary
Listing all values in a dictionary
Delete a key from a dictionary
Last updated
Was this helpful?