Python arrays are collections of data stored in a specific order. They allow you to store multiple values in a single variable. Think of them as a row of boxes where you can put different items. Each item has its position in the row, making it easy to access and manipulate them individually.
Python arrays are versatile and can hold various types of data, such as numbers, strings, or even other arrays. They provide a convenient way to work with and organize data in your programs.
Create an array containing animal names:
animal = ["cat", "dog", "fox"]
print(animal)
PythonOutput [‘cat’, ‘dog’, ‘fox’] |
Accessing the Elements of an Array
Example
animal = ["cat", "dog", "fox"]
x = animal[0]
print(x)
PythonOutput cat |
This code defines a list named animal
containing three strings: “cat”, “dog”, and “fox”. Then, it retrieves the first element of the list (index 0), which is “cat”, and assigns it to the variable x
. Finally, it prints the value of x
, which is “cat”.
Change Element of Python Arrays
We can modify any element of the array by using indexing.
animal = ["cat", "dog", "fox"]
animal[0] = "Horse"
print(animal)
PythonOutput [‘Horse’, ‘dog’, ‘fox’] |
This code initializes a list named animal
containing three strings: “cat”, “dog”, and “fox”. Then, it modifies the first element of the list (index 0) to be “Horse”. Finally, it prints the modified list, resulting in ['Horse', 'dog', 'fox']
.
Length of Array
We can use the len () method to return the total length of an array.
Example
animal = ["cat", "dog", "fox"]
x = len(animal)
print(x)
PythonOutput 3 |
This code initializes a list named animal
containing three strings: “cat”, “dog”, and “fox”. Then, it calculates the length of the list using the len()
function and assigns it to the variable x
. Finally, it prints the value of x
, which is 3
in this case, as there are three elements in the list.
Print Array using Loop
Example
animal = ["cat", "dog", "fox"]
for x in animal:
print(x)
PythonOutput cat dog fox |
This code initializes a list named animal
containing three strings: “cat”, “dog”, and “fox”. Then, it iterates over each element in the list using a for
loop and prints each element individually. So, when you run this code, it will print each animal’s name on a new line.
Adding new Element in Array
We can add new elements by using the append () method.
animal = ["cat", "dog", "fox"]
animal.append("Horse")
print(animal)
PythonOutput [‘cat’, ‘dog’, ‘fox’, ‘Horse’] |
This code initializes a list named animal
containing three strings: “cat”, “dog”, and “fox”. Then, it uses the append()
method to add the string “Horse” to the end of the list. Finally, it prints the modified list, which now includes “Horse”.
Remove an Element from Array
We can remove elements from our array by using the pop () method.
animal = ["cat", "dog", "fox"]
animal.pop(2)
print(animal)
PythonOutput [‘cat’, ‘dog’] |
This code initializes a list named animal
containing three strings: “cat”, “dog”, and “fox”. Then, it uses the pop()
method to remove the element at index 2 (which is “fox”) from the list. Finally, it prints the modified list, which now contains only “cat” and “dog”.
We can also use the remove () method similar to the pop () method.
Example
animal = ["cat", "dog", "fox"]
animal.remove("fox")
print(animal)
PythonOutput [‘cat’, ‘dog’] |
This code initializes a list named animal
containing three strings: “cat”, “dog”, and “fox”. Then, it uses the remove()
method to remove the element “fox” from the list. Finally, it prints the modified list, which now contains only “cat” and “dog”.
Author: TCF Editorial
Copyright The Cloudflare.
You can also read: