hamburger menu
All Coursesall course arrow
adda247
reward-icon
adda247
    arrow
    arrow
    arrow
    Consider the following assignment for a List L: L = [10, 20, 30, 40, 50, 60] Which print statement option will display the content in the following wa
    Question

    Consider the following assignment for a List L: L = [10, 20, 30, 40, 50, 60] Which print statement option will display the content in the following way? 60, 10, 20, 30, 40, 50

    A.

    print(L[-1] + L[1:])

    B.

    print(L[-1] + L[1:len(L)−1])

    C.

    print(L[len(L)−1] + L[1:len(L)])

    D.

    print(L[-1], L[1:])

    Correct option is D


    The print statement print(L[-1], L[1:]) will display the content of the list in the following way: 60, 10, 20, 30, 40, 50. Here's why:
    · L[-1] accesses the last element of the list, which is 60.
    · L[1:] accesses all elements starting from the second element, i.e., 10, 20, 30, 40, 50.
    · When combined in the print statement, it will display 60, 10, 20, 30, 40, 50.
    Important Key Points:
    1. Negative Indexing: L[-1] accesses the last element of the list, which is 60.
    2. List Slicing: L[1:] is a slice that gets all elements from index 1 to the end of the list, which is [10, 20, 30, 40, 50].
    3. Print Format: The , in the print statement ensures that the elements are printed with a space in between.
    Knowledge Booster:
    · Option (a): L[-1] + L[1:] would result in a TypeError because you cannot concatenate an integer (L[-1], which is 60) with a list (L[1:]). This would lead to a mismatch in types.
    · Option (b): L[-1] + L[1:len(L)-1] would also attempt to concatenate an integer with a list, which would cause an error. It also does not produce the correct desired output format.
    · Option (c): L[len(L)-1] + L[1:len(L)] would lead to a TypeError due to concatenating an integer with a list, similar to option (a). Also, this slicing wouldn't correctly rearrange the elements in the desired order.

    Free Tests

    Free
    Must Attempt

    UPTET Paper 1: PYP Held on 23rd Jan 2022 (Shift 1)

    languageIcon English
    • pdpQsnIcon150 Questions
    • pdpsheetsIcon150 Marks
    • timerIcon150 Mins
    languageIcon English
    Free
    Must Attempt

    UPTET Paper 2 Social Science : PYP Held on 23rd Jan 2022 (Shift 2)

    languageIcon English
    • pdpQsnIcon150 Questions
    • pdpsheetsIcon150 Marks
    • timerIcon150 Mins
    languageIcon English
    Free
    Must Attempt

    UPTET Paper 2 Maths & Science : PYP Held on 23rd Jan 2022 (Shift 2)

    languageIcon English
    • pdpQsnIcon150 Questions
    • pdpsheetsIcon150 Marks
    • timerIcon150 Mins
    languageIcon English
    test-prime-package

    Access ‘EMRS PGT’ Mock Tests with

    • 60000+ Mocks and Previous Year Papers
    • Unlimited Re-Attempts
    • Personalised Report Card
    • 500% Refund on Final Selection
    • Largest Community
    students-icon
    354k+ students have already unlocked exclusive benefits with Test Prime!
    Our Plans
    Monthsup-arrow