Python Array Example - Create array with Random Integer Numbers

Published November 16, 2021

In this Python array example tutorial, "Create an Array which will store integeres. Generate Random numbers and add them in created list and finally display the list items"

Let's write program

from array import *
import random
nums=array('i',[])

for i in range(0,5):
   num=random.randint(1,100)
   nums.append(num)
for i in nums:
   print(i)

 

In the above example

  • Created an array nums, nums=array('i',[])
  • Then written loop statement to add items into array upto 5 times
  • Generated Random numbers using randint() method between 0 to 100 randint(1,100)
  • This added generated random integer number into array by append() method
  • Then finally print the array

 

Output:

47
88
54
57
66

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1916 Views