Write a Code to Loop Through a List of Numbers And Print it in Python

Hi, in this article you will learn about a python program to loop through a list of numbers and print the numbers on standard output.

I will be using a for loop to iterate all the items present in the list and print it.

 

 

Write a Code to Loop Through a List of Numbers And Print it in Python

 

 


Write a Code to Loop Through a List of Numbers And Print it in Python

To iterate all the numbers present in the list first we have to get the list containing numbers from the list. Take a variable length and ask the user to enter list length.

Take an empty list of numbers, use the for loop to iterate from 0 to list length and ask the user to enter numbers using input() function call. Once the user enters a number perform type conversion and append it to a list of numbers.

length = int(input("Enter List Length : "))
numbers = []
for _ in range(length):
   temp = int(input("Enter List Item : "))
   numbers.append(temp)




Above is the method to get the list of numbers into your program during runtime. Now take a for loop and use the print statement to iterate all the numbers present in the list and print it.

print("Looping Through a List of Numbers And Printing it ...")
for number in numbers:
   print(number)




Complete Python Code
==========================
length = int(input("Enter List Length : "))
numbers = []
for _ in range(length):
   temp = int(input("Enter List Item : "))
   numbers.append(temp)
 
print("Looping Through a List of Numbers And Printing it ...")
for number in numbers:
   print(number)

 

 


Output
===============
Enter List Length : 4
Enter List Item : 21
Enter List Item : 12
Enter List Item : 34
Enter List Item : 44
Looping Through a List of Numbers And Printing it ...
21
12
34
44




Output
===============
Enter List Length : 10
Enter List Item : 1
Enter List Item : 2
Enter List Item : 3
Enter List Item : 4
Enter List Item : 5
Enter List Item : 6
Enter List Item : 7
Enter List Item : 8
Enter List Item : 9
Enter List Item : 10
Looping Through a List of Numbers And Printing it ...
1
2
3
4
5
6
7
8
9
10





Write a Code to Loop Through a List of Numbers And Print it in Python using while loop

Using while loop is quite tricky because while loop does not handle the Index Error, I need to handle it using try and except block, I will be initializing the index and using the infinite while loop and using try block to print the numbers. Then using except block catch the index error and exit the infinite loop

 



Python complete program using while loop
==========================================
length = int(input("Enter List Length : "))
numbers = []
for _ in range(length):
   temp = int(input("Enter List Item : "))
   numbers.append(temp)

print("Looping Through a List of Numbers And Printing it ...")
index = 0
while True:
   try:
       print(numbers[index])
   except IndexError:
       break
   index += 1





Conclusion
==============

Execute the above python program by providing a random list of numbers and note down the result.

Comment it down below if you have queries regarding the above python program

.

Post a Comment

0 Comments