Program to copy all elements of one array into another array in Python

Program to copy all elements of one array into another array in Python. This program contains for loop, Array initialisation and print function. This is for Beginners, class 11,12 and B Tech computer science students. Download source code for free.

Program to copy all elements of one array into another array

What is an Array ?

An array is defined as a collection of items that are stored at contiguous memory locations. It is a container which can hold a fixed number of items, and these items should be of the same type.

Source code :

     
arr1 = [1, 2, 3, 4, 5];     
     
    
arr2 = [None] * len(arr1);    
     
    
for i in range(0, len(arr1)):    
    arr2[i] = arr1[i];     
     
     
print("Elements of original array: ");    
for i in range(0, len(arr1)):    
   print(arr1[i]),    
     
print();    
     
print("Elements of new array: ");    
for i in range(0, len(arr2)):    
   print(arr2[i]),  

Source code Explanation :

step 1: Initialize array
arr1 = [1, 2, 3, 4, 5];
step 2: Create another array arr2 with size of arr1
arr2 = [None] * len(arr1);
step 3: Copying all elements of one array into another
for i in range(0, len(arr1)):
arr2[i] = arr1[i];
step 4: Displaying elements of array arr1
print(“Elements of original array: “);
for i in range(0, len(arr1)):
print(arr1[i]),
print();
step 5:Displaying elements of array arr2
print(“Elements of new array: “);
for i in range(0, len(arr2)):
print(arr2[i]),

Output :

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/csstudy/source code file 2/Program to copy all elements of one array into another array in Python.py 
Elements of original array: 
1
2
3
4
5

Elements of new array: 
1
2
3
4
5
>>> 

Output Explanation :

First Elements of first array is printed.
Elements of original array: 1 2 3 4 5
After copying the new array is printed
Elements of new array: 1 2 3 4 5

Conclusion :

Program is running fine.

Thank you for reading, learning and download: Program to copy all elements of one array into another array in Python. This program contains for loop, Array initialisation and print function. This is for Beginners, class 11,12 and B Tech computer science students. Below are the link provided for more programs.
Python Programs Archives – CS Study
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study
Python Projects for Class 12 with Source Code (Mysql Connectivity) – CS Study

Leave a Comment

Your email address will not be published.