深度阅读

How to pass an array to a function in Python

作者
作者
2023年08月22日
更新时间
6.87 分钟
阅读时间
0
阅读量

In Python, you can pass an array (or any iterable) to a function as an argument. Here is an example of how this can be done:

def my_function(arr):
    for i in arr:
        print(i)

my_array = [1, 2, 3, 4, 5]
my_function(my_array)

In this example, we define a function called my_function that accepts an array as an argument and prints each element in the array. We then create an array called my_array and pass it to the my_function as an argument.

Note that Python lists are commonly used as arrays in Python, as they are dynamic and can change size. However, you can also use the array module to create arrays of specific type codes (e.g. 'i' for integers).

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。