Day 14 - Data Types in Python - Part 2

Day 14 - Data Types in Python - Part 2

We will look in details about Mapped data type and Set data type.

DICTIONARIES

Dictionaries are an ordered collection of key-value pairs enclosed within curly braces {} with unique keys.

They store multiple items in a single variable.

Accessing Dictionaries Items:

There are two ways, you can access the values of items in the dictionary: -

  1. using dict['item']

  1. using dict.get('item')

Accessing multiple values/keys

We can print all the values in the dictionaries using values() method.

Similarly, for accessing all the keys, use keys() method.

Accessing Key-Value Pairs

We can print all the key-value pairs in the dictionary using items() method.

print(f"The value of the {key} is {value}") , there is f-string used in this print statement.

f-string is a string formatting mechanism. It is also known as "Literal String Interpolation". They are string literals that have an f before the opening quotation mark, as you can see the print statement.

f-strings joined the party in Python 3.6 with PEP 498.

f-strings can be formatted in much same as the str.format() method.

  1. Using str.format() method:

  1. Using f-string:

Dictionary Methods

Dictionary uses several built-in methods for manipulation.

  1. update(): updates the value of the key provided to it if the item already exists in the dictionary, else it creates a new key-value pair.

  2. clear(): removes all the items.

  3. pop(): removes the key-value pair whose key is passed as a parameter.

  4. popitem(): removes the last key-value pair from the dictionary.

  5. del: del keyword is used to remove a dictionary item. If key is not provided, then the del keyword will delete the dictionary entirely.

SETS

Sets are an unordered and mutable collection of unique elements, enclosed in curly braces {} or using the set() function.

Sets do not contain duplicate items.

Accessing the Sets

You can see the blow screenshots, random orders of items are getting generated when we are printing set items. So, In Sets, we cannot access items using index number.

Using for loop, you can access the set items:

Sets Methods

  1. union and union_update:

  2. intersection and intersection_update:

  3. symmetric_difference and symmetric_difference_update:

  4. difference and difference_update:

    The difference and difference_update methods prints only items that are only present in the original set and not in both the sets.

    The difference method returns a new set Whereas, the difference_update method updates into the existing set from the another set.

  5. isdisjoint(): checks if items of given set are present in the another set.

    This method returns "False", if items are present else "True".

  6. issuperset(): checks if all the items of a particular set are present in the original set.

    This method returns "True", if all the items are present else "False".

  7. issubset(): checks if all the items in the original set are present in the particular set.
    This method returns "True", if all items are present else "False".

  8. add: to add single item to a set.

  9. update: to add more than one item.
    Simply create another set or any other data type(list, tuple, dict) and use this method to add it into the existing set.

  10. remove/discard: removes item from set.

    The main difference between remove and discard is that if we delete an item which is not present in set then remove method will raise an error and discard doesn't.

  11. pop: removes the last item of the set but the catch is that we don't know which item will get pop as set are unordered.
    However, we can access the popped item by using pop() method, assigning it to a variable.

  12. del: It deletes the set entirely.

  13. clear: clears all items in the set and prints the empty set.

CONCLUSION

In Python, dictionaries and sets are essential data structures with distinct purposes.

  1. Dictionaries:

    • A dictionary is a collection of key-value pairs.

    • Keys are unique and immutable, and they are used to access associated values.

    • Created using curly braces {} and key-value pairs separated by colons.

  2. Sets:

    • A set is an unordered collection of unique elements.

    • Created using curly braces {} or the set() constructor.

    • Sets are useful for mathematical operations like union, intersection, and difference.

In summary, dictionaries are used for key-value mappings, while sets are employed for collections of unique elements. Both are versatile and contribute to efficient data manipulation and storage in Python.

👆The information presented above is based on my interpretation. Suggestions are always welcome.😊

~Smriti Sharma✌