Python array Example - Create two different arrays and join them

Published November 16, 2021


In this python array example, 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

Let's write program

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

  • Array num1 with user enetered elements
  • Create Array num2 with random numbers
  • Then we joined two arrays using extend() method
  • Finally we sorted joined array using sorted() method

 

 

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

 


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

9 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions