Open In App
Related Articles

Output of Python Program | Set 3

Improve Article
Save Article
Like Article

Difficulty level : Intermediate

Predict the output of following Python Programs.

Program 1:

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



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:

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


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:

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



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


Program 4:

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



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.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 11 Sep, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials