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 i…


This content originally appeared on DEV Community and was authored by rrtutors

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

  • 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
    

    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

  • This content originally appeared on DEV Community and was authored by rrtutors


    Print Share Comment Cite Upload Translate Updates
    APA

    rrtutors | Sciencx (2021-11-18T18:12:32+00:00) Python array Example – Create two different arrays and join them. Retrieved from https://www.scien.cx/2021/11/18/python-array-example-create-two-different-arrays-and-join-them/

    MLA
    " » Python array Example – Create two different arrays and join them." rrtutors | Sciencx - Thursday November 18, 2021, https://www.scien.cx/2021/11/18/python-array-example-create-two-different-arrays-and-join-them/
    HARVARD
    rrtutors | Sciencx Thursday November 18, 2021 » Python array Example – Create two different arrays and join them., viewed ,<https://www.scien.cx/2021/11/18/python-array-example-create-two-different-arrays-and-join-them/>
    VANCOUVER
    rrtutors | Sciencx - » Python array Example – Create two different arrays and join them. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/18/python-array-example-create-two-different-arrays-and-join-them/
    CHICAGO
    " » Python array Example – Create two different arrays and join them." rrtutors | Sciencx - Accessed . https://www.scien.cx/2021/11/18/python-array-example-create-two-different-arrays-and-join-them/
    IEEE
    " » Python array Example – Create two different arrays and join them." rrtutors | Sciencx [Online]. Available: https://www.scien.cx/2021/11/18/python-array-example-create-two-different-arrays-and-join-them/. [Accessed: ]
    rf:citation
    » Python array Example – Create two different arrays and join them | rrtutors | Sciencx | https://www.scien.cx/2021/11/18/python-array-example-create-two-different-arrays-and-join-them/ |

    Please log in to upload a file.




    There are no updates yet.
    Click the Upload button above to add an update.

    You must be logged in to translate posts. Please log in or register.