An array is used to manage multiple data at once. Create an array like [Element 1, Element 2, ...]. Each value in the list is called an element. Arrays allow you to manage multiple strings and multiple numbers as one.
fruits = ['apple','banana','orange']
Since the array is also a single value, it can be assigned to a variable. At this time, remember that the variable name to which the list is assigned is often pluralized by convention.
print(fruits[0])
Output result
apple
The numbers "0, 1, 2, ..." are assigned to the elements of the list in order from the front. This is called the index number. Note that the index number starts at 0. Each element of the list can be obtained by using the list [index number].
print('My favorite fruit is' + fruits[2] + 'is')
Output result
My favorite fruit is orange
You can also add new elements to the list. You can add a new element to the end of an already defined list by doing "list.append (value)".
python
fruits.append('grape')
You can also update the elements of the list. You can update the element with the specified index number in the list by setting "List [index number] = Value".
python
fruits[0] = 'strawberry'
Recommended Posts