In this program, we will print the last element of list using a while loop.
Steps to solve the program
- Take a list as input, create a variable and assign its value equal to 0.
- While the value of the variable is less than the length of the list add 1 to the variable.
- At the end of the loop, the variable’s value is equal to the length of the list.
- Print the last element of the list with the help of that variable and indexing.
list1 = ["s","q","a","t","o","o","l","s"]
count = 0
while count<len(list1):
count += 1
a = count
print("Last element of the list: ",list1[a-1])
Last element of the list: s
Output :