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:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers