Output of Python program | Set 17

Prerequisite – Tuples and Dictionaryin Python
Predict the output of the following Python programs.

  1. What is the output of the following program?
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    numberGames = {}
    numberGames[(1,2,4)] = 8
    numberGames[(4,2,1)] = 10
    numberGames[(1,2)] = 12
       
    sum = 0
    for k in numberGames:
        sum += numberGames[k]
       
    print len(numberGames) + sum

    chevron_right

    
    

    Output:

    33
    

    Explanation:
    Tuples can be used for keys into dictionary. The tuples can have mixed length and the order of the items in the tuple is considered when comparing the equality of the keys.

  2. What is the output of the following program?
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    my_tuple = (1, 2, 3, 4)
    my_tuple.append( (5, 6, 7) )
    print len(my_tuple)

    chevron_right

    
    

    Output:

    Error !
    

    Explanation:
    Tuples are immutable and don’t have an append method as in case of Lists.Hence an error is thrown in this case.

  3. What is the output of the following program?
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    t = (1, 2)
    print 2 * t

    chevron_right

    
    

    Output:

    (1, 2, 1, 2)
    

    Explanation:
    Asterick Operatr (*) operator concatenates tuple.

  4. What is the output of the following program?
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    d1 = {"john":40, "peter":45}
    d2 = {"john":466, "peter":45}
    print d1 > d2

    chevron_right

    
    

    Output:

    False
    

    Explanation:
    Arithmetic operator less than ( < ) or greater than ( > ) can be used with dictionaries and each corresponding key with its values are compared

  5. What is the output of the following program?
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    my_tuple = (6, 9, 0, 0)
    my_tuple1 = (5, 2, 3, 4)
    print my_tuple > my_tuple1

    chevron_right

    
    

    Output:

    True
    

    Explanation:
    Each elements of the tuples are compared one by one and if maximum number of elements are there in tuple1 which are greater of equal to corresponding element of tuple2 then tuple1 is said to be grater than tuple2.

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


Article Tags :

Be the First to upvote.


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