Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Dictionary in python programming language

Posted on October 9, 2017September 29, 2019

Table of Contents

  • Python dictionary
      • How to create the dictionary
    • How to accessing Dictionary
      • How to print all of element of dictionary
      • How to update an item of dictionary If we want to update items to the dictionary, we can use an assignment operator. An example is given below
      • How to remove the item of dictionary If we want to remove items from the dictionary, we can use pop() operator. Example is given below

Python dictionary

In this tutorial, we will discuss the dictionary in the python programming language.

Dictionary is a special type of variable in python. It is an unordered collection of items, it has a pair of items, key and value. When creating the dictionary, we can place items inside of the dictionary using  curly braces{} and separated by a comma.

Every item has a key and the corresponding value expressed as a pair key: value

Dictionary in python programming language
Dictionary in python

How to create the dictionary

Dictionary can have items inside curly braces {} and every item is separated by the comma.
Dictionary can have a pair of items such as key and value.

ProgrammingLanguages={}
ProgrammingLanguages[“php”]=”Hyper text Preprocessor”
ProgrammingLanguages[“htm”]=”Hyper text markup language”
ProgrammingLanguages[“css”]=”cascading style sheet”
ProgrammingLanguages[“js”]=”Java script”

How to accessing Dictionary

How to print all of element of dictionary

print(ProgrammingLanguages) //print all element of dictionary

Output

{‘php’: ‘Hyper text Preprocessor’, ‘html’: ‘Hyper text markup language’, ‘css’: ‘cascading style sheet’, ‘js’: ‘Java script’}

Print  every element of using name instead index value or key value

Output                                   //print every element of dictionary using keys

Hypertext Preprocessor
Hypertext markup language
cascading style sheet
Java script

How to update an item of dictionary

If we want to update items to the dictionary, we can use an assignment operator. An example is given below


When the above code is executed, it produces the following results


{'age': 45, 'name': 'saman'} // before update
{'age': 34, 'name': 'saman'} //after update
How to add the item of dictionary


If we want to add items to the dictionary, we can use the assignment operator. Example is given below


When the above code is executed, it produced the following results


{'age': 45, 'name': 'saman'}  // before add
{'age': 45, 'name': 'saman', 'address': 'Jaffna'} //after add


How to remove the item of dictionary

If we want to remove items from the dictionary, we can use pop() operator. Example is given below

 
remove particular item using pop
 
 
 
When the above code is executed, it produced the following results
 
{'age': 45, 'name': 'saman', 'address': 'colombo'}

45

{'name': 'saman', 'address': 'colombo'}


 
remove any item using pop

 

 

 

When the above code is executed, it produced the following results
{'age': 45, 'name': 'saman', 'address': 'colombo'}

('age', 45)

{'name': 'saman', 'address': 'colombo'}


How to clear or delete dictionary items using del, clear()
 
 
 


When the above code is executed, it produced the following results

{1: 1, 2: 3, 3: 6, 4: 10, 5: 15}
{1: 1, 2: 3, 3: 6, 5: 15}

{}
 

 

Related post
Data type in Python                                     List in Python       
Tuple in Python                                           
Set Data type in Python

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes