Introduction To Python Programming (23ECAE21) Dictionary: SJB Institute of Technology
Introduction To Python Programming (23ECAE21) Dictionary: SJB Institute of Technology
INTRODUCTION TO
PYTHON PROGRAMMING
[23ECAE21]
By:
DICTIONARY Surabhi B(1JB23EC108)
Sushith S (1JB23EC109)
Swati Rani (1JB23EC110)
TR Koushik (1JB23EC111)
Tanisha S (1JB23EC112)
Introduction To Dictionary
Definition Of Dictionary:
• Python dictionary is a data structure that stores the
value in ‘key’: value format.
This makes it different from lists, tuples, and
arrays as in a dictionary each key has an
associated value.
Example:
Dict = {1: 'Tic', 2: 'For', 3: 'Tac'}
print(Dict)
Output:
{1: 'Tic', 2: 'For', 3: 'Tac'}
Keys
• Keys are analogous to indexes of a list.
When using lists you access the elements via the
index. With dictionaries you access values via the
keys.
Example:
• To change the value of items within a specific range,
define a list with the new values, and refer to the
range of index numbers where you want to insert the
new values.
Example:
What are methods?
• methods are functions that are associated with an
object and can manipulate its data or perform
actions on it.
• Methods of dictionary inpython programming
are:-
Clear() copy() fromkeys() get()
Items() keys() pop() popitem()
Setdefault() update() values()
clear()
• Remove all the items from the dictonary
my_dict ={‘x’ : 1, ‘ y’ : 2}
my_dict.clear()
print(“Dict cleared:”,my_dict)
SYNTAX:
dict.pop(key,[default])
Course code:23MET14D/24D Krishna Murthy N K /ASSISTANT PROFESSOR/MECHANICAL/SJBIT
pop()
• Condition-1:
If mentioned key is found, key and value is removed
from the dictionary.
pop()
• Condition-2:
If mentioned key is not found and default is mentioned,
the default argument would be passed.
pop()
• Condition-3:
If mentioned key is not found and default is not
mentioned, a key error is raised.
SYNTAX:
dict.update()
values()
• This method returns all the values of all the keys without
displaying the keys.
• The output is shown in the list format.
SYNTAX:
dict.values()
Nested dictionary
• It is a collection of key-value pairs.
• In python, a dictionary is nested dictionaries are
dictionaries within dictionaries ,allowing for
complex data structures
Example:
nested_dict = { 'key1': {'nested_key1':
'value1', 'nested_key2': 'value2’},
'key2': {'nested_key3':
'value3', 'nested_key4': 'value4’} }
Difference between list ,dictionary and tuple :
Thank You