- This event has passed.
Python Set and Dictionary
April 30, 2024 @ 8:00 am - 5:00 pm
What is Set?
- A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. A set is a collection that is both unorderedand unindexed. Sets are written with curly brackets.
How to create Set?
- myset = {“revin”, “reviving”, 54, 54.36}
- print(myset)
- x = set([1,2,3,4])
- print(x,type(x))
Empty Set
- # Empty set using set() function
- myset= set()
- print(type(myset))
Read and Access
- In set we can’t access using index or key only we can loop to get the elements or items of set.
- myset = {“revin”, “reviving”, 54, 54.36}
- for var in myset:
- print(myset)
- print(myset) #complete accesss
Add or Change
- We cannot change its items, but you can add new items.We can add new items with add() & update() method
Add() method to add one item to a set use the add() method.
- myset.add(“hello world”)
print(myset)
Update method() To add items from another set into the current set, use the update() method.
- myset1 = {“revin”, “reviving”, 54, 54.36}
- myset2 = {1, 2}
- myset1.update(myset2)
Remove : To remove an item in a set, use following methods:
- remove
- discared
- pop
- clear
- del
————————————————————————————————————————————————————————–
What is Dictionary?
- A dictionary consists of a collection of key-value pairs.Dictionary items are unordered, changeable, and does not allow duplicates keys. Dictionaries cannot have two items with the same key.
How to create Dictionary?
- To create a dictionary declare key-value pairs enclosing a comma-separated in curly braces ({})
- A colon (:) separates each key from its associated value.
Creating a Dictionary
- mydict= {1: ‘Reviving’, ‘a’: ‘India’, 34.5: ‘Revin’}
- print(mydict)
Creating an empty Dictionary
- mydict = {}
- print(“Empty Dictionary: “)
- print(mydict)
ACCESS DICTIONARY ITEMS
- He items of a dictionary can be accessed by referring to its key name, inside square brackets.
- mydict[1]
- Get() method is also used to get the value of existing key in dictionary
- x = mydict.get(1)
- If we use the square brackets [], KeyErroris raised in case a key is not found in the dictionary. On the other hand, the get() method returns None if the key is not found.
ACCESS ALL THE KEYS
- The keys() method will return a list of all the keys in the dictionary.
- mydict.keys()
ACCESS ALL THE VALUES
- The values() method will return a list of all the values in the dictionary.
- mydict.values()
KEY,VALUE BOTH ACCESS
- The items() method will return each item in a dictionary, as tuples in a list.
- mydict.items()
How to update Dictionary?
- The value of a specific item or key can be changed by referring to its key name.
- mydict[1] = ’2021’
- If the key will not exist in the dictionary , then it will add the key value pair rather than update.
UPDATE : The update() method will update the dictionary with the items from the given argument.
- mydict.update({1: 58})
REMOVE DICT ITEMS
- pop – method removes the item with the specified key name
- popitem(): method removes the last inserted item.
- Del – keyword removes the item with the specified key name
- Clear : This method removes all the elements from a dict
