📜  Python Arrays(1)

📅  最后修改于: 2023-12-03 14:45:56.060000             🧑  作者: Mango

Python Arrays

Arrays are an essential data structure in programming. They allow you to store multiple values in a single variable. In Python, arrays are represented using the built-in array module or the list data type.

1. Introduction to Arrays

Arrays consist of elements, which can be of any data type - integers, floats, strings, or even other arrays. These elements are stored in contiguous memory locations, making it easy to access them using their index positions.

1.1 Creating Arrays

In Python, you can create an array using the array module or the simpler list data type. Let's see some examples:

# Using the array module
import array

int_array = array.array('i', [1, 2, 3, 4])  # Creating an array of integers
float_array = array.array('f', [1.5, 2.5, 3.5])  # Creating an array of floats

# Using the list data type
str_list = ['apple', 'banana', 'cherry']  # Creating a list of strings
mixed_list = [1, 2.5, 'hello']  # Creating a list of mixed data types
1.2 Accessing Array Elements

Arrays have zero-based indexing, meaning the first element is accessed using index 0, the second element with index 1, and so on. You can access individual elements using square brackets [].

fruits = ['apple', 'banana', 'cherry']

print(fruits[0])  # Output: 'apple'
print(fruits[1])  # Output: 'banana'
print(fruits[2])  # Output: 'cherry'
1.3 Modifying Array Elements

You can modify individual elements by assigning new values to them using their respective indices.

fruits = ['apple', 'banana', 'cherry']
fruits[1] = 'grape'

print(fruits)  # Output: ['apple', 'grape', 'cherry']
2. Common Array Operations

Python provides many operations and functions to manipulate arrays efficiently.

2.1 Array Length

To get the length of an array, you can use the built-in len() function.

fruits = ['apple', 'banana', 'cherry']

print(len(fruits))  # Output: 3
2.2 Adding Elements to an Array

You can append new elements to the end of an array using the append() method or the + operator for lists.

fruits = ['apple', 'banana', 'cherry']

fruits.append('orange')
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'orange']

fruits = fruits + ['grape', 'watermelon']
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'orange', 'grape', 'watermelon']
2.3 Removing Elements from an Array

You can remove elements from an array using the remove() method or the del statement.

fruits = ['apple', 'banana', 'cherry']

fruits.remove('banana')
print(fruits)  # Output: ['apple', 'cherry']

del fruits[0]
print(fruits)  # Output: ['cherry']
2.4 Searching for an Element

To check if an element exists in an array, you can use the in keyword.

fruits = ['apple', 'banana', 'cherry']

print('banana' in fruits)  # Output: True
print('orange' in fruits)  # Output: False
Conclusion

Arrays are a fundamental data structure in Python. They allow you to store and manipulate multiple values efficiently. With the array module or the list data type, you can perform various operations such as creating arrays, accessing elements, modifying values, and more. Explore the vast array of possibilities with Python arrays!

Note: Markdown code block is represented using triple backticks, i.e., ```.