What is Dictionary in python

Ad Code

What is Dictionary in python

 

What is Dictionary in python?

A dictionary is a kind of data structure that stores items in key-value pairs. A key is a unique identifier for an item, and a value is the data associated with that key. The dictionary value may be of any type. Dictionary can be changed after they are created. They are also unordered, indicating the items in a dictionary are not stored in any particular order.

Example1: Dictionary

capitals = {"USA":"Washington D.C.", "France":"Paris", "India":"New Delhi"}
print(capitals)

Example2:

dict={1:'windows',2:'microsoft',3:'pyhton'}

print(dict[3])

 

Example3:

capitals={"USA":"Washington DC","France":"Paris","India":"New Delhi"}

cap=input("Enter Country Name:")

print("Capital of ",cap,"is ",capitals[cap])

Example4:

capitals={"USA":"Washington DC","France":"Paris","India":"New Delhi"}

ch='y'

while ch!='n':

    cap=input("Enter Country Name:")

    print("Capital of ",cap,"is ",capitals[cap])

    print("do you want more y:n")

Example5

capitals = {"USA":"Washington D.C.", "France":"Paris", "India":"New Delhi"}

print(capitals)

ch='y'

while ch=='y':

    a=input("Enter country name")

    print("Capital of",a ,"is",capitals[a])

    ch= input("Are you want more(Y/N:)?")

    if ch!='n' and ch!='y':

        print("Invalid selection")

        ch= input("Are you want more(Y/N:)?")

Conclusion

Dictionaries in Python are powerful, flexible, and essential for handling structured data. 
They provide fast lookups and are incredibly useful in everything from web apps to data analysis.
Keys must be unique and immutable, while values can be of any type.
With support for nesting and many built-in methods, dictionaries make data manipulation clean and intuitive.


Post a Comment

0 Comments

Ad Code