The methods for inputting data into a Python array are: direct assignment using append() method using insert() method using extend() method
How to enter data into an array in Python
Direct assignment
The most direct method is direct assignment. For example:
<code class="python">my_array = [1, 2, 3, 4, 5]</code>
Use append() method
append() method can add elements to the end of the array. For example:
<code class="python">my_array = [] my_array.append(1) my_array.append(2)</code>
Use insert() method
insert() method can insert elements at the specified position in the array. For example:
<code class="python">my_array = [1, 2, 3] my_array.insert(1, 4) # 在索引 1 处插入元素 4</code>
Use the extend() method
extend() method to add elements from another iterable object (such as a list or tuple) to an array middle. For example:
<code class="python">my_array = [1, 2, 3] my_other_array = [4, 5, 6] my_array.extend(my_other_array)</code>
Other methods
There are other methods to input data into the array, for example:
The above is the detailed content of How to input into an array in python. For more information, please follow other related articles on the PHP Chinese website!