SlideShare a Scribd company logo
Python Programming –Part 7
Megha V
Research Scholar
Dept.of IT
Kannur University
16-11-2021 meghav@kannuruniv.ac.in 1
Dictionary
• Creating Dictionary,
• Accessing and Modifying key : value Pairs in Dictionaries
• Built-In Functions used on Dictionaries,
• Dictionary Methods
• Removing items from dictonary
16-11-2021 meghav@kannuruniv.ac.in 2
Dictionary
• Unordered collection of key-value pairs
• Defined within braces {}
• Values can be accessed and assigned using square braces []
• Keys are usually numbers or strings
• Values can be any arbitrary Python object
16-11-2021 meghav@kannuruniv.ac.in 3
Dictionary
Creating a dictionary and accessing element from dictionary
Example:
dict={}
dict[‘one’]=“This is one”
dict[2]=“This is two”
tinydict={‘name’:’jogn’,’code’:6734,’dept’:’sales’}
studentdict={‘name’:’john’,’marks’:[35,80,90]}
print(dict[‘one’]) #This is one
print(dict[2]) #This is two
print(tinydict) #{name’:’john’,’code’:6734,’dept’:’sales’}
print(tinydict.keys())#dict_keys([’name’,’code’,’dept’])
print(tinydict.value())#dict_values([’john’,6734,’sales’])
print(studentdict) #{name’:’john’,’marks’:[35,80,90])
16-11-2021 meghav@kannuruniv.ac.in 4
Dictionary
• We can update a dictionary by adding a new key-value pair or modifying an existing entry
Example:
dict1={‘Name’:’Tom’,’Age’:20,’Height’:160}
print(dict1)
dictl[‘Age’]=25 #updating existing value in Key-Value pair
print ("Dictionary after update:",dictl)
dictl['Weight’]=60 #Adding new Key-value pair
print (" Dictionary after adding new Key-value pair:",dictl)
Output
{'Age':20,'Name':'Tom','Height':160}
Dictionary after update: {'Age’:25,'Name':'Tom','Height': 160)
Dictionary after adding new Key-value pair:
{'Age’:25,'Name':'Tom',' Weight':60,'Height':160}
16-11-2021 meghav@kannuruniv.ac.in 5
Dictionary
• We can delete the entire dictionary elements or individual elements in a dictionary.
• We can use del statement to delete the dictionary completely.
• To remove entire elements of a dictionary, we can use the clear() method
Example Program
dictl={'Name':'Tom','Age':20,'Height':160}
print(dictl)
del dictl['Age’] #deleting Key-value pair'Age':20
print ("Dictionary after deletion:",dictl)
dictl.clear() #Clearing entire dictionary
print(dictl)
Output
{'Age': 20, 'Name':'Tom','Height': 160}
Dictionary after deletion: {‘Nme ':' Tom','Height': 160}
{}
16-11-2021 meghav@kannuruniv.ac.in 6
Properties of Dictionary Keys
• More than one entry per key is not allowed.
• No duplicate key is allowed.
• When duplicate keys are encountered during assignment, the
last assignment is taken.
• Keys are immutable- keys can be numbers, strings or
tuple.
• It does not permit mutable objects like lists.
16-11-2021 meghav@kannuruniv.ac.in 7
Built-In Dictionary Functions
1.len(dict) - Gives the length of the dictionary.
Example Program
dict1= {'Name':'Tom','Age':20,'Height':160}
print(dict1)
print("Length of Dictionary=",len(dict1))
Output
{'Age': 20, 'Name': 'Tom', 'Height': 160}
Length of Dictionary= 3
16-11-2021 meghav@kannuruniv.ac.in 8
Built-In Dictionary Functions
2.str(dict) - Produces a printable string representation of the dictionary.
Example Program
dict1= {'Name':'Tom','Age':20,'Height' :160}
print(dict1)
print("Representation of Dictionary=",str(dict1))
Output
{'Age': 20, 'Name': 'Tom', 'Height': 160}
Representation of Dictionary= {'Age': 20, 'Name': 'Tom', 'Height': 160}
16-11-2021 meghav@kannuruniv.ac.in 9
Built-In Dictionary Functions
3.type(variable)
• The method type() returns the type of the passed variable.
• If passed variable is dictionary then it would return a dictionary type.
• This function can be applied to any variable type like number, string,
list, tuple etc.
16-11-2021 meghav@kannuruniv.ac.in 10
type(variable)
Example Program
dict1= {'Name':'Tom','Age':20,'Height':160}
print(dict1)
print("Type(variable)=",type(dict1) )
s="abcde"
print("Type(variable)=",type(s))
list1= [1,'a',23,'Tom’]
print("Type(variable)=",type(list1))
Output
{'Age': 20, 'Name': 'Tom', 'Height': 160}
Type(variable)= <type ‘dict'>
Type(variable)= <type 'str'>
Type(variable)= <type 'list' >
16-11-2021 meghav@kannuruniv.ac.in 11
Built-in Dictionary Methods
1.dict.clear() - Removes all elements of dictionary dict.
Example Program
dict1={'Name':'Tom','Age':20,'Height':160}
print(dict1)
dict1.clear()
print(dict1)
Output
{'Age': 20, 'Name':' Tom’ ,'Height': 160}
{}
16-11-2021 meghav@kannuruniv.ac.in 12
2.dict.copy() - Returns a copy of the dictionary dict().
3.dict.keys() - Returns a list of keys in dictionary dict
- The values of the keys will be displayed in a random order.
- In order to retrieve keys in sorted order, we can use the sorted() function.
- But for using the sorted() function, all the key should of the same type.
4. dict.values() -This method returns list of all values available in a dictionary
-The values will be displayed in a random order.
- In order to retrieve the values in sorted order, we can use the sorted()
function.
- But for using the sorted() function, all the values should of the same type
16-11-2021 meghav@kannuruniv.ac.in 13
Built-in Dictionary Methods
Built-in Dictionary Methods
5. dict.items() - Returns a list of dictionary dict's(key,value) tuple pairs.
dict1={'Name':'Tom','Age':20,'Height’:160}
print(dict1)
print("Items in Dictionary:",dict1.items())
Output
{'Age': 20, 'Name': 'Tom', 'Height': 160}
tems in Dictionary:[('Age', 20),( ' Name ' ,'Tom’), ('Height', 160)]
16-11-2021 meghav@kannuruniv.ac.in 14
6. dict1.update(dict2) - The dictionary dict2's key-value pair will be updated in dictionary dictl.
Example:
dictl= {'Name':'Tom','Age':20,'Height':160}
print(dictl)
dict2={'Weight':60}
print(dict2)
dictl.update(dict2)
print(“Dictl updated Dict2:",dictl)
Output
{'Age': 20,'Name':'Tom,' Height':160}
{‘Weight’:60}
Dict1 updated Dict2 :{'Age': 20,'Name':'Tom,' Height':160,’Weight’:60}
16-11-2021 meghav@kannuruniv.ac.in 15
Built-in Dictionary Methods
Built-in Dictionary Methods
7. dict.has_key(key) – Returns true if the key is present in the dictionary, else False
is returned
8. dict.get(key,default=None) – Returns the value corresponding to the key
specified and if the key is not present, it returns the default value
9. dict.setdefault(key,default=None) – Similar to dict.get() byt it will set the key
with the value passed and if the key is not present it will set with default value
10. dict.fromkeys(seq,[val]) – Creates a new dictionary from sequence ‘seq’ and
values from ‘val’
16-11-2021 meghav@kannuruniv.ac.in 16
Removing Items from a Dictionary
• The pop() method removes the item with the specified key name.
thisdict={"brand":"Ford","model":"Mustang","year":1964}
thisdict.pop("model")
print(thisdict)
• The popitem() method removes the last inserted item (in versions before 3.7, a random item
is removed instead):
thisdict = {"brand":"Ford","model":"Mustang","year":1964}
thisdict.popitem()
print(thisdict)
16-11-2021 meghav@kannuruniv.ac.in 17
Removing Items from a Dictionary
• The del keyword removes the item with the specified key name:
thisdict={"brand":"Ford","model":"Mustang","year":1964}
del thisdict["model"]
print(thisdict)
• The del keyword can also delete the dictionary completely:
thisdict={"brand":"Ford","model":"Mustang","year":1964}
del thisdict
print(thisdict) #this will cause an error because "thisdict“ no
#longer exists.
• The clear() keyword empties the dictionary
thisdict.clear()
16-11-2021 meghav@kannuruniv.ac.in 18
LAB ASSIGNMENT
• Write a Python program to sort(ascending and descending) a dictionary by
value
• Write python script to add key-value pair to dictionary
• Write a Python script to merge two dictionaries
16-11-2021 meghav@kannuruniv.ac.in 19
Ad

More Related Content

What's hot (20)

Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
baabtra.com - No. 1 supplier of quality freshers
 
Java Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APIJava Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream API
Svetlin Nakov
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
Emertxe Information Technologies Pvt Ltd
 
Iteration
IterationIteration
Iteration
Pooja B S
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: Arrays
Svetlin Nakov
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
Pooja B S
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
Emertxe Information Technologies Pvt Ltd
 
The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
Python
PythonPython
Python
Kumar Gaurav
 
Chapter 4 - Classes in Java
Chapter 4 - Classes in JavaChapter 4 - Classes in Java
Chapter 4 - Classes in Java
Khirulnizam Abd Rahman
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
Khirulnizam Abd Rahman
 
Python programing
Python programingPython programing
Python programing
hamzagame
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
Intro C# Book
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
Giovanni Della Lunga
 
The Ring programming language version 1.4.1 book - Part 29 of 31
The Ring programming language version 1.4.1 book - Part 29 of 31The Ring programming language version 1.4.1 book - Part 29 of 31
The Ring programming language version 1.4.1 book - Part 29 of 31
Mahmoud Samir Fayed
 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQChapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
Chapter 2 Method in Java OOP
Chapter 2   Method in Java OOPChapter 2   Method in Java OOP
Chapter 2 Method in Java OOP
Khirulnizam Abd Rahman
 
Introduction to python programming 2
Introduction to python programming   2Introduction to python programming   2
Introduction to python programming 2
Giovanni Della Lunga
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Arnaud Joly
 
Java Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APIJava Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream API
Svetlin Nakov
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: Arrays
Svetlin Nakov
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
Pooja B S
 
The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
Python programing
Python programingPython programing
Python programing
hamzagame
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
Intro C# Book
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
Giovanni Della Lunga
 
The Ring programming language version 1.4.1 book - Part 29 of 31
The Ring programming language version 1.4.1 book - Part 29 of 31The Ring programming language version 1.4.1 book - Part 29 of 31
The Ring programming language version 1.4.1 book - Part 29 of 31
Mahmoud Samir Fayed
 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQChapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
Introduction to python programming 2
Introduction to python programming   2Introduction to python programming   2
Introduction to python programming 2
Giovanni Della Lunga
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Arnaud Joly
 

Similar to Python programming –part 7 (20)

Python dictionaries
Python dictionariesPython dictionaries
Python dictionaries
Krishna Nanda
 
Dictionary in python Dictionary in python Dictionary in pDictionary in python...
Dictionary in python Dictionary in python Dictionary in pDictionary in python...Dictionary in python Dictionary in python Dictionary in pDictionary in python...
Dictionary in python Dictionary in python Dictionary in pDictionary in python...
sumanthcmcse
 
Unit4-Basic Concepts and methods of Dictionary.pptx
Unit4-Basic Concepts and methods of Dictionary.pptxUnit4-Basic Concepts and methods of Dictionary.pptx
Unit4-Basic Concepts and methods of Dictionary.pptx
SwapnaliGawali5
 
dictionary14 ppt FINAL.pptx
dictionary14 ppt FINAL.pptxdictionary14 ppt FINAL.pptx
dictionary14 ppt FINAL.pptx
MamtaKaundal1
 
2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt
tocidfh
 
Dictionaries in Python programming language
Dictionaries in Python programming languageDictionaries in Python programming language
Dictionaries in Python programming language
ssuserbad56d
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
Bilal Ahmed
 
Farhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionariesFarhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionaries
Farhana Shaikh
 
The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181
Mahmoud Samir Fayed
 
First few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examplesFirst few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examples
Nebojša Vukšić
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
Pedro Rodrigues
 
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
Slides
SlidesSlides
Slides
butest
 
Python slide
Python slidePython slide
Python slide
Kiattisak Anoochitarom
 
The Ring programming language version 1.10 book - Part 22 of 212
The Ring programming language version 1.10 book - Part 22 of 212The Ring programming language version 1.10 book - Part 22 of 212
The Ring programming language version 1.10 book - Part 22 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212
Mahmoud Samir Fayed
 
Dictionaries in python
Dictionaries in pythonDictionaries in python
Dictionaries in python
JayanthiNeelampalli
 
beginners_python_cheat_sheet_pcc_all_bw.pdf
beginners_python_cheat_sheet_pcc_all_bw.pdfbeginners_python_cheat_sheet_pcc_all_bw.pdf
beginners_python_cheat_sheet_pcc_all_bw.pdf
GuarachandarChand
 
The Ring programming language version 1.7 book - Part 44 of 196
The Ring programming language version 1.7 book - Part 44 of 196The Ring programming language version 1.7 book - Part 44 of 196
The Ring programming language version 1.7 book - Part 44 of 196
Mahmoud Samir Fayed
 
DP080_Lecture_2 SQL related document.pdf
DP080_Lecture_2 SQL related document.pdfDP080_Lecture_2 SQL related document.pdf
DP080_Lecture_2 SQL related document.pdf
MinhTran394436
 
Dictionary in python Dictionary in python Dictionary in pDictionary in python...
Dictionary in python Dictionary in python Dictionary in pDictionary in python...Dictionary in python Dictionary in python Dictionary in pDictionary in python...
Dictionary in python Dictionary in python Dictionary in pDictionary in python...
sumanthcmcse
 
Unit4-Basic Concepts and methods of Dictionary.pptx
Unit4-Basic Concepts and methods of Dictionary.pptxUnit4-Basic Concepts and methods of Dictionary.pptx
Unit4-Basic Concepts and methods of Dictionary.pptx
SwapnaliGawali5
 
dictionary14 ppt FINAL.pptx
dictionary14 ppt FINAL.pptxdictionary14 ppt FINAL.pptx
dictionary14 ppt FINAL.pptx
MamtaKaundal1
 
2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt
tocidfh
 
Dictionaries in Python programming language
Dictionaries in Python programming languageDictionaries in Python programming language
Dictionaries in Python programming language
ssuserbad56d
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
Bilal Ahmed
 
Farhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionariesFarhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionaries
Farhana Shaikh
 
The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181
Mahmoud Samir Fayed
 
First few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examplesFirst few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examples
Nebojša Vukšić
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
Pedro Rodrigues
 
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
Slides
SlidesSlides
Slides
butest
 
The Ring programming language version 1.10 book - Part 22 of 212
The Ring programming language version 1.10 book - Part 22 of 212The Ring programming language version 1.10 book - Part 22 of 212
The Ring programming language version 1.10 book - Part 22 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212
Mahmoud Samir Fayed
 
beginners_python_cheat_sheet_pcc_all_bw.pdf
beginners_python_cheat_sheet_pcc_all_bw.pdfbeginners_python_cheat_sheet_pcc_all_bw.pdf
beginners_python_cheat_sheet_pcc_all_bw.pdf
GuarachandarChand
 
The Ring programming language version 1.7 book - Part 44 of 196
The Ring programming language version 1.7 book - Part 44 of 196The Ring programming language version 1.7 book - Part 44 of 196
The Ring programming language version 1.7 book - Part 44 of 196
Mahmoud Samir Fayed
 
DP080_Lecture_2 SQL related document.pdf
DP080_Lecture_2 SQL related document.pdfDP080_Lecture_2 SQL related document.pdf
DP080_Lecture_2 SQL related document.pdf
MinhTran394436
 
Ad

More from Megha V (19)

Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptx
Megha V
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
Megha V
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Megha V
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
Megha V
 
Python programming
Python programmingPython programming
Python programming
Megha V
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
Megha V
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
Megha V
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
Megha V
 
UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data  UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data
Megha V
 
Seminar presentation on OpenGL
Seminar presentation on OpenGLSeminar presentation on OpenGL
Seminar presentation on OpenGL
Megha V
 
Msc project_CDS Automation
Msc project_CDS AutomationMsc project_CDS Automation
Msc project_CDS Automation
Megha V
 
Gi fi technology
Gi fi technologyGi fi technology
Gi fi technology
Megha V
 
Digital initiatives in higher education
Digital initiatives in higher educationDigital initiatives in higher education
Digital initiatives in higher education
Megha V
 
Information and Communication Technology (ICT) abbreviation
Information and Communication Technology (ICT) abbreviationInformation and Communication Technology (ICT) abbreviation
Information and Communication Technology (ICT) abbreviation
Megha V
 
Basics of internet, intranet, e mail,
Basics of internet, intranet, e mail,Basics of internet, intranet, e mail,
Basics of internet, intranet, e mail,
Megha V
 
Information and communication technology(ict)
Information and communication technology(ict)Information and communication technology(ict)
Information and communication technology(ict)
Megha V
 
Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptx
Megha V
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
Megha V
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Megha V
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
Megha V
 
Python programming
Python programmingPython programming
Python programming
Megha V
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
Megha V
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
Megha V
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
Megha V
 
UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data  UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data
Megha V
 
Seminar presentation on OpenGL
Seminar presentation on OpenGLSeminar presentation on OpenGL
Seminar presentation on OpenGL
Megha V
 
Msc project_CDS Automation
Msc project_CDS AutomationMsc project_CDS Automation
Msc project_CDS Automation
Megha V
 
Gi fi technology
Gi fi technologyGi fi technology
Gi fi technology
Megha V
 
Digital initiatives in higher education
Digital initiatives in higher educationDigital initiatives in higher education
Digital initiatives in higher education
Megha V
 
Information and Communication Technology (ICT) abbreviation
Information and Communication Technology (ICT) abbreviationInformation and Communication Technology (ICT) abbreviation
Information and Communication Technology (ICT) abbreviation
Megha V
 
Basics of internet, intranet, e mail,
Basics of internet, intranet, e mail,Basics of internet, intranet, e mail,
Basics of internet, intranet, e mail,
Megha V
 
Information and communication technology(ict)
Information and communication technology(ict)Information and communication technology(ict)
Information and communication technology(ict)
Megha V
 
Ad

Recently uploaded (20)

Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 

Python programming –part 7

  • 1. Python Programming –Part 7 Megha V Research Scholar Dept.of IT Kannur University 16-11-2021 [email protected] 1
  • 2. Dictionary • Creating Dictionary, • Accessing and Modifying key : value Pairs in Dictionaries • Built-In Functions used on Dictionaries, • Dictionary Methods • Removing items from dictonary 16-11-2021 [email protected] 2
  • 3. Dictionary • Unordered collection of key-value pairs • Defined within braces {} • Values can be accessed and assigned using square braces [] • Keys are usually numbers or strings • Values can be any arbitrary Python object 16-11-2021 [email protected] 3
  • 4. Dictionary Creating a dictionary and accessing element from dictionary Example: dict={} dict[‘one’]=“This is one” dict[2]=“This is two” tinydict={‘name’:’jogn’,’code’:6734,’dept’:’sales’} studentdict={‘name’:’john’,’marks’:[35,80,90]} print(dict[‘one’]) #This is one print(dict[2]) #This is two print(tinydict) #{name’:’john’,’code’:6734,’dept’:’sales’} print(tinydict.keys())#dict_keys([’name’,’code’,’dept’]) print(tinydict.value())#dict_values([’john’,6734,’sales’]) print(studentdict) #{name’:’john’,’marks’:[35,80,90]) 16-11-2021 [email protected] 4
  • 5. Dictionary • We can update a dictionary by adding a new key-value pair or modifying an existing entry Example: dict1={‘Name’:’Tom’,’Age’:20,’Height’:160} print(dict1) dictl[‘Age’]=25 #updating existing value in Key-Value pair print ("Dictionary after update:",dictl) dictl['Weight’]=60 #Adding new Key-value pair print (" Dictionary after adding new Key-value pair:",dictl) Output {'Age':20,'Name':'Tom','Height':160} Dictionary after update: {'Age’:25,'Name':'Tom','Height': 160) Dictionary after adding new Key-value pair: {'Age’:25,'Name':'Tom',' Weight':60,'Height':160} 16-11-2021 [email protected] 5
  • 6. Dictionary • We can delete the entire dictionary elements or individual elements in a dictionary. • We can use del statement to delete the dictionary completely. • To remove entire elements of a dictionary, we can use the clear() method Example Program dictl={'Name':'Tom','Age':20,'Height':160} print(dictl) del dictl['Age’] #deleting Key-value pair'Age':20 print ("Dictionary after deletion:",dictl) dictl.clear() #Clearing entire dictionary print(dictl) Output {'Age': 20, 'Name':'Tom','Height': 160} Dictionary after deletion: {‘Nme ':' Tom','Height': 160} {} 16-11-2021 [email protected] 6
  • 7. Properties of Dictionary Keys • More than one entry per key is not allowed. • No duplicate key is allowed. • When duplicate keys are encountered during assignment, the last assignment is taken. • Keys are immutable- keys can be numbers, strings or tuple. • It does not permit mutable objects like lists. 16-11-2021 [email protected] 7
  • 8. Built-In Dictionary Functions 1.len(dict) - Gives the length of the dictionary. Example Program dict1= {'Name':'Tom','Age':20,'Height':160} print(dict1) print("Length of Dictionary=",len(dict1)) Output {'Age': 20, 'Name': 'Tom', 'Height': 160} Length of Dictionary= 3 16-11-2021 [email protected] 8
  • 9. Built-In Dictionary Functions 2.str(dict) - Produces a printable string representation of the dictionary. Example Program dict1= {'Name':'Tom','Age':20,'Height' :160} print(dict1) print("Representation of Dictionary=",str(dict1)) Output {'Age': 20, 'Name': 'Tom', 'Height': 160} Representation of Dictionary= {'Age': 20, 'Name': 'Tom', 'Height': 160} 16-11-2021 [email protected] 9
  • 10. Built-In Dictionary Functions 3.type(variable) • The method type() returns the type of the passed variable. • If passed variable is dictionary then it would return a dictionary type. • This function can be applied to any variable type like number, string, list, tuple etc. 16-11-2021 [email protected] 10
  • 11. type(variable) Example Program dict1= {'Name':'Tom','Age':20,'Height':160} print(dict1) print("Type(variable)=",type(dict1) ) s="abcde" print("Type(variable)=",type(s)) list1= [1,'a',23,'Tom’] print("Type(variable)=",type(list1)) Output {'Age': 20, 'Name': 'Tom', 'Height': 160} Type(variable)= <type ‘dict'> Type(variable)= <type 'str'> Type(variable)= <type 'list' > 16-11-2021 [email protected] 11
  • 12. Built-in Dictionary Methods 1.dict.clear() - Removes all elements of dictionary dict. Example Program dict1={'Name':'Tom','Age':20,'Height':160} print(dict1) dict1.clear() print(dict1) Output {'Age': 20, 'Name':' Tom’ ,'Height': 160} {} 16-11-2021 [email protected] 12
  • 13. 2.dict.copy() - Returns a copy of the dictionary dict(). 3.dict.keys() - Returns a list of keys in dictionary dict - The values of the keys will be displayed in a random order. - In order to retrieve keys in sorted order, we can use the sorted() function. - But for using the sorted() function, all the key should of the same type. 4. dict.values() -This method returns list of all values available in a dictionary -The values will be displayed in a random order. - In order to retrieve the values in sorted order, we can use the sorted() function. - But for using the sorted() function, all the values should of the same type 16-11-2021 [email protected] 13 Built-in Dictionary Methods
  • 14. Built-in Dictionary Methods 5. dict.items() - Returns a list of dictionary dict's(key,value) tuple pairs. dict1={'Name':'Tom','Age':20,'Height’:160} print(dict1) print("Items in Dictionary:",dict1.items()) Output {'Age': 20, 'Name': 'Tom', 'Height': 160} tems in Dictionary:[('Age', 20),( ' Name ' ,'Tom’), ('Height', 160)] 16-11-2021 [email protected] 14
  • 15. 6. dict1.update(dict2) - The dictionary dict2's key-value pair will be updated in dictionary dictl. Example: dictl= {'Name':'Tom','Age':20,'Height':160} print(dictl) dict2={'Weight':60} print(dict2) dictl.update(dict2) print(“Dictl updated Dict2:",dictl) Output {'Age': 20,'Name':'Tom,' Height':160} {‘Weight’:60} Dict1 updated Dict2 :{'Age': 20,'Name':'Tom,' Height':160,’Weight’:60} 16-11-2021 [email protected] 15 Built-in Dictionary Methods
  • 16. Built-in Dictionary Methods 7. dict.has_key(key) – Returns true if the key is present in the dictionary, else False is returned 8. dict.get(key,default=None) – Returns the value corresponding to the key specified and if the key is not present, it returns the default value 9. dict.setdefault(key,default=None) – Similar to dict.get() byt it will set the key with the value passed and if the key is not present it will set with default value 10. dict.fromkeys(seq,[val]) – Creates a new dictionary from sequence ‘seq’ and values from ‘val’ 16-11-2021 [email protected] 16
  • 17. Removing Items from a Dictionary • The pop() method removes the item with the specified key name. thisdict={"brand":"Ford","model":"Mustang","year":1964} thisdict.pop("model") print(thisdict) • The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead): thisdict = {"brand":"Ford","model":"Mustang","year":1964} thisdict.popitem() print(thisdict) 16-11-2021 [email protected] 17
  • 18. Removing Items from a Dictionary • The del keyword removes the item with the specified key name: thisdict={"brand":"Ford","model":"Mustang","year":1964} del thisdict["model"] print(thisdict) • The del keyword can also delete the dictionary completely: thisdict={"brand":"Ford","model":"Mustang","year":1964} del thisdict print(thisdict) #this will cause an error because "thisdict“ no #longer exists. • The clear() keyword empties the dictionary thisdict.clear() 16-11-2021 [email protected] 18
  • 19. LAB ASSIGNMENT • Write a Python program to sort(ascending and descending) a dictionary by value • Write python script to add key-value pair to dictionary • Write a Python script to merge two dictionaries 16-11-2021 [email protected] 19