This program demonstrate how to display elements inside a list using for and while loop
Create a list of numbers
num = [1,2,3,4,5,6,7,8,9]
Calculate number of elements in a list using len() function
l = len(num)
print('Number of elements in a list:',l)
Create a variable i and assign 0 value to it
i = 0
Using while loop to display each element of a list
while i < l:
print(num[i])
i+=1
Using for loop to display each elements of a list
for n in num:
print(n)
Output
Number of elements in a list: 9
Displaying elements using while loop
1
2
3
4
5
6
7
8
9
Displaying elements using for loop
1
2
3
4
5
6
7
8
9
Complete Code
num = [1,2,3,4,5,6,7,8,9]
l = len(num)
print('Number of elements in a list:',l)
i = 0
print('Displaying elements using while loop')
while i < l:
print(num[i])
i+=1
print('Displaying elements using for loop')
for n in num:
print(n)