Lesson 21 of 32By Simplilearn
Last updated on Jun 29, 20206344Python is one of the most popular programming languages available today. It is widely used in many business-related IT processes such as programming, web development, machine learning, and data science. Given its widespread use, it's not surprising that Python has surpassed Java as the top programming language. In this article, we will be looking at sets and dictionaries, one of the most essential elements in Python programming.
Our discussion will include:
Python is a general-purpose programming language often used for scripting. Although many scripting languages have limitations, Python is a simple yet robust programming language that delivers both power and complexity. Python is designed for simplicity, both in reading and writing.
A Python set is an unordered collection of unique elements. Every set element is unique and immutable. A set can consist of different data types such as integers, strings, tuples, etc.
Fig: Example of Python Sets
In Python, sets are written within curly braces separated by commas, or by using the set() method.
Example: Using curly braces { }
Fig: Creating a Python Set using curly braces
Example: Using the set() method
Fig: Creating a Set using set() method
We cannot access or change elements by using index numbers since sets are unordered. However, we can loop through the set to find out if the particular item exists or not.
Example:
Fig: To check if a particular element exists in Set or not
If we want to add a single element, we use the add() method.
Syntax: set_name.add (element)
Example:
Fig: Adding single element using add() method
To add multiple elements, we use the update() method.
Example:
Fig: Add multiple elements using update() method
To check how many elements a Python Set has, we use the len() method.
Example:
Fig: To check the number of elements in a Python Set
To remove an item from a set, we use remove() or discard() method.
Example:
Fig: Deleting elements using remove() and discard() method
Now that we are done adding and removing elements from a set, let’s look at set operations and methods.
There are various operations that can be performed within a Python set. They are:
The union of the two sets is calculated using ( | ) operator. The union consists of all the elements from both sets.
Example:
Fig: Union of Two Sets
The intersection of two sets is calculated using ( & ) operator. The intersection consists of common elements from both the sets.
Example:
Fig: Intersection of two Sets
The difference between the two sets is calculated by using ( - ) operator.
Example:
Fig: Difference between two sets
In the output, we get only those elements that are unique to set A.
Here are some useful methods available for the set objects.
Deletes all the items from the set.
Syntax : set_name.clear()
Example:
Fig: To clear all the elements in a Python Set
Returns a copy of the set.
Syntax: set_name.copy()
Example:
Fig: copy() method
This method updates the set, who requested the method, and what the differences are between the two sets.
Example:
Fig: difference_update() method
This method updates the set, who requested the method, by the intersection of two sets.
Example:
Fig: intersection_update() method
Python set has some in-built functions. They are:
Returns the maximum value from the set.
Example:
Fig: max() function
Returns the minimum value from the set.
Example:
Fig: min() function
Returns the total sum of all the elements.
Example:
Fig: sum() function
Now that we are done with sets, let’s focus on dictionaries.
A Python dictionary is a collection of key-value pairs. They are an unordered collection of items and can be changed.
One or more “key: value” pairs, separated by commas, are put inside curly brackets to form a dictionary object.
Syntax: variable_name = { key 1 : value 1, key 2 : value 2 }
Fig: To create a Python Dictionary of various data types
We can access the elements inside a dictionary by referring to its key name, inside the square brackets, or by using get() method.
Example:
Fig: To access element
We can change the value of a specific item by referring to its key name.
Example:
Fig: To change a value in a Dictionary
To add a new element, we use the following syntax.
Syntax: dictionary_name [ “ key value “] = “element”
Example:
Fig: To add a new element in a Python Dictionary
The del() keyword removes the element of the specified key name.
Syntax: del dictionary_name [ “ key name “ ]
Example:
Fig: To delete an element in Python Dictionary
Python dictionaries have some useful built-in methods.
1. clear()
This method removes all the elements from the dictionary.
Example:
Fig: clear() method
2. fromkeys()
The fromkeys() method creates a new dictionary with a default value for all specified keys.
If the default value is not specified, all keys are set to None.
Syntax: dict.fromkeys( keys, value )
Example:
Fig: fromkeys() method
3. update()
The update() method inserts the new key-value pair.
Example:
Fig:update method()
A comprehension is a means of creating a Python data structure from iterators. Dictionary comprehension is a way to build a new dictionary by applying an expression to each item in the iterable.
Syntax: { key: value for var in interable }
Example:
Fig: Dictionary Comprehension
Learn data operations in Python, strings, conditional statements, error handling, and the commonly used Python web framework Django with the Python Training course..
Python sets and dictionaries are very useful for constructing and gaining easy access to individual Python elements. To learn more about Python sets and dictionaries, visit this link: https://www.youtube.com/watch?v=WA_JDDaaMe0
If you have any questions or comments, please share them in the comment section of the article, and we’ll have our experts get back to you.
Python is an indispensable skill for any data scientist—and data scientists are in super high demand. If you’re interested in joining this exciting field, consider enrolling in our Data Science with Python Course, which is co-developed with IBM.
Name | Date | Place | |
---|---|---|---|
Full Stack Java Developer | Cohort starts on 10th Mar 2021, Weekend batch | Your City | View Details |
Full Stack Java Developer | Cohort starts on 22nd Mar 2021, Weekend batch | Chicago | View Details |
Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.
Full Stack Java Developer
Python Training
*Lifetime access to high-quality, self-paced e-learning content.
Explore Category