Difficulty level : Intermediate
Predict the output of following Python Programs.
Explanation: Instantiation of the class “Acc” automatically calls the method __init__ and passes the object as the self parameter. 111 is assigned to data attribute of the object called id.
The value “555” is not retained in the object as it is not assigned to a data attribute of the class/object. So, the output of the program is “111”
0 1 4 5
Explanation: If only single argument is passed to the range method, Python considers this argument as the end of the range and the default start value of range is 0. So, it will print all the numbers starting from 0 and before the supplied argument.
For the second for loop the starting value is explicitly supplied as 4 and ending is 5.
1 2 3 4
Explanation: The function “filter” will return all items from list values which return True when passed to the function “checknums”. “checknums” will check if the value is in the set. Since all the numbers in the set come from the values list, all of the original values in the list will return True.
Explanation: The task of “len” function is to return number of keys in a dictionary. Here 3 keys are added to the dictionary “country” using the “addToCounter” function.
Please note carefully – The keys to a dictionary are case sensitive.
Try yourself: What happens If same key is passed twice??
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.