Difficulty level : Intermediate
Predict the output of following Python Programs.
The index position -1 represents either the last element in a list or the last character in a String. In the above given list of names “nameList”, the index 1 represents the second element i.e, the second string “Pratik” and the index -1 represents the last character in the string “Pratik”. So, the output is “k”.
An Exception is thrown, ValueError: 'GeeksforGeeks' is not in list
The task of index is to find the position of a supplied value in a given list. In the above program the supplied value is “GeeksforGeeks” and list is nameList. As GeeksforGeeks is not present in the list, an exception is thrown.
The task of append() method is to append a passed obj into an existing list. But instead of passing a list to the append method will not merge the two lists, the entire list which is passed is added as an element of the list. So the output is 5.
In Python, everything is a reference and references are passed by value. Parameter passing in Python is same as reference passing in Java. As a consequence, the function can modify the value referred by passed argument, i.e. the value of the variable in the caller’s scope can be changed. Here the task of the function “addToList” is to add an element 10 in the list, So this will increase the length of list by 1. So the output of program is 5.
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 email@example.com. 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.
- Output of Python Program | Set 1
- Output of Python program | Set 5
- Output of python program | Set 2
- Output of Python Program | Set 3
- Output of Python program | Set 17
- Output of python program | Set 14 (Dictionary)
- Output of Python program | Set 15 (Loops)
- Output of python program | Set 11(Lists)
- Output of Python program | Set 6 (Lists)
- Output of python program | Set 15 (Modules)
- Output of Python program | Set 16 (Threads)
- Output of python program | Set 13(Lists and Tuples)
- Output of python program | Set 12(Lists and Tuples)
- Output of C++ Program | Set 20
- Output of C++ Program | Set 19