2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Python3]リストの扱いの基本

Posted at

リストを作りたい

### まずは基本から

list001.py
list001 = [1, 2, 4, 8]
print(list001)

[1, 2, 4, 8]と表示されて、list001が作られる。

list002.py
list002 = ['a', 'b', 'c']
print(list002)

['a', 'b', 'c']と表示されて、list002が作られる。

#### 変数から作ることもできる

list003.py
x = 10
y = 20
z = 30
list003 = [x, y ,z]
print(list003)

[10, 20, 30]と表示されて、list003が作られる。
#### いろいろな型が混在しても大丈夫

list004.py
list004 = [1, 2, 'a', 'b', True, False]
print(list004)

[1, 2, 'a', 'b', True, False]と表示されて、list004が作られる。

#### 効率的な作り方

list005.py
list005 = [0]*10
print(list005)

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]と表示されて、list005が作られる。

list006.py
Numbers = [1, 2, 3, 4, 5]
list006 = Numbers*3
print(list006)

[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]と表示されて、list006が作られる。

list007.py
list007 = ['abc']*3
print(list007)

['abc', 'abc', 'abc']と表示されて、list007が作られる。

list008.py
list008 = list(range(-5, 6))
print(list008)

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]と表示されて、list008が作られる。

#### 偶数のリストが作りたい時

list009.py
list009 = list(range(0, 20, 2))
print(list009)

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]と表示されて、list009が作られる。

#### 奇数のリストが作りたい時

list010.py
list010 = list(range(1, 20, 2))
print(list010)

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]と表示されて、list010が作られる。

#### 3の倍数のリストが作りたい時

list011.py
list011 = list(range(0, 30, 3))
print(list011)

[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]と表示されて、list011が作られる。

#### 文字列をlist()に入れるとバラバラになってしまう。

list012.py
list012 = list('apple')
print(list012)

['a', 'p', 'p', 'l', 'e']と表示されて、list012が作られる。

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?