Class 11 CBSE - Python

Python is designed to understand the user’s responsibility, the protocol, and the ethical issues related to the field of computing.
Python in class 11 for cbse is very handy when it comes to practice on computers but for the beginers there might be some errors and problems such as exception. But it's always advisable to practice program on computer than writing it on paper because it cherishes your practical skills , knowledge and typing speed and you'll come to know new things and if you want to be a software developer someday then its a must call for you.

Dictionary

Dictionary in Python




The dictionary is one of the datatypes used in Python for storing a collection of data values.

It is an unordered collection of elements where each element is stored as key-value pairs. It is also referred to as a mapping between a key and a value

Some Important features:
  • Keys should be unique in the dictionary while values can be similar
  • Each key is separated from its value by a comma(:),and every element is separated by commas, and the entire dictionary is enclosed in curly braces
  • The values can be of any type, while keys should be of strings, numbers, or tuple type
  • Dictionary is immutable, which means we can add, update or delete the existing element in the string


Example:
{"tutorial":"Dictionaries"}


Creating Dictionary




Dictionary can be created by simply enclosing key-value pairs inside curly braces {}, separated by a comma. The colon (:) is used to separate the key and the value in a pair

Creation of Dictionary

here {} represent empty dictionary

It can also be created using dict() method, by passing the comma-separated key: value pairs inside the dict() method

Creation of dictionary by dict() method


Accessing Dictionary




To access a value from dictionary,you have to use square brackets along with the key to obtain its value

Example:

Accessing Dictionary


If we try to access that key which does not exist in the dictionary,it will throw error as:

Accessing non-exist key


You can also access the value by get() method.This method will return the value of given key from a dictionary

Accessing dictionary by get()


Traversing Dictionary




In python, we can easily access each element of a dictionary by using a for loop

Example:

Traversing dictionary


Appending values in the dictionary




As dictionaries are immutable, we can add new elements to existing dictionaries, extend them with single pairs, or combine two of them into one

Syntax:
Dictionary_name[key_name]=value


Example:
Appending values of a dictionary

Updating values in the dictionary




We can also update the dictionary by changing key-value pairs or by combining another dictionary with an existing one

Syntax:
Dictionary_name[key_name]=value


Example:
Updating values of a dictionary


We can also update the existing pairs in the dictionary,and can merge the keys and values of one dictionary with another and if same key is there then it overwrites it value,all this is done by update()method

Updating values of a dictionary by update() method


Deleting element in the dictionary




To remove an element from the existing dictionary, we can use the pop() method and the del command.

We can use these methods ,to delete the single element or the last element or delete the entire dictionary only.

The following methods are:

Del command




This command is used to delete an element from a dictionary. The format to delete an element from a dictionary by using del is:

Del dictionary_name[key_name]


No value is returned when using the del method

Example:

Deleting item in a dictionary by del command

We can also delete the entire dictionary by using del keyword followed by dictionary name

Del dictionary_name




Using pop() method




This method is also used to delete an element from a dictionary. The format to delete an element from a dictionary by using the pop() method is:

Dictionary_name.pop(key_name)

This method will not only delete the element from the existing dictionary but also return the deleted element

Deleting item in a dictionary pop()

Membership Operators




In python, we can also check whether a particular key exists in the given dictionary or not using membership operators. These operators are ‘in’ and ‘not in’

‘in’ operator: This operator returns true or false depending on whether the given key exists in a dictionary

'not in’ operator. This operator returns true when that given key does not exist in the dictionary

Example:

Membership Operators in a dictionary


Basic Dictionary functions





len()

This method is used to return number of key-value pairs in the dictionary

Syntax:
len(d)
d is a dictionary

For Example:

len function of a dictionary

Back to table


clear()

This method is to remove all key-value pairs in the dictionary

Syntax:
d.clear()
d is a dictionary

Example:

clear function of a dictionary

Back to table


get()

This method is used to provide value for the given key.If that given key is not available ,then it returns default value as None

Syntax:
Dict_name.get(key,default =None)
Key-This is the key to be searched in the dictionary
Default-This is the value to be returned when that given key is not found in the dictionary

Example:

get function of a dictionary

Back to table


Items()

This method returns all the items of dictionary as a list of tuple having key-value pairs

Syntax:
d.items()
d is a dictionary

Example:

item function in a dictionary



keys()

This method returns all the list of keys in a dictionary

Syntax:
d.keys()
d is a dictionary

Example:

keys function in a dictionary



Values()

This method returns all the list of values from key-value pairs in a dictionary

Syntax:
d.values()
d is a dictionary

Example:

values function in a dictionary



copy()

This method is used to create copy of the dictionary

Syntax:
d.copy()
d is a dictionary

Example:

copy function in a dictionary



popItem()

This method is used to delete last element from dictionary and returns that deleted element

Syntax:
d.popitem()
d is a dictionary

Example:

popitem function of a dictionary



setDefault()

This method returns the value of the given key.If the key does not exist ,it inserts the key with the default value

This method returns:
  • Value of the key,when it is in dictionary
  • None,if key is not in the dictionary and default value is not specified
  • Default_value,if the key is not in the dictionary and default value is specified

Example:

setdefault function of a dictionary



max() and min()

This max() methods returns key with the maximum value in the dictionary

And min() method returns key with the minimum value in the dictionary

Syntax:
Dictionary.max()
Dictionary.min()

Example:

max and min function of a dictionary



sorted()

This method is used to sort the elements in the dictionary by its key or values

Syntax:
d.sorted()
d is the dictionary

Example:

sorted function of a dictionary



Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com