## DEV Community is a community of 695,260 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Array in C++ and Python - Data Structures and Algorithms

Deepak Raj
Skilled in Data Science, Machine Learning, Deep Learning, As Well As Web Development Knowledge. React ❤️ to encourage Author.

### Introduction to Array

Arrays are used to store multiple values in a single variable, rather than declaring separate variables for each value. It's like containers and used to store similar type of elements as in the data type must be the same for all elements. it's usually presented as a native data structure in many programming languages.

`array` and `list` are different data structures in a language like python.

• Code Optimization (less code)
• Random Access
• Easy to traverse data
• Easy to manipulate data
• Easy to sort data etc.

### Array in C++

Arrays are one of the most important and fundamental data structures available in C++. The elements field within square brackets [], representing the number of elements in the array.

``````#include <iostream>
using namespace std;

int foo [5] = {10, 8, 6, 4, 2};

void sum() {
// it print sum of all elements
int i, result = 0;

for(i=0;i<5;i++) {
result += foo[i];
}
cout << "Total sum of array : " << result << endl;
}

void updation() {
// updation of array
foo[1] = 20;
cout << "Array element after updation :";
cout << foo[0]<< " ";
cout << foo[1]<< " ";
cout << foo[2]<< " ";
cout << foo[3]<< " ";
cout << foo[4]<< " ";

}
int main() {
sum();
updation();
return 0;
};
``````
##### output
``````Total sum of array : 30
Array element after updation : 10 20 6 4 2
``````

### Array in Python

We can access each element of an array using the index of the element.

Tyepcode Value
b Represents signed integer of size 1 byte/td>
B Represents unsigned integer of size 1 byte
c Represents character of size 1 byte
i Represents signed integer of size 2 bytes
I Represents unsigned integer of size 2 bytes
f Represents floating point of size 4 bytes
d Represents floating point of size 8 bytes

#### Traverse Operation

Traverse operation refers to print all the array elements one by one.

``````from array import *

array1 = array('i', [10,20,30,40,50])
# i is the typecode which Represents signed integer of size 2

for x in array1:
print(x)
``````
##### Output
``````10
20
30
40
50
``````

#### Update Operation

Update operation refers to updating an existing element from the array at a given index.

``````from array import *

arr = array('i', [10,20,30,40,50])

arr[2] = 80

for x in arr:
print(x)
``````
##### output
``````10
20
80
40
50
``````

Python also has numpy array. It's used to work with arrays. The array object in NumPy is called ndarray . We can create a NumPy ndarray object by using the array() function.

`Note: Python array is faster than python List.`