Python Editor

Python | Lesson3 | Map

 

# Declaring a map (dictionary)
Employee = {"name": "David", "age": 25, "profession": "Engineer"}

# Accessing elements in a map
print(Employee["name"])
# Output: "David"
print(Employee["age"])
# Output: 25

# Modifying elements in a map
Employee["age"] = 25
print(Employee) # Output: {"name": "David", "age": 25, "profession": "Engineer"}

# Adding elements to a map
Employee["location"] = "New York"
print(Employee) # Output: {"name": "David", "age": 25, "profession": "Engineer",
# "location": "New York"}

# Removing elements from a map
del Employee["location"]
print(Employee) # Output: {"name": "David", "age": 25, "profession": "Engineer"}

# Finding the length of a map
print(len(Employee))
# Output: 3

# Iterating through a map
for mapKey in Employee:
    print(mapKey, Employee[mapKey])
# Output:
# name David
# age 25
# profession Engineer


No comments:

Post a Comment