An Interest In:
Web News this Week
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
Python dictionary and dictionary methods
What is Dictionary?
We have said that there are mutable (modifiable) data types that contain different data types such as Tuple
and List
data types in dictionaries and are shown with curly braces.
Dictionaries
are a little different because Dictionaries
consist of two parts; keys
and values
, value part can contain all data type but keys part can only be of string
and int
type.
How to Use Dictionaries?
Before I show its usage, Lets explain why dictionary; Imagine that you are making an English dictionary, how would it be without a database. You have to write an if
condition for each word. Each word is a conditional cycle, which makes the program tiring. Let me show you with an example;
"""Romanian Dictionary Application"""word = input("Enter the english word: ")if word == "Computer": print("Calculator")elif word == "School": print("coal")elif word == "Memory": print("Memorie")elif word == "House": print("Casa")elif word == "Bus": print("Autobuz")elif word == "Car": print("Main")
You will probably have written code like this.
Let's write the same application with Dictionaries.
"""Romanian Dictionary Application"""word = input("Enter the english word: ")dictionary = {"Computer":"Calculator","School":"coal","Memory":"Memorie","House":"Casa","Bus":"Autobuz","Car":"Main"}
In this way, you will have written more neat and readable code, and since Dictionaries are mutable (modifiable) data types, they can be easily added, deleted, changed, etc. we can do the work.
Accessing Dictionary Items
Dictionaries are based on key=value
relationships, and every value has a key value. Then we can reach the value with the key we know. Let's give an example right away;
dictionary = {"Computer":"Calculator","School":"coal","Memory":"Memorie","House":"Casa","Bus":"Autobuz","Car":"Main"}print(dictionary["Computer"])# Calculator
Dictionary Methods
Let's list the dictionary methods immediately;
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
If we omit __X__
private methods
['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
We will cover their methods.
keys
Method
Continue this post on my blog! Python dictionary and dictionary methods.
Original Link: https://dev.to/baransel/python-dictionary-and-dictionary-methods-320a
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To