An array is a collection of elements of the same data type stored in adjacent memory locations. Arrays make it easy to calculate the positions of elements.
They are useful when we only have to work with a specific data type value. The user can treat the list as an array and note the data type in the array must be the same. Now we’re gonna introduce you to using Array in Python. Hope you understand.
animals = ["cat", "dog", "tiger"] print(animals)
[‘cat’, ‘dog’, ‘tiger’]
Accessing array elements
Now, we will access the first item:
animals = ["cat", "dog", "tiger"] x = animals print(x)
For example, we will edit the value of the first item:
animals = ["cat", "dog", "tiger"] animals = "chicken" print(animals)
[‘chicken’, ‘dog’, ‘tiger’]
The length of an array
Use len() function to print out the length of the array. For example:
animals = ["cat", "dog", "tiger"] x = len(animals) print(x)
Loop array elements
Using for in loop to iterate over each element of the array. For example, we will print out each item in the animals array:
animals = ["cat", "dog", "tiger"] for x in animals: print(x)
We just introduced you to how to use Array in Python.
Thank you for reading!
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.