Output of Python Program | Set 3

Difficulty level : Intermediate

Predict the output of following Python Programs.

Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

class Geeks:
    def __init__(self, id):
        self.id = id
  
manager = Geeks(100)
  
manager.__dict__['life'] = 49
  
print manager.life + len(manager.__dict__)

chevron_right


Output:

51

Explanation : In the above program we are creating a member variable having name ‘life’ by adding it directly to the dictionary of the object ‘manager’ of class ‘Geeks’. Total numbers of items in the dictionary is 2, the variables ‘life’ and ‘id’. Therefore the size or the length of the dictionary is 2 and the variable ‘life’ is assigned a value ’49’. So the sum of the variable ‘life’ and the size of the dictionary is 49 + 2 = 51.

 

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

a = "GeeksforGeeks "
  
b = 13
  
print a + b

chevron_right


Output:

An error is shown.

Explanation : As you can see the variable ‘b’ is of type integer and the variable ‘a’ is of type string. Also as Python is a strongly typed language we cannot simply concatenate an integer with a string. We have to first convert the integer variable to the type string to concatenate it with a string variable. So, trying to concatenate an integer variable to a string variable, an exception of type “TypeError” is occurred.

 

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

dictionary = {}
dictionary[1] = 1
dictionary['1'] = 2
dictionary[1] += 1
  
sum = 0
for k in dictionary:
    sum += dictionary[k]
  
print sum

chevron_right


Output:

4

Explanation : In the above dictionary, the key 1 enclosed between single quotes and only 1 represents two different keys as one of them is integer and other is string. So, the output of the program is 4.


 

Program 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

dictionary = {1:'1', 2:'2', 3:'3'}
del dictionary[1]
dictionary[1] = '10'
del dictionary[2]
print len(dictionary)

chevron_right


Output:

2

Explanation : The task of the ‘del’ function is to remove key-value pairs from a dictionary. Initially the size of the given dictionary was 3. Then the key value pair for key 1 is first removed and then added back with a new value. Then the key value pair for key 2 is removed. So, finally the size of the dictionary is 2.

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

1


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