In this simple python program, we need to display elements of a python array. It's an array python program.
To understand this example, you should have knowledge of the following Python programming topics:
An array is a set of elements of the same data type accessed using a common variable index. In python language, we use the List instead of Array but with all the array characteristics. In this simple python program, we need to print all the elements of an array.
For example, suppose we have an array named A, and it has some integers like 1, 2, 3, 4, 5. We have to access these elements using A[i] where i is from 0 to 4. a[0] is 1, a[1] = 2 like that. Here, we use a for loop to traverse through the python array from zero to four and print the numbers inside each array location using a[i].
STEP 1: We have to initialize an array A. We can use an input method also if the user wants to insert the array elements in the Python programming language.
STEP 2: Using a print statement, print 'the array elements are '.
STEP 3: Use a for loop
from 0 to the length of an array using the range()
method, where the length of the array is calculated using the len()
method in python.
STEP 4: Print each element in the array in each iteration of the for loop using index A[i]
A = [1, 2, 3, 4, 5]; # initialize the array A
print("Array elements are: ");
for i in range(0, len(A)): # for loop to traverse through each element in array
print(A[i]),
Array elements are: 1 2 3 4 5