Dictionaries in Python | Dictionaries Functions and methods in python | CBSE Class XII Computer science

Dictionaries in Python | Dictionaries Functions and methods in python 

Hi guys in this blog we are going to discuss about Dictionaries in Python 

Python dictionaries are a collection of some key-value pairs. Dictionaries are mutable, unordered collections with elements in the form of a key : value pairs that associate keys to values.


Creating a Dictionary

To create a dictionary, you need to include the key: value pairs in curly braces as per following

syntax:

<dictionary-name>= {<key>:<value>, <key>:<value>...}

Following is an example dictionary by the name teachers that stores the names of teachers as keys and the subjects being taught by them as values of respective keys.

teachers = { "Dimple" : "Computer Science", "Karen" "Sociology", "Harpreet":"Mathematics", "Sabah" "Legal Studies" }

Note

  • the curly brackets mark the beginning and end of the dictionary,
  • each entry (Key: Value) consists of a pair separated by a colon - the key and corresponding
  • value is given by writing colon () between them,
  • the key-value pairs are separated by commas ().
  • Internally, dictionaries are indexed (i.e., arranged) on the basis of keys.


Accessing Elements of a Dictionary

In dictionaries, the elements are accessed through the keys defined in the key:value pairs, as per

the syntax shown below:

<dictionary-name> [<key>]

Dictionaries Functions and methods in Python

  • len()
  • clear()
  • get()
  • items()
  • keys()
  • values ()
  • update() 

Post a Comment

If you any doubts, Please let me know

Previous Post Next Post