Python Dictionary

Python Dictionary Agenda: 1. Python Dictionary 2. Ex: Keys and Values 3. Python Methods 4. Dictionary Example Program.
Agenda:
1. Python Dictionary
2. Ex: Keys and Values
3. Python Methods
4. Dictionary Example Program

1. Python Dictionary

A python dictionary is a collection of unordered, changeable and
indexed.
It has defined by curly braces {}
It has keys and values.

2. Example: Keys and Values


My_dict={1:’python’,2:’programming’}
         ↓               ↓
        Key             Value


Dictionary is not allows duplicate keys.

3.Dictionary Methods

Accessing

Mydict={1:’a’,2:’b’} --> mydict[1]--> 2

Len()

Len() function is used to find the length of the dictionary

mydict={1:’a’,2:’b’}--> len(mydict) --> 2 

Key()

Key() is used to shows the keys in the dictionary.

mydict={1:’a’,2:’b’}--> mydict.key()-->[1,2]

Values()

Values () function is used to shows the values of the dictionary.


mydict={1:’a’,2:’b’} --> mydict.values() --> [‘a’,’b’]

Dictionary Example Program

# Dictionary
Mydict={1:'python',2:'programming'}
print("Mydict=",Mydict,"This Integer keys")
Mydict1={'First name': 'Hemarangan', 'Last name':"S"}
print(Mydict1)
Mydict2={'Welcome':'HRS',2:[2,4,6]}
print("Mixed Keys",Mydict2)
print("Dictionary not allows duplicate keys")
# Dictionary not allows duplicate keys
print("Before")
Mydict={1:'python',2:'programming'}
print(Mydict)
print("After")
Mydict={1:'python',2:'programming',2:"hello"}
print(Mydict)
#Dictionary Methods
print("Let's move the Dictionary Methods")
print(Mydict)
print(Mydict[2])
print(len(Mydict))
print(Mydict.keys())
print(Mydict.values())

Output

Mydict= {1: 'python', 2: 'programming'} This Integer keys
{'First name': 'Hemarangan', 'Last name': 'S'}
Mixed Keys {'Welcome': 'HRS', 2: [2, 4, 6]}
Dictionary not allows duplicate keys
Before
{1: 'python', 2: 'programming'}
After
{1: 'python', 2: 'hello'}
Let's move the Dictionary Methods
{1: 'python', 2: 'hello'}
hello
2
dict_keys([1, 2])
dict_values(['python', 'hello'])

Process finished with exit code 0
If you have any queries, please contact us Try to make own program, it definitely improves your programming skill level. 
Please share your feedback in the contact or comment.
Thank You guys. We'll see another python series.