Share how to dictionary in python

nhansamlytuong

New member
..

** Cách sử dụng từ điển trong Python **

Từ điển là một cấu trúc dữ liệu lưu trữ dữ liệu trong các cặp giá trị khóa.Các khóa là duy nhất và các giá trị có thể thuộc bất kỳ loại nào.Từ điển rất hữu ích để lưu trữ dữ liệu có liên quan đến nhau.Ví dụ: bạn có thể sử dụng từ điển để lưu trữ tên và tuổi của bạn bè.

Để tạo từ điển, bạn sử dụng cú pháp sau:

`` `
Từ điển = {key1: value1, key2: value2}
`` `

Ví dụ: mã sau đây tạo ra một từ điển lưu trữ tên và tuổi của ba người bạn:

`` `
Bạn bè = {"John": 20, "Mary": 21, "Peter": 22}
`` `

Bạn có thể truy cập các giá trị trong từ điển bằng cú pháp sau:

`` `
Từ điển [khóa]
`` `

Ví dụ: mã sau in tuổi của Mary:

`` `
In (bạn bè ["Mary"])
`` `

Bạn cũng có thể thêm các cặp giá trị khóa mới vào từ điển bằng cú pháp sau:

`` `
Từ điển [khóa] = giá trị
`` `

Ví dụ: mã sau đây thêm một cặp giá trị khóa mới vào từ điển `friends`:

`` `
Bạn bè ["Susan"] = 23
`` `

Bạn cũng có thể xóa các cặp giá trị khóa khỏi từ điển bằng cú pháp sau:

`` `
Từ điển Del [khóa]
`` `

Ví dụ: mã sau đây xóa cặp giá trị khóa cho "John" từ từ điển `friends`:

`` `
Del Friends ["John"]
`` `

Từ điển là một cấu trúc dữ liệu mạnh mẽ có thể được sử dụng để lưu trữ nhiều dữ liệu khác nhau.Chúng rất dễ sử dụng và có thể rất hiệu quả.

** Hashtags: **

* #Python
* #Từ điển
* #Cấu trúc dữ liệu
* #Programming
* #tutorial
=======================================
#Python #dictionary #datastructure #Programming #tutorial

**How to Use a Dictionary in Python**

A dictionary is a data structure that stores data in key-value pairs. The keys are unique, and the values can be of any type. Dictionaries are very useful for storing data that is related to each other. For example, you could use a dictionary to store the names and ages of your friends.

To create a dictionary, you use the following syntax:

```
dictionary = {key1: value1, key2: value2}
```

For example, the following code creates a dictionary that stores the names and ages of three friends:

```
friends = {"John": 20, "Mary": 21, "Peter": 22}
```

You can access the values in a dictionary using the following syntax:

```
dictionary[key]
```

For example, the following code prints the age of Mary:

```
print(friends["Mary"])
```

You can also add new key-value pairs to a dictionary using the following syntax:

```
dictionary[key] = value
```

For example, the following code adds a new key-value pair to the `friends` dictionary:

```
friends["Susan"] = 23
```

You can also delete key-value pairs from a dictionary using the following syntax:

```
del dictionary[key]
```

For example, the following code deletes the key-value pair for "John" from the `friends` dictionary:

```
del friends["John"]
```

Dictionaries are a powerful data structure that can be used to store a variety of data. They are easy to use and can be very efficient.

**Hashtags:**

* #Python
* #dictionary
* #datastructure
* #Programming
* #tutorial
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top