In this tutorial, we will be discussing about python arrays. But Before getting started with python arrays, it is very important to note that python does not contain default input-data setting for its arrays but uses its python list which is found in the Numpy data import library
Generally, Python arrays are unique variables that can contain multiple values at the same time. Arrays are the basic foundation in all data-science in python.
To get the full package of python array, you need to install NumPy from the import library and then use the array () function to create an array and use it as an input list.
A python array begins with an opening square and end with a closing square bracket. Objects inside the square brackets are separated by commas.
An array element can be access by referring to the index number
Example
fruits= ["apple", "watermelon", "cherry"]
x = fruits[0]print(x)
OUTPUT
Finding the length of an array
We Use the len() to know the exact length of an array
colors = ["green", "violet", "red"]
x = len(colors)print(x)
In this case, the len() function instructs the compiler to count the total number of objects within the square bracket
OUTPUT
Looping array elements
Use the for in to through all the arrays elements
colors = ["green", "brown", "purple"]
for x in colors:print(x)
As we earlier saw in array addition, the function for in in looping of arrays simply instructs the compiler to list the objects within the square bracket.
OUTPUT
How to add array elements
Addition is done by using the append method
fruits = ["apple", "cherry", "watermelon"]
fruits.append("Honda")print(fruits)
The compiler follows the instruction of the append function. Its adds what is in between the brackets of the append fuction.
OUTPUT
How to remove arrays
colors = ["red", "blur", "violet"]
colors.remove("violet")print(colors)
The compiler follows the instruction of the remove function that is it subtracts what is in the bracket of the remove function
OUTPUT
In python, there are many default input list which can be used on arrays. These standards types are found in the NumPy package as python itself does support data input list.
Comments
Post a Comment
Please do not enter any spam link in the comment box.