LoginSignup
3
0

More than 5 years have passed since last update.

プログラミング初心者のPython アウトプット

Posted at

Python アウトプット用

最近勉強し始めたpythonの自分用のアウトプットを書き留めておきます。

リスト

リストは文字列や数字をまとめて変数に渡すことができます。

hello.py
drinks = ['beer', 'cocoa', 'coffee']
           #0        #1       #2

いろいろしてみる

リストをすべて取り出すときは

hello.py
drinks = ['beer', 'cocoa', 'coffee']
print(drinks) # =>['beer', 'cocoa', 'coffee']

リストを1つ取り出す

hello.py
drinks = ['beer', 'cocoa', 'coffee']
print(drinks[0]) # => 'beer'

リストを書き換える

hello.py
drinks = ['beer', 'cocoa', 'coffee']
drinks[0] = 'milk' # => 'beer' => 'milk'
print(drinks) # => ['milk' , 'cocoa', 'coffee']

末尾に付け加える

hello.py
drinks = ['beer', 'cocoa', 'coffee']
drinks.append('milk')
print(drinks) # => ['beer', 'cocoa', 'coffee', 'milk']

辞書

辞書はキーと値を関連づけることができます。またリストと違って順番の指定はできません。

hello1.py
colors = {'red':'赤', 'blue':'青', 'yellow':'黄'}
          #キー: 値

いろいろしてみる

全て取り出す

hello1.py
colors = {'red':'赤', 'blue':'青', 'yellow':'黄'}
print(colors) # => {'red':'赤', 'blue':'青', 'yellow':'黄'}

一つ値を取り出す

hello1.py
colors = {'red':'赤', 'blue':'青', 'yellow':'黄'}
print(colors['red']) # => 赤

値を変える

hello1.py
colors = {'red':'赤', 'blue':'青', 'yellow':'黄'}
colors['red'] = 'あか'
print(colors['red']) # => あか

キーを値を追加する

hello1.py
colors = {'red':'赤', 'blue':'青', 'yellow':'黄'}
colors['green'] = '緑'
print(colors) # => {'red': '赤', 'blue': '青', 'yellow': '黄', 'green': '緑'}

リストと辞書について初心者なりに書いてみました

どこか間違っているところなどがあればご指摘いただきたいです。なお初心者なので間違い等があるかもしれませんが、そこはご了承ください。これからも、いっぱいインプットとアウトプットしていきます。

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