Home » Python

Accessing elements from the array in Python

Here, we are going to learn how to access elements from the array in Python programming language?
Submitted by IncludeHelp, on March 04, 2020

Prerequisite:

Here are the examples to demonstrate how to access elements from the array

Example 1:

# Accessing elements from the array

# importing array module 
import array as arr 

# int array
arr1 = arr.array('i', [10, 20, 30, 40, 50, 60]) 

# accessing element of array 
print("arr1[0]: ", arr1[0]) 
print("arr1[3]: ", arr1[3]) 

# float array
arr2 = arr.array('d', [22.59, 33.32, 43.32]) 

# accessing elements of array 
print("arr2[1]: ", arr2[1]) 
print("arr2[2]: ", arr2[2]) 

Output

arr1[0]:  10
arr1[3]:  40
arr2[1]:  33.32
arr2[2]:  43.32

Example 2:

# importing array module
import array as arr

# declaring int array
arr = arr.array('i', [20, 40, 60, 80])

print("First element  arr[0] :", arr[0])
print("Second element arr[1] :", arr[1])
print("Third element  arr[2] :", arr[2])
print("Fourth element arr[1] :", arr[3])
print()

print("Last element arr[-1]:", arr[-1])
print("Second last element arr[-2]:", arr[-2])

Output

First element  arr[0] : 20
Second element arr[1] : 40
Third element  arr[2] : 60
Fourth element arr[1] : 80
Last element arr[-1]: 80
Second last element arr[-2]: 60





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.