** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
>>> l = [1, 20, 4, 50, 2, 17, 80]
>>> type(l)
<class 'list'>
In this way, you can use lists.
>>> l
[1, 20, 4, 50, 2, 17, 80]
>>> l[0]
1
>>> l[1]
20
>>> l[-1]
80
>>> l[0:2]
[1, 20]
>>> l[:2]
[1, 20]
>>> l[2:]
[4, 50, 2, 17, 80]
>>> l[:]
[1, 20, 4, 50, 2, 17, 80]
You can also use indexes and slices for lists.
len ()
for lists>>> len(l)
7
Using len ()
on a list returns the number of data contained in the list.
>>> letters = list('abcdefg')
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
It seems that it is not very useful, but ...
>>> n = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> n[::2]
[1, 3, 5, 7, 9]
By setting [:: 2]
, it can be extracted by skipping one.
>>> n[::-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
By setting [:: -1]
, the order can be reversed and output.
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[1]
[1, 2, 3]
In this state, the included list is indexed.
>>> x[0][1]
'b'
You can specify [1]
in [0]
by setting [0] [1]
.
Recommended Posts