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?
    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



    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?
    my_tuple = (1, 2, 3, 4)
    my_tuple.append( (5, 6, 7) )
    print len(my_tuple)


    Error !

    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?
    t = (1, 2)
    print 2 * t


    (1, 2, 1, 2)

    Asterick Operatr (*) operator concatenates tuple.

  4. What is the output of the following program?
    d1 = {"john":40, "peter":45}
    d2 = {"john":466, "peter":45}
    print d1 > d2



    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?
    my_tuple = (6, 9, 0, 0)
    my_tuple1 = (5, 2, 3, 4)
    print my_tuple > my_tuple1



    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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.