How to Creating a dictionary in Python datatypes
three ways to create a dictionary.
Using curly brackets: The dictionaries are created by enclosing the comma-separated Key: Value pairs inside the {} curly brackets. The colon ‘:‘ is used to separate the key and value in a pair.
ex:
employee = {"empname": "raemsh", "jobdesc": "Software Developer", "salary":88000}
print(employee)
Using dict() constructor: Create a dictionary by passing the comma-separated key: value pairs inside the dict().
ex:
emp=dict({"empname":"krishna","jobdesc":"Python Developer","salaru":120000})
print(emp)
Using sequence having each item as a pair (key-value)
ex:
emp=dict([("empname","ravikrishna"),("jobdesc","software developer"),("salary",130000)])
print(emp)
# create dictionary with value as a list
ex:
emp = {"empname": "mohan rao", "empnumber": [987, 654,3210]}
print(emp)
Comments
Post a Comment