Dictionaries in Python

 Dictionaries in Python


Dictionaries are a collection of key-value pairs in Python. Here's an example:


python


person = {

  "name": "John",

  "age": 36,

  "country": "USA"

}


print(person)

In this example, person is a dictionary with three key-value pairs: "name" with a value of "John", "age" with a value of 36, and "country" with a value of "USA". We can access individual values in the dictionary using their keys:


python


print(person["name"])

This will print "John", since "name" is the key for the value "John".


We can also add new key-value pairs to a dictionary:


python


person["occupation"] = "engineer"

print(person)

This will add a new key-value pair to the person dictionary with a key of "occupation" and a value of "engineer".

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...