In this tutorial, we will learn writing the python program to create an array (list in case of python) and print the length (number of elements stored) in the array (list).
Problem Statement
For the given array, our program should return the length of the array as an output.
For example:
Case 1: if the given the array (list) is [1, 2, 3, 4].
The output should be 4.
Case 2: if the given array (list) is [9, 8, 7, 6, 5].
The output should be 5.
Python code to print length of an array(list)
#given array (list)
arr = [1,2,3,4,5]
# print the return value of len(arr) i.e. the length of the array (list)
print("Length of the array:",len(arr))
Output :
Length of the array: 5
Explanation:
For the given array (list) [1, 2, 3, 4, 5], the len() function will calculates the length of the array (list) passed as an argument in len() function, the len() function will return the length of array (list) in ‘int’ data type.
In this example, the given array (list) is passed as the parameter of the len() function, so the len() function returned the size of the array (list).