Python dict Data Type (PYTHON FUNDAMENTALS)

Created with Sketch.

Python dict Data Type

  • If we want to represent a group of values as key-value pairs then we should go for
    dict data type.
  • Eg: d = {101:’durga’,102:’ravi’,103:’shiva’}
  • Duplicate keys are not allowed but values can be duplicated. If we are trying to
    insert an entry with the duplicate key then old value will be replaced with new value.
>>> d={101:'durga',102:'ravi',103:'shiva'} 
>>> d[101]='sunny' 
>>> d 
{101: 'sunny', 102: 'ravi', 103: 'shiva'} 

We can create empty dictionary as follows 
 d={ } 
We can add key-value pairs as follows 
d['a']='apple' 
d['b']='banana' 
print(d)

Note: dict is mutable and the order won’t be preserved.

Note:
1) In general we can use bytes and bytearray data types to represent binary information
like images, video files etc
2) In Python2 long data type is available. But in Python3 it is not available and we can
represent long values also by using int type only.
3) In Python there is no char data type. Hence we can represent char values also by using
str type.

Leave a Reply

Your email address will not be published. Required fields are marked *