python 返回字典的值_Python返回多个值–如何返回元组,列表或字典

本文介绍了如何在Python中通过函数返回多个值,包括使用元组、列表和字典作为数据结构。元组是不可变有序序列,而列表是可变有序序列。字典则包含键值对,可以通过键来获取对应的值。文章通过示例展示了如何创建和使用这些数据结构来返回多个值。
摘要由CSDN通过智能技术生成

python 返回字典的值

You can return multiple values from a function in Python.

您可以从Python中的函数返回多个值。

To do so, return a data structure that contains multiple values, like a list containing the number of miles to run each week.

为此,返回一个包含多个值的数据结构,例如包含每周运行里程数的列表。

def miles_to_run(minimum_miles):
   week_1 = minimum_miles + 2
   week_2 = minimum_miles + 4
   week_3 = minimum_miles + 6
   return [week_1, week_2, week_3]
 
print(miles_to_run(2))
# result: [4, 6, 8]

Data structures in Python are used to store collections of data, which can be returned from  functions. In this article, we’ll explore how to return multiple values from these data structures: tuples, lists, and dictionaries.

Python中的数据结构用于存储数据集合,这些数据可以从函数中返回。 在本文中,我们将探讨如何从这些数据结构中返回多个值:元组,列表和字典。

元组 (Tuples)

A tuple is an ordered, immutable sequence. That means, a tuple can’t change.

元组是有序的,不变的序列。 这意味着,元组不能改变。

Use a tuple, for example, to store information about a person: their name, age, and location.

例如,使用元组来存储有关一个人的信息:其姓名,年龄和位置。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值