Write a python program to create a list of 5 students and print the list in reverse
order.
Answers
Answered by
5
Answer:
The given code is written in Python.
list=list()
print("Enter names of 5 students in the list...")
for i in range(5):
list.append(input(">> "))
print("Given List:",list)
print("Reversed List:",list[::-1])
Explanation:
- The given problem is solved using the concept of list slicing.
- Syntax: listName[start:end:step].
- When step value is not mentioned, it is taken as 1.
- Here, start and end value is not mentioned. So, start value is taken as 0 and end value is 4 (length of list - 1).
- As the step value is -1. elements will be displayed in reversed order.
See the attachment for output.
•••♪
Attachments:
Similar questions