Tuples
Tuples are like lists, only that they store immutable and ordered data types. They can be used when dealing with information or data that is closely related and often used together e.g., latitudes and longitudes.
More often, programmers omit using parentheses ()
if they do not provide further definition to the code. Tuples, like lists, can be accessed by their indices e.g,
dimensions = 3, 4, 5
print ("length:" , dimensions[0])
>>> 3
Noteworthy, trailing commas are used to define tuples e.g.,
variable = 3,
print (type(variable))
>>> <class 'tuple'>
When the comma is removed, the code will return an int
as illustrated below:
variable = 3
print (type(variable))
>>> <class 'int'>
Sets
They are a mutable and unordered collection of unique elements. An example of a set is a google form response for vehicles models that will be present at a car meet in Eldoret.
print (set(vehicle_types))
returns an unordered set of vehicle_types
without any duplicates.
>>> {'volkswagen', 'mazda', 'nissan', 'mercedes', 'toyota', 'mitsubishi', 'honda'}
print (len(set(vehicle_types)))
returns the number of unique vehicles present. In this case, it is 7.
Additionally, we can check for the presence of a vehicle type or its absence using in
e.g
print ("toyota" in vehicle_types)
returns True
whereas print ("suzuki" in vehicle_types)
returns False
Set Methods
add()
- adds an element to the setpop()
- sets are unordered so this methods removes an element at randomremove()
- removes a specified element from the set. However, removing elements not in a set returns aKeyError
. Usingdiscard()
bypasses this error although it is advisable to first check whether an element is in the list usingin
clear()
- clears all elements from a setfrozenset()
- returns an immutable set. Yes, I recall sets being mutable but try this out just for the kick of it :)
Dictionaries and Identity Operators
Dictionaries
According to my understanding, dictionaries store pairs of elements i.e keys and their values. They are defined using curly braces {}
.
For example:
print(type(ages))
returns class 'dict'
print(ages["Brian"]
returns 27
In the above example, the value "Brian"
has been accessed by using square brackets.
To add elements in a dictionary, I tried the following:
ages["Maminka"] = 55
print (ages)
>>> {'Tracie': 29, 'Brian': 27, 'Anne': 25, 'Maminka': 55}
Values in a dictionary can also be accessed by using the get()
method. For example,
print (ages.get("Maya"))
The above syntax returns None
as the default value. However, it is possible to pass another argument within the get()
method.
print (ages.get("Maya", "The age specified is not available"))
Further, we can check if an element is within a dictionary using is
and is not
. These are referred to as Identity Operators.
x = ages.get("Allan")
is_null = x is None
print (is_null)
>>> True
Using is not
we get:
not_null = x is not None
print (not_null)
>>> False
Please feel free to reach out or comment on anything that I have written above. Additional tips and tricks are always welcome!