1
1

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 3 years have passed since last update.

【Python】listの使い方1

Last updated at Posted at 2020-05-11

はじめに

とみーさんのブログのPython入門者のための学習ロードマップに沿ってPython学習を進めています。
今回は【Python入門】list(リスト)の使い方の総まとめ(前編)の前半を扱います。

対象者

  • とみーさんブログで学習中の方
  • listの使い方の概要を知りたい方

環境

Google Colaboratory

学習内容

  1. リストの作り方と参照方法
    リストはシーケンス型でミュータブル

  2. タプル、rangeはイミュータブル。タプル変数をlist()で囲って、新しい変数にできる

  3. listにlistも入れられる

1

list_num = [1,2,3,4]  # 1,2,3,4でlist作って
print(list_num)       # list_num表示で
実行結果
[1, 2, 3, 4]

2

シーケンス型にはリスト以外にタプル、rangeなどがあるが、これらはイミュータブルなのでデータ追加できない。(宣言時に代入した値を変更できない)タプル変数をlist()で囲って、新しい変数にする
タプルのリスト型への変換

tuple_samp = (1,2,3,4)
print(tuple_samp)
print(type(tuple_samp))  # 型の確認

list_samp = list(tuple_samp)
print(list_samp)
print(type(list_samp))
実行結果
(1, 2, 3, 4)
<class 'tuple'>
[1, 2, 3, 4]
<class 'list'>

rangeも同様

range_samp = range(4)
print(range_samp)
print(type(range_samp))
list_samp = list(range_samp)
print(list_samp)
print(type(list_samp))
実行結果
range(0, 4)
<class 'range'>
[1, 2, 3, 4]
<class 'list'>

3

list_0 = [1,2,3]
list_1 = [4,5,6]
list_2 = [7,8,9]
list_two_dim = [list_0,list_1,list_2]  # リストをリストにいれる
print(list_two_dim)
実行結果
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

参考

【Python入門】list(リスト)の使い方の総まとめ(前編)

1
1
1

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?