Python array basics

Python array basics

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.

Assign a variable to a list that has multiple strings as elements

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.

Output the element with index number 0

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].

Please output by concatenating the element with index number 2 with the character string.

print('My favorite fruit is' + fruits[2] + 'is')

Output result


My favorite fruit is orange

Add the string "grape" to the end of the list

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')

Update the element with index number 0 to the string "strawberry"

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

Python array basics
Python basics ⑤
Python basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
Python basics memorandum
Python multidimensional array
[Beginner] Python array
Python CGI basics
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Python basics ② for statement
Python: Unsupervised Learning: Basics
Python basics 8 numpy test
Errbot: Python chatbot basics
python numpy array calculation
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
Basics of python: Output
[python] Array slice operation
Python memo (for myself): Array
Quicksort an array in Python 3
Empty multidimensional array in python
Python
Stumble story with Python array
Create a python numpy array
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Python 2D array trap [Copy array]
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
SNS Python basics made with Flask
Subscript access to python numpy array
Getting Started with Python Basics of Python
Python application: Numpy Part 3: Double array
[Python] How to swap array values
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
Compare Python and JavaScript array loops
About the basics list of Python basics
(python) Deep Learning Library Chainer Basics Basics