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.
Explanation: To access values in lists, we use the square brackets for slicing along with the index or indices to obtain required value available at that index.For N items in a List MAX value of index will be N-1. Statement 1 : This will print item located at index 0 in Output. Statement 2 : This will print item located at index -2 i.e.second last element in Output. Statement 3 : This will print items located from index 1 to end of the list. Statement 4 : This will print items located from index 1 to 4 of the list.
Program 2
Python
list1 =['physics', 'chemistry', 1997, 2000]
print"list1[1][1]: ", list1[1][1] #statement 1
print"list1[1][-1]: ", list1[1][-1] #statement 2
Output:
list1[1][1]: h
list1[1][-1]: y
Explanation: In python we can slice a list but we can also slice a element within list if it is a string. The declaration list[x][y] will mean that ‘x’ is the index of element within a list and ‘y’ is the index of entity within that string.
Explanation: When addition(+) operator uses list as its operands then the two lists will get concatenated. And when a list id multiplied with a constant k>=0 then the same list is appended k times in the original list.
Program 4
Python
list1 =range(100, 110) #statement 1
print"index of element 105is: ", list1.index(105) #statement 2
Output:
index of element 105 is : 5
Explanation:Statement 1 : will generate numbers from 100 to 110 and append all these numbers in the list. Statement 2 : will give the index value of 105 in the list list1.
Program 5
Python
list1 =[1, 2, 3, 4, 5]
list2 =list1
list2[0] =0;
print"list1=: ", list1 #statement 2
Output:
list1= : [0, 2, 3, 4, 5]
Explanation: In this problem, we have provided a reference to the list1 with another name list2 but these two lists are same which have two references(list1 and list2). So any alteration with list2 will affect the original list.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
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!
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Please go through our recently updated Improvement Guidelines before submitting any improvements.
This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab.
You will be notified via email once the article is available for improvement.
Thank you for your valuable feedback!
Please go through our recently updated Improvement Guidelines before submitting any improvements.
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
Please Login to comment...