Output of Python programs | Set 8

Prerequisite – Lists in Python
Predict the output of the following Python programs.

  • Program 1
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']]
    print len(list)

    chevron_right

    
    

    Output:

    6
    

    Explanation:
    The beauty of python list datatype is that within a list, a programmer can nest another list, a dictionary or a tuple. Since in the code there are 6 items present in the list the length of the list is 6.



  • Program 2
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    list = ['python', 'learning', '@', 'Geeks', 'for', 'Geeks']
      
    print list[::]          
    print list[0:6:2]       
    print list[ :6: ]       
    print list[ :6:2]      
    print list[ ::3]       
    print list[ ::-2]     

    chevron_right

    
    

    Output:

    ['python', 'learning', '@', 'Geeks', 'for', 'Geeks']
    ['python', '@', 'for']
    ['python', 'learning', '@', 'Geeks', 'for', 'Geeks']
    ['python', '@', 'for']
    ['python', 'Geeks']
    ['Geeks', 'Geeks', 'learning']
    

    Explanation:
    In python list slicing can also be done by using the syntax listName[x:y:z] where x means the initial index, y-1 defines the final index value and z specifies the step size. If anyone of the values among x, y and z is missing the interpreter takes default value.

    Note:
    1. For x default value is 0 i.e. start of the list.
    2. For y default value is length of the list.
    3. For z default value is 1 i.e. every element of the list.

  • Program 3
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    d1 = [10, 20, 30, 40, 50]
    d2 = [1, 2, 3, 4, 5]
    print d1 - d1

    chevron_right

    
    

    Output:

    No Output
    

    Explanation:
    Unlike additon or relational operators not all the artihmatic operaters can use lists as their operands. Since – minus operator can’t take lists as its operand no output will be produced. Program will produce following error.

    TypeError: unsupported operand type(s) for -: 'list' and 'list'
  • Program 4
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    list = ['a', 'b', 'c', 'd', 'e']
    print list[10:]

    chevron_right

    
    

    Output:

    []
    

    Explanation:
    As one would expect, attempting to access a member of a list using an index that exceeds the number of members (e.g., attempting to access list[10] in the list above) results in an IndexError. However, attempting to access a slice of a list at a starting index that exceeds the number of members in the list will not result in an IndexError and will simply return an empty list.

  • Program 5
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    list = ['a', 'b', 'c']*-3
    print list

    chevron_right

    
    

    Output:

    []
    

    Explanation:
    A expression list[listelements]*N where N is a integer appends N copies of list elements in the original list. If N is a negetive integer or 0 output will be a empty list else if N is positive list elements will be added N times to the original list.

This article is contributed by Avinash Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Akanksha_Rai



Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.