简体   繁体   中英

New to Python need to create a function [on hold]

Given a list of numbers that is at least length of 2, write a function called XT that returns the sum of the first 2 elements in the list.

With such a question google would be the better source of information.

def xt(lst):
    return lst[0] + lst[1]

print(xt([1, 2, 3]))
# 3

Function names should be lowercase.

take

sum(myList[:2])

or

myList[0] + myList[1]

and put it into "{approach}"

getSum = lambda myList: {approach}

or

def getSum(myList):
    return {approach}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM