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"}
- 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
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
here {} represent empty dictionary
It can also be created using dict() method, by passing the comma-separated key: value pairs inside the
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:
If we try to access that key which does not exist in the dictionary,it will throw error as:
You can also access the value by get() method.This method will return the value of given key from a
dictionary
Traversing Dictionary
In python, we can easily access each element of a dictionary by using a for loop
Example:
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:
Dictionary_name[key_name]=value
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:
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
Dictionary_name[key_name]=value
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:
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
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:
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:
clear()
This method is to remove all key-value pairs in the dictionary
Syntax:
d.clear()
d is a dictionary
Example:
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:
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:
keys()
This method returns all the list of keys in a dictionary
Syntax:
d.keys()
d is a dictionary
Example:
Values()
This method returns all the list of values from key-value pairs in a dictionary
Syntax:
d.values()
d is a dictionary
Example:
copy()
This method is used to create copy of the dictionary
Syntax:
d.copy()
d is a dictionary
Example:
popItem()
This method is used to delete last element from dictionary and returns that deleted element
Syntax:
d.popitem()
d is a dictionary
Example:
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:
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:
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:
This method is used to return number of key-value pairs in the dictionary
Syntax:
len(d)
d is a dictionary
len(d)
d is a dictionary
For Example:
This method is to remove all key-value pairs in the dictionary
Syntax:
d.clear()
d is a dictionary
d.clear()
d is a dictionary
Example:
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
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:
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:
d.items()
d is a dictionary
This method returns all the list of keys in a dictionary
Syntax:
d.keys()
d is a dictionary
Example:
d.keys()
d is a dictionary
This method returns all the list of values from key-value pairs in a dictionary
Syntax:
d.values()
d is a dictionary
Example:
d.values()
d is a dictionary
This method is used to create copy of the dictionary
Syntax:
d.copy()
d is a dictionary
Example:
d.copy()
d is a dictionary
This method is used to delete last element from dictionary and returns that deleted element
Syntax:
d.popitem()
d is a dictionary
Example:
d.popitem()
d is a dictionary
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:
- 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
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:
Dictionary.max()
Dictionary.min()
This method is used to sort the elements in the dictionary by its key or values
Syntax:
d.sorted()
d is the dictionary
Example:
d.sorted()
d is the dictionary