How to add data to empty dictionary and types of data a dictionary can store

Which types of data a dictionary can store?

Variables can hold values of different data types. Python is a dynamically typed language hence we need not define the type of the variable while declaring it. The interpreter implicitly binds the value with its type.

Types of data – numbers, strings, list, dictionary

How to check the types of variables in Python?

Python enables us to check the type of variable used in the program. Python provides us the type() function which returns the type of the variable passed.

Dictionary Type

user = {}
print(type(user))Output:<class 'dict'>

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
<class 'dict'>

Process finished with exit code 0

Tuple Type

user = ()
print(type(user))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
<class 'tuple'>

Process finished with exit code 0

Integer Types

user = 10
print(type(user))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
<class 'int'>

Process finished with exit code 0

Types of String

user = 'Pramod Kumar Yadav'
print(type(user))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
<class 'str'>

Process finished with exit code 0

Boolean Type

user = True
print(type(user))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
<class 'bool'>

Process finished with exit code 0

Same ways you can check different data types in python easily.

How to add data into an empty dictionary?

To add data in an empty dictionary first we have to create an empty dictionary.

Example:

# create an empty dictionary
user1 = {}

#add key and value into an empty dictionay
user1['name'] = 'Pramod'

user1['age'] = 24

user1['Roll no'] = '17691a05b9'

user1['Class'] = 'B-Tech'

#print the value after added the data to an empty dict
print(user1)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
{'name': 'Pramod', 'age': 24, 'Roll no': '17691a05b9', 'Class': 'B-Tech'}

Process finished with exit code 0

Recommended Post:

Get Salesforce Answers

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