14
Python array Example - Create two different arrays and join them
Create two arrays one contains three numbers that the user enters and other containing a set of five random numbers. Join these two arrays and display this array
from array import *
import random
num1 = array('i', [])
num2 = array('i', [])
for i in range(0, 3):
num = int(raw_input("Enter a number: "))
num1.append(num)
print("Second Random Array")
for i in range(0, 5):
num = random.randint(1, 100)
num2.append(num)
print("Join two Arrays")
num1.extend(num2)
for i in num1:
print(i)
print("Sort Joined Array")
num1 = sorted(num1)
for i in num1:
print(i)
In the above example we create
Output
Enter a number: 12
Enter a number: 23
Enter a number: 45
Second Random Array
Join two Arrays
12
23
45
85
77
86
71
22
Sort Joined Array
12
22
23
45
71
77
85
86
Python Array Examples
14