Dictionary in Python

 Dictionary:

  • The dictionary data type is similar to the sets but a little bit different and the dictionary are also mutable.
  • It contains pair of parameters. i.e, keys and values.
  • Key is used to store element names.
  • Value is used to store element values.

Syntax:

  1. Declaring dictionry:

    Variable_name={}

    Example:

    dict={}

    Declaring value into the list:

    Variable_name={item_name 1: item_value 1,

                               item_name 2: item_value 2,

                               item_name n: item_value n}

    Example:

    std={"Roll_num":1000,'Name':'Kurshitha','Gender':'Female','mark':450}

Example program for print the dictionay of elements:

  1. std={"Roll_num":1000, 'Name':'Kurshitha','Gender':'Female','mark':450}

    print("Student name: ",std['Name'])

    print("Elements in dictionary:")

    print(std)

Output:

Print element name in for loop using keys() function:

  1. std={"Roll Number":1000,'Name':'Kurshitha','Gender':'Female','Mark':450}

    for i in std.keys():

        print(i)

Output:

Print element values in for loop using values() function:

  1. std={"Roll Number":1000,'Name':'Kurshitha','Gender':'Female','Mark':450}

    for i in std.values():

        print(i)

Output:

Print key and values in for loop using item() function:

  1. std={"Roll Number":1000,'Name':'Kurshitha','Gender':'Female','Mark':450}

    for k,v in std.items():

        print(k,":",v)

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me