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 the dictionary to access dictionary elements, you can use square brackets familiar with the key to getting its value.

How to access data from the python dictionary

Note – There is no indexing because of unordered collections of data

print(user1['name'])    

# you cannot do like this

Click here to know more

which types of data python dictionaries can store?

user_info = {

'name' : 'pramod',

'age' : 22 ,

'rollno' :'17691a05b9',

'class' : 'B-Tech'}

type(usser_info)

 print(user_info['name'])

 print(user_info['age'])

 print(user_info['rollno'])

 print(user_info['class'])

Click here to know more.

How to add data to an empty dictionary

user_info2 = {}

user_info2['name'] = 'prem kumar'

user_info2['age'] = 22

 print(user_info2)

Recommended Post:

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment