Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

Dictionaries in Python. – DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

1. What are dictionaries in Python

  • Dictionary is among the knowledge varieties which is used to retailer knowledge within the pairs of key : knowledge.

  • It’s ordered.

  • It’s mutable.

  • Keys shouldn’t be duplicated however knowledge could be identical.

  • Dictionaries are written with curly brackets.

2. Making a Dictionary

  1. It’s encloses in curly {} braces.
  2. Separated by a comma (,).
  3. It holds values in pairs of keys corresponding with knowledge.
  4. Keys can’t be repeated and are immutable.
  5. Information could be of any Datatype.

2.1 Instance

dict = {1: 'A', 2: 'B', 3: 'C'}
print(dict)
Enter fullscreen mode

Exit fullscreen mode

OUTPUT
{1: ‘A’, 2: ‘B’, 3: ‘C’}

3. Accessing knowledge of a dictionary
We are able to entry the information by their respective keys.
3.1 Syntax

dict = {1: 'A', 2: 'B', 3: 'C'}
print(dict[1])
Enter fullscreen mode

Exit fullscreen mode

OUTPUT
A
4. Including components to a dictionary
Syntax:

dict = {1: 'A', 2: 'B', 3: 'C'}
print("outdated dictionary")
print(dict)
new_dict=dict[4]='a'
print("Up to date dictionary")
print(dict)
Enter fullscreen mode

Exit fullscreen mode

OUTPUT:
outdated dictionary
{1: ‘A’, 2: ‘B’, 3: ‘C’}
Up to date dictionary
{1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘a’}

Add a Comment

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

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?