The sea does not like to be restrained.
Emektar Üye
- Katılım
- 15 Tem 2021
- Mesajlar
- 1,724
- Çözümler
- 86
- Tepki puanı
- 665
- Ödüller
- 10
- Yaş
- 25
- Sosyal
4 HİZMET YILI
In Python, a dictionary is a collection of key-value pairs. Each key is unique and is used to access its corresponding value. Here's an example of a simple dictionary:
In this example, we define a dictionary called person with three key-value pairs. The keys are "name", "age", and "occupation", and the corresponding values are "Alice", 30, and "Engineer".
You can access the values of a dictionary using the keys. For example:
This would output:
You can also modify the values of a dictionary by assigning new values to its keys. For example:
This would output:
You can add new key-value pairs to a dictionary by assigning a value to a new key. For example:
This would output:
You can use the del keyword to delete a key-value pair from a dictionary. For example:
This would output:
You can also loop through a dictionary using a for loop. For example:
This would output:
Python:
person = {
"name": "Alice",
"age": 30,
"occupation": "Engineer"
}
In this example, we define a dictionary called person with three key-value pairs. The keys are "name", "age", and "occupation", and the corresponding values are "Alice", 30, and "Engineer".
You can access the values of a dictionary using the keys. For example:
Python:
print(person["name"])
print(person["age"])
print(person["occupation"])
This would output:
Python:
Alice
30
Engineer
You can also modify the values of a dictionary by assigning new values to its keys. For example:
Python:
person["age"] = 31
print(person["age"])
This would output:
Python:
31
You can add new key-value pairs to a dictionary by assigning a value to a new key. For example:
Python:
person["city"] = "New York"
print(person["city"])
This would output:
Python:
New York
You can use the del keyword to delete a key-value pair from a dictionary. For example:
Python:
del person["occupation"]
print(person)
This would output:
Python:
{'name': 'Alice', 'age': 31, 'city': 'New York'}
You can also loop through a dictionary using a for loop. For example:
Python:
for key in person:
print(key, person[key])
This would output:
Python:
name Alice
age 31
city New York