Ask Question
29 June, 12:49

Write a function called first_last that takes a single parameter, seq, a sequence. first_last should return a tuple of length 2, where the first item in the tuple is the first item in seq, and the second item in tuple is the last item in seq. If seq is empty, the function should return an empty tuple. If seq has only one element, the function should return a tuple containing just that element.

+5
Answers (1)
  1. 29 June, 13:02
    0
    The Python code with the function is given below. Testing and output gives the results of certain chosen parameters for the program

    Explanation:

    def first_last (seq):

    if (len (seq) = = 0):

    return ()

    elif (len (seq) = = 1):

    return (seq[0],)

    else:

    return (seq[0], seq[len (seq) - 1])

    #Testing

    print (first_last ([]))

    print (first_last ([1]))

    print (first_last ([1,2,3,4,5]))

    # Output

    ()

    (1,)

    (1, 5)
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a function called first_last that takes a single parameter, seq, a sequence. first_last should return a tuple of length 2, where the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers