Python Dictionary | How to access data and can store data


Python Dictionary | How to access data and can store data

Python Dictionary | How to access data and can store data                          Each key is separated by a colon (:) from its value, items are separated by commas, and the whole thing is enclosed in a curved brace. An empty dictionary with no items is written with only two curly braces, such as: {}.

Keys are unique within a dictionary while values ​​cannot be. The values ​​of a dictionary can be of any type, but the key must be of an immutable data type such as strings, numbers, or tuples.

Access value in dictionary
To access dictionary elements, you can use square brackets familiar with the key to get its value.

How to access data from python dictionary

Note – There is no indexing because of unordered

collections of data print(user1[‘name’])    

# you cannot do like this

which types of data a python dictionaries can store ?

 Anything eg – numbers , strings , list ,dictionary

user_info = {

‘name’ : ‘pramod’,

‘age’ : 22 ,

‘rollno’ :‘17691a05b9’,

‘class’ : ‘B-Tech’}

 print(user_info[‘name’])

 print(user_info[‘age’])

 print(user_info[‘rollno’])

 print(user_info[‘class’])

How to add data to empty dictionary

user_info2 = {}

user_info2[‘name’] = ‘prem kumar’user_info2[‘age’] = 22

 print(user_info2)

Python Dictionary | How to access data and can store data

Have any Question or Comment?

Leave a Reply

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

Categories