Output of Python programs | Set 8 Last Updated : 06 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list, a dictionary or a tuple. Since in the code there are 6 items present in the list the length of the list is 6. Program 2 Python list = ['python', 'learning', '@', 'Geeks', 'for', 'Geeks'] print list[::] print list[0:6:2] print list[ :6: ] print list[ :6:2] print list[ ::3] print list[ ::-2] Output: ['python', 'learning', '@', 'Geeks', 'for', 'Geeks']['python', '@', 'for']['python', 'learning', '@', 'Geeks', 'for', 'Geeks']['python', '@', 'for']['python', 'Geeks']['Geeks', 'Geeks', 'learning']Explanation: In python list slicing can also be done by using the syntax listName[x:y:z] where x means the initial index, y-1 defines the final index value and z specifies the step size. If anyone of the values among x, y and z is missing the interpreter takes default value.Note: 1. For x default value is 0 i.e. start of the list. 2. For y default value is length of the list. 3. For z default value is 1 i.e. every element of the list. Program 3 Python d1 = [10, 20, 30, 40, 50] d2 = [1, 2, 3, 4, 5] print d1 - d1 Output: No OutputExplanation: Unlike addition or relational operators not all the arithmetic operators can use lists as their operands. Since - minus operator can't take lists as its operand no output will be produced. Program will produce following error. TypeError: unsupported operand type(s) for -: 'list' and 'list' Program 4 Python list = ['a', 'b', 'c', 'd', 'e'] print list[10:] Output: []Explanation: As one would expect, attempting to access a member of a list using an index that exceeds the number of members (e.g., attempting to access list[10] in the list above) results in an IndexError. However, attempting to access a slice of a list at a starting index that exceeds the number of members in the list will not result in an IndexError and will simply return an empty list. Program 5 Python list = ['a', 'b', 'c']*-3 print list Output: []Explanation: A expression list[listelements]*N where N is a integer appends N copies of list elements in the original list. If N is a negative integer or 0 output will be a empty list else if N is positive list elements will be added N times to the original list. Comment More infoAdvertise with us Next Article Output of Python programs | Set 8 A Avinash Kumar Singh Improve Article Tags : Python Python Programs Practice Tags : python Similar Reads Python Quiz These Python quiz questions are designed to help you become more familiar with Python and test your knowledge across various topics. From Python basics to advanced concepts, these topic-specific quizzes offer a comprehensive way to practice and assess your understanding of Python concepts. These Pyt 3 min read Python MCQ (Multiple Choice Questions) with Answers Python is a free open-source, high-level and general-purpose with a simple and clean syntax which makes it easy for developers to learn Python. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Sof 3 min read Output of Python Program | Set 1 Predict the output of following python programs: Program 1:Python r = lambda q: q * 2 s = lambda q: q * 3 x = 2 x = r(x) x = s(x) x = r(x) print (x) Output:24Explanation : In the above program r and s are lambda functions or anonymous functions and q is the argument to both of the functions. In firs 2 min read Output of python program | Set 2 Difficulty level : IntermediatePredict the output of following Python Programs. Program 1: Python3 class Acc: def __init__(self, id): self.id = id id = 555 acc = Acc(111) print (acc.id) Output: 111 Explanation: Instantiation of the class "Acc" automatically calls the method __init__ and passes the o 2 min read Output of Python Program | Set 3 Difficulty level : Intermediate Predict the output of following Python Programs. Program 1: Python3 class Geeks: def __init__(self, id): self.id = id manager = Geeks(100) manager.__dict__['life'] = 49 print (manager.life + len(manager.__dict__)) Output:51 Explanation : In the above program we are cr 2 min read Output of Python Program | Set 4 Difficulty level : Intermediate Predict the output of the following Python Programs. Program 1: Python nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv'] print nameList[1][-1] Output: k Explanation: The index position -1 represents either the last element in a list or the last character in a String. In 2 min read Output of Python program | Set 5 Predict the output of the following programs: Program 1: Python def gfgFunction(): "Geeksforgeeks is cool website for boosting up technical skills" return 1 print (gfgFunction.__doc__[17:21]) Output:coolExplanation: There is a docstring defined for this method, by putting a string 3 min read Output of Python program | Set 6 (Lists) Prerequisite - Lists in Python Predict the output of the following Python programs. These question set will make you conversant with List Concepts in Python programming language. Program 1 Python list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]: 3 min read Output of Python programs | Set 7 Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ", var1[0] # statement 1 print "var2[1:5 3 min read Output of Python programs | Set 8 Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list, 3 min read Like