What is the usage of items in Python?

In Python, the items() function is a method used to return all key-value pairs of a dictionary. It can be applied to dictionary objects, and it returns a list composed of tuples, where each tuple contains one key from the dictionary and its corresponding value.

Here is an example of using the items() function:

my_dict = {"apple": 2, "banana": 3, "orange": 4}

# 使用items()函数获取字典中的键值对
items = my_dict.items()

# 打印键值对
for item in items:
    print(item)

# 输出:
# ("apple", 2)
# ("banana", 3)
# ("orange", 4)

In this example, the items() function returns the key-value pairs from the dictionary my_dict as tuples and stores them in the items variable. Then, we use a loop to iterate through the items list and print each key-value pair.

bannerAds