Course
Access Array Items
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Access Array Items
Since the array object behaves very much like a sequence, you can perform indexing and slicing operation with it.
Example
import array as arra = arr.array('i', [1, 2, 3])#indexingprint (a[1])#slicingprint (a[1:])
Changing Array Items
You can assign value to an item in the array just as you assign a value to item in a list.
Example
import array as arra = arr.array('i', [1, 2, 3])a[1] = 20print (a[1])
Here, you will get "20" as the output. However, Python doesn't allow assigning value of any other type than the typecode used at the time of creating an array. The following assignment raises TypeError.
import array as arra = arr.array('i', [1, 2, 3])# assignmenta[1] = 'A'
It will produce the following output
TypeError: 'str' object cannot be interpreted as an integer
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.