write a code to loop through a list of numbers and print it

In this article you will be learning about a python program loop through a list of numbers and print it. There are multiple ways you can write this program the input list may be either declared in program itself or requested from the user


Python Program to loop through a list of numbers and print

The below program the list of numbers are initialized in the program itself during the list declaration. The list of numbers are then traversed through each element using a for loop and the number gets printed using print.

number_list = [343, 43545, 58554, 873872, 283728, 82732, 23234, 87485]

for x in number_list:

        print("%d " %(x))

 

OUTPUT:

343
43545
58554
873872
283728
82732
23234
87485

 

 

Python Program to accept user input list of numbers, loop through and print

The python program below requests the user to enter the numbers, store them as a list of numbers. The list of numbers are then traversed through each element using a for loop and the number gets printed using print function call.

number_list = []
while True:
        x = input("Please enter the number : ")
        if x == "":
                break
        else:
                number_list.append(int(x))
print("The list of numbers you entered are ...")
for x in number_list:
        print("%d " %(x))

 

 

OUTPUT 1:

Please enter the number : 1
Please enter the number : 2
Please enter the number : 3
Please enter the number :
The list of numbers you entered are ...
1
2
3



OUTPUT 2:

Please enter the number : 342
Please enter the number : 563
Please enter the number : 8923
Please enter the number : 327821321
Please enter the number : 2391283
Please enter the number : 23829183921898321
Please enter the number : 1232173921783217
Please enter the number : 2389218321
Please enter the number : 2321
Please enter the number : 43242
Please enter the number :
The list of numbers you entered are ...
342
563
8923
327821321
2391283
23829183921898321
1232173921783217
2389218321
2321
43242

Post a Comment

0 Comments