LoginSignup
0
0

More than 3 years have passed since last update.

Python基礎 〜データ型〜

Last updated at Posted at 2019-07-27

はじめに

Pythonのデータ型についてまとめ。
出力のサンプルはPythonのインタラクティブシェルの実行結果です。

文字列型

”(ダブルコーテーション)または ’(シングルコーテーション)で括った文字列

>>> string="abc"
>>> print(string)
abc

>>> string="あいう"
>>> print(string)
あいう

>>> string="123"
>>> print(string)
123

数値型

  • 整数
>>> integer=123
>>> print(integer)
123
  • 浮動小数点
>>> decimal=0.5
>>> print(decimal)
0.5

>>> decimal=1.0+1.0
>>> print(decimal)
2.0
  • 複素数

虚数と複素数についてはコチラのWebサイトの解説がわかりやすいです。

>>> complex=1j+1j
>>> print(complex)
2j

>>> complex=2+1j
>>> print(complex)
(2+1j)

>>> complex=(1J+2)+(5+4J)
>>> print(complex)
(7+5j)

論理型(bool型)

TrueとFalseは必ず頭文字が大文字でなければならない。

>>> type_bool=True
>>> type_bool
True

>>> type_bool=False
>>> type_bool
False

>>> type_bool=true
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined

>>> 50<51
True

>>> 50<50
False

>>> 50<=50
True

配列(リスト)型

リストの値を追加したり削除する事が可能。
sortメソッドを使用すると並び替えることも可能だが数値型と文字列型が混在するリストではエラーになる。

>>> type_list=["あいう","abc","123",123]
>>> type_list
['あいう', 'abc', '123', 123]

>>> type_list.append("onetwothree")
>>> type_list
['あいう', 'abc', '123', 123, 'onetwothree']

>>> type_list.remove("onetwothree")
>>> type_list
['あいう', 'abc', '123', 123]

>>> type_list=[5,6,9,2,5,0]
>>> type_list.sort()
>>> type_list
[0, 2, 5, 5, 6, 9]

>>> type_list=["あ","ら","が","き","ゆ","い"]
>>> type_list.sort()
>>> type_list
['あ', 'い', 'が', 'き', 'ゆ', 'ら']

>>> type_list=["あ","ら","が","き","ゆ","い","1","番"]
>>> type_list.sort()
>>> type_list
['1', 'あ', 'い', 'が', 'き', 'ゆ', 'ら', '番']

>>> type_list=["あ","ら","が","き","ゆ","い",1,"番"]
>>> type_list.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()

>>> list=["あ","ら","が","き","ゆ","い",1,"番"]
>>> list[0]
'あ'
>>> list[6]
1
>>> list[6]=2
>>> list
['あ', 'ら', 'が', 'き', 'ゆ', 'い', 2, '番']

辞書型

>>> plan={"mon":"music","tue":"run","wed":"swim"}
>>> plan["mon"]
'music'
>>> plan["wed"]
'swim'
>>> plan.keys()
dict_keys(['wed', 'tue', 'mon'])
>>> plan.values()
dict_values(['swim', 'run', 'music'])

タプル型

読み込み専用のデータ型で定義した後に値を書き換えることが出来ない。
辞書型のキーとして使用することが出来る。

>>> type_tuple=('あいう', 'abc', '123', 123)
>>> type_tuple
('あいう', 'abc', '123', 123)

>>> type_tuple[0]
'あいう'

>>> type_tuple[0]='かきく'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

>>> walk={}
>>> type_tuple=("太郎","8月1日")
>>> walk[type_tuple]='60min'
>>> walk
{('太郎', '8月1日'): '60min'}
>>> walk.keys()
dict_keys([('太郎', '8月1日')])
>>> walk.values()
dict_values(['60min'])

集合型

>>> color={"red","blue","yellow","pink"}
>>> color
{'pink', 'yellow', 'blue', 'red'}

>>> band={"mrchildren"}
>>> band=set("mrchildren")
>>> band
{'h', 'i', 'd', 'm', 'r', 'n', 'l', 'e', 'c'}

>>> color=["red","blue","yellow","pink"]
>>> shirt=set(color)
>>> shirt
{'pink', 'blue', 'yellow', 'red'}
>>> shirt.update(["green"])
>>> shirt
{'pink', 'blue', 'yellow', 'green', 'red'}

#配列でupdateしないと順番を保持してくれない。

>>> color=["red","blue","yellow","pink"]
>>> shirt=set(color)
>>> shirt
{'pink', 'blue', 'yellow', 'red'}
>>> shirt.update("green")
>>> shirt
{'g', 'r', 'blue', 'yellow', 'pink', 'n', 'e', 'red'}

#重複の削除
>>> color=["red","blue","yellow","pink","red","yellow","pink"]
>>> set(color)
{'yellow', 'blue', 'pink', 'red'}


#差分を求める
>>> color1={"red","blue"}
>>> color2={"pink","blue"}
>>> color1-color2
{'red'}
>>> color2-color1
{'pink'}
0
0
2

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