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?

Python入門 - 学習記録

Last updated at Posted at 2025-04-16

Python学習記録

2025.04.16

やったこと

  • pythonの基本的な書き方を学ぶ
  • リストについて
  • 辞書について
  • 制御構文ついて

学んだこと

print(100 + 2000 + \
      1000)
>>> 3100
  • \の前後は改行していても1行だと見なされる
  • 変数とは値を入れる箱のようなもの
x = 'バナナ'
print(x)
>>> バナナ
  • 変数は上書きされていく
x = 'バナナ'
x = 'りんご'
print(x)
>>> りんご
  • Pythonは変数の型が自動で決まる
  • リストとは変数をまとめたもの
  • リストの中の1つ1つの値を要素という
  • 0から順番、インデックスと呼ぶ
  • リストは[]で表す
weather = ['sunny', 'cloudy', 'rainy']
  • リストの中は同じ型でなくても良い
  • リストに何も入れないと空のリストが作れる
  • リストの要素の数はlenを使う
weather = ['sunny', 'cloudy', 'rainy']
ptint(len(weather))
>>> 3
  • リストの中の要素を取り出したいときはインデックスを指定する
weather = ['sunny', 'cloudy', 'rainy']
ptint(weather(1))
>>> cloudy
  • リストの後ろに要素を追加したい場合はappendを使う
weather = ['sunny', 'cloudy', 'rainy']
weather.append('snow')
ptint(weather)
>>> ['sunny', 'cloudy', 'rainy', 'snow']
  • 要素の削除にはremoveを使う
  • リストの結合にはextendを使う
x = ['a', 'b', 'c']
y = ['d', 'e', 'f']
x.extend(y)
print(x)
  • または+でも結合可能 *新たにリストが作成される
  • リストを分割することも可能
x = ['a', 'b', 'c', 'd', 'e']
print(x[1:4])
>>> ['b', 'c', 'd']
  • 辞書型の変数はキーと値がペアになってる
  • 辞書を使用するときは{}を使用する
x = {'a': 0, 'b':1, 'c':2}
sec = x['b']
print(sec)
>>> 1
  • 追加も可能
x['d'] = 3
  • 変更も可能
x['a'] = 100
  • 二つの辞書を結合するときはupdateを使う
x.update(y)
  • バーティカルバー(|)を使うことも可能 *Python3.9以降
z = x | y
  • リストと同じく要素数をlenで取得できる
  • if, elseを使うと条件分岐可能
x = 16
if x >= 10:
    print('10以上')
else:
    print('10未満')
  • elifでさらに条件分岐が可能
  • forで繰り返しが可能
x_list = [100, 190, 2980]
for x in x_list:
    x_yen = str(x) + '円'
    print(x_yen)
  • 辞書の場合はinの後に辞書items()とする
x_dict = {'apple': 100, 'banana': 350}
for key, value in x_dict.items():
    text = key + 'は' + str(value) + '円です'
    print(text)
  • range()は連番の整数を作れる *繰り返す回数が決まっている時に使う
for x in range(10):
    print(x)
1
1
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
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?