Dictionaries in Python.

1. What are dictionaries in Python

Dictionary is one of the data types which is used to store data in the pairs of key : data.
It is ordered.
It is mutable.
Keys should not be duplicated but data can be same.
Dictionaries are written with curly brack…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Keshav Jindal

1. What are dictionaries in Python

  • Dictionary is one of the data types which is used to store data in the pairs of key : data.

  • It is ordered.

  • It is mutable.

  • Keys should not be duplicated but data can be same.

  • Dictionaries are written with curly brackets.

2. Creating a Dictionary

  1. It is encloses in curly {} braces.
  2. Separated by a comma (,).
  3. It holds values in pairs of keys corresponding with data.
  4. Keys cannot be repeated and are immutable.
  5. Data can be of any Datatype.

2.1 Example

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

OUTPUT
{1: 'A', 2: 'B', 3: 'C'}

3. Accessing data of a dictionary
We can access the data by their respective keys.
3.1 Syntax

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

OUTPUT
A
4. Adding elements to a dictionary
Syntax:

dict = {1: 'A', 2: 'B', 3: 'C'}
print("old dictionary")
print(dict)
new_dict=dict[4]='a'
print("Updated dictionary")
print(dict)

OUTPUT:
old dictionary
{1: 'A', 2: 'B', 3: 'C'}
Updated dictionary
{1: 'A', 2: 'B', 3: 'C', 4: 'a'}


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Keshav Jindal


Print Share Comment Cite Upload Translate Updates
APA

Keshav Jindal | Sciencx (2022-12-29T15:18:40+00:00) Dictionaries in Python.. Retrieved from https://www.scien.cx/2022/12/29/dictionaries-in-python/

MLA
" » Dictionaries in Python.." Keshav Jindal | Sciencx - Thursday December 29, 2022, https://www.scien.cx/2022/12/29/dictionaries-in-python/
HARVARD
Keshav Jindal | Sciencx Thursday December 29, 2022 » Dictionaries in Python.., viewed ,<https://www.scien.cx/2022/12/29/dictionaries-in-python/>
VANCOUVER
Keshav Jindal | Sciencx - » Dictionaries in Python.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/29/dictionaries-in-python/
CHICAGO
" » Dictionaries in Python.." Keshav Jindal | Sciencx - Accessed . https://www.scien.cx/2022/12/29/dictionaries-in-python/
IEEE
" » Dictionaries in Python.." Keshav Jindal | Sciencx [Online]. Available: https://www.scien.cx/2022/12/29/dictionaries-in-python/. [Accessed: ]
rf:citation
» Dictionaries in Python. | Keshav Jindal | Sciencx | https://www.scien.cx/2022/12/29/dictionaries-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.