dictionaries in python class 11

Learn dictionaries in python class 11

 

dictionaries in python

 

What is Dictionaries:-

Dictionaries are mutable, unordered collection with elements in the form of a key: value pairs are associate keys to value.

Creating Dictionaries:-

to create dictionaries, you need to include the key: value pairs in curly braces as per following syntax:

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

dictionary in python class 11

example:-

teachers = {“dimple”:”computer science”,”karen”:”sociology”,”harpreet”:”mathematics”,:”sabah”:”legal studies”}

dict1 = {}                    # empty dictionaries

now you can easily identify the keys and corresponding values from above given dictionaries.

one thing that you must know is that keys of a dictionary must be of immutable types, such as:

  • a python strings,
  • a number,
  • a tuple (containing only immutable entries).

Accessing elements of a dictionaries:-

<dictionary-name>[<key>]

example:

teachers = {“dimple”:”computer science”,”karen”:”sociology”,”harpreet”:”mathematics”,:”sabah”:”legal studies”}

>>>teachers[“karen”]

sociology

>>>print(“karen teaches”, teachers[“karen”]

karen teaches sociology

>>>teachers

{“dimple”:”computer science”,”karen”:”sociology”,”harpreet”:”mathematics”,:”sabah”:”legal studies”}

Traversing a dictionary:-

for <item> in <dictionary>:

process each item here

Accessing keys or values simultaneously:-  (dictionaries in python)

to see all the keys in a dictionary in one go, you may write <dictionary>.keys() and see all values in one go, you may write <dictionary>.values(), as shown below:

Characteristics of a dictionary:-

dictionary like lists are mutable and that is the only similarity they have with lists. Otherwise dictionaries are different type of data structure with following characteristics:

1. Unordered Set

a dictionary is a unordered set of key:value pairs. Its values can contain references to any type of object.

2. Not a Sequence

Unlike a string, list and tuple, a dictionary is not a sequence because it is unordered set of elements. The sequences are indexed by a range of ordinal numbers. Hence, they are ordered, but a dictionary is an unordered collection.

3. Indexed by Keys, Not Number:-

following dictionary dict1 has keys of different immutable types:

>>>dict1 = {

0 : “value for key 0”, 1 : “value for key 1”,

“3” : “value for string-as-a-key”,

(4,5) : “value for tuple-as-a-key”,

“and for fun” : 7

}

>>>dict1[0]

‘value for key 0’

>>>dict1[(4,5)]

‘value for a tuple-as-a-key’

>>>dict[4,5]

‘value for tuple-as-a-key’

dict1[“3”]

‘value for a string-as-a-key’

4. Keys must be unique:-

each of the keys within a dictionary must be unique. since keys are used to identify values in a dictionary, there cannot be duplicate keys in a dictionary.

however, two unique keys can have same values.

5. Mutable:-

lide lists, dictionaries are also mutable. we can change the value of a certain key “in place” using the assignment statement per syntax:

<dictionary>[<keys>] = <value>

for example:

>>>dict1 = {

0 : “value for key 0”, 1 : “value for key 1”,

“3” : “value for string-as-a-key”,

(4,5) : “value for tuple-as-a-key”,

“and for fun” : 7

}

>>>dict1[“3”]

‘value for string-as-a-key’

>>>dict[“3”] = “changed to new strig”

>>>dict1[“3”]

‘changed to new string’

 6. internally stored as mapping:-

internally, the key : value pairs of a dictionary are associated with one another with some internal function (called hash-function1). the way of linking is called mapping.

fig 9.2

Working with dictionaries:-

let us discuss about various operations that you can perform on dictionaries, such as adding elements to dictionaries, updating and deleting elements of dictionaries. This section is dedicated to the same.

multiple ways of creating dictionaries:

(i) initializing a dictionary

in this method all the key : value pairs of dictionary are written collectively, separated by commas and enclosed in curly braces.

(ii) adding key : value pairs to an empty dictionary

in this method, firstly an empty dictionary is created and then keys and values are added to it one pair at a time.

(iii) creating a dictionary from name and value pairs

using the dict() constructor of dictionary, you can also create a new dictionary initialize from specified set of keys and values. There are multiple ways to provide keys and values to dict() constructor.

Adding elements to dictionary:-

add new elements(key : value pair) to a dictionary using assignment as per following syntax. but the key being added must not exist in dictionary and must be unique. if the key already exists, then this statement will change the value of existing key key and no new entry will be added to dictionary.

9.2

Updating Existing Elements in a dictionary:-

updating an element is similar to what we did just now. that is, you can change value of an existing key using assignment as per following syntax:

dictionary>[<key>] = <value>

Deleting elements from a dictionary:-

dictionaries in python

there are two methods for deleting elements from a dictionary.

(i) to delete a dictionary elements or a dictionary entry, i.e., a key: value pair, you can use Del command.
the syntax for doing so is given below:

Del<dictionary>[<key>]

(ii) another method to delete to delete elements from a dictionary is by using pop() method as per following syntax:

<dictionary>.pop(<key>)

Checking for existence of a key:-

usual membership operator in and not in work with dictionaries as well. but they can check for the existence of keys only. to check for whether a value is present in a dictionary, you need to write proper code for that.

to use a membership operator for key’s presence in a dictionary, you may write in the dictionary, you may write statement as per syntax given below:

<key> in <dictionary>

<key> not in <dictionary>

dictionaries in python  class 11 important questions

cbse sample paper with answer

learn more about this chapter

 
dictionaries in python class 11