DEV Community

rrtutors
rrtutors

Posted on • Originally published at rrtutors.com

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)
Enter fullscreen mode Exit fullscreen mode

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
    
    Enter fullscreen mode Exit fullscreen mode

    Python Array Examples

  • Ask the user to enter numbers. If the user enter number between 10 to 100 store them in array other wise display message "Outside the Range". If the list contains five items then display message "Thank you " and disply entered list items Python Array Example - Create array with fixed size elements
  • Create an Array which contains 10 numbers (one can be multiple times), display whole array to user, then ask user to enter one of the numbers from the array and then display a message that how many times that number appears in the list Python Array Example - Check array elements availability count
  • Ask the user to Enter five integeres, then Store them in an array. After that sort the list and display them in reveres order Python Array Example - Insert and sort the array items
  • Create an Array which will store integeres. Generate Random numbers and add them in created list and finally display the list items Python Array Example - Create array with Random Integer Numbers
  • Discussion (0)