Python program prints an array

王林
Release: 2023-09-12 13:29:02
forward
1373 people have browsed it

Python program prints an array

A collection of homogeneous elements in a single variable and contiguous memory locations is called an array. The elements in the array can be of any data type, but all elements present in the array should be homogeneous, that is, they should belong to the same data type.

An array is a special type of variable that actually stores multiple values ​​or elements under the name of a single variable, with contiguous memory locations, accurately called "indexes".

index

The word index represents the plural form of index. The term index indicates the position of an element in an array. If there are n elements in an array, the position number of the element in the array or the index of the element starts from 0 and goes up to n – 1. The first element of the array has index 0, and the last element of the array is n – 1.

step

  • Step 1 - First, declare an array containing the corresponding elements or values ​​so that the array size is not exceeded. Note that all elements considered in the array should be of the same data type.

  • Step 2 - In order to iterate over the entire array, starting from the first element with index "0" to the last element with index "n – 1", a loop must take The way the variable is incremented on each iteration means that the index of the elements starts from 0.

  • Step 3 - In the loop, considering the array with index " i ", print the elements of array[i] representing the element of the i-th index iteration. This operation must be performed within the loop itself so that the element is printed as soon as it is traversed.

Example

In the following example, we will understand the process of printing an array.

arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
size = len(arr)

print("The integral elements present in the array are: ")
for i in range(0, size):
   print(arr[i])
Copy after login

Output

The output of the above program is as follows -

The integral elements present in the array are: 
1
2
3
4
5
6
7
8
9
10
Copy after login

The above is the detailed content of Python program prints an array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template