函数定义需要序列吗? Python

问题描述 投票:-1回答:1

我正在尝试编写一个带有序列并返回第一个和第二个值的函数定义。我怀疑我的代码是错误的,因为它无法接受列表,但是我不确定。这是我的目标:

写一个名为first_and_second的函数定义,该函数定义按顺序接收并返回该顺序的第一个和第二个值作为列表

这是我遇到麻烦的代码:

def first_and_second(list):返回列表[0和1]

这里是我是否正确的测试:

断言first_and_second([1、2、3、4])== [1、2]断言first_and_second([“ python”,“ is”,“ awesome”])== [“ python”,“ is”]

python list function sequence definition
1个回答
0
投票
def first_and_second(list):
    return [list[0],list[1]]

def first_and_second(list):
    return list[0:2]
© www.soinside.com 2019 - 2024. All rights reserved.