In Python, you can declare an array using a list, which is the most common way to handle arrays. Here’s how you can declare an array:
Example 1: Using a List
# Declare an array with elements
my_array = [1, 2, 3, 4, 5]
print(my_array)
Example 2: Using the array
module (for type-specific arrays)
If you need arrays with specific data types (like integers or floats), you can use the array
module, which creates arrays more like traditional arrays in other languages.
import array
# Declare an array of integers
my_array = array.array('i', [1, 2, 3, 4, 5])
print(my_array)
In this case, 'i'
denotes that the array is of type integer (int
). You can find other type codes in the documentation for the array
module.
Would you like help with any specific type of array or operation?