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

新Python生活一日目(振返り)【PyQ/2,3,4章】

Last updated at Posted at 2017-11-11

表示

書式

print('文字列') #文字列を表示する

print('Hello ' + 'World') #Hello World

計算

■変数を使用した計算

stock = 12 #stockって名前の変数に12を代入
delivery = 20 #deliveryって変数に20を代入
new_stock = stock + delivery #32 #new_stockって変数はstock変数とdeliveryの合算
print('今の在庫は' + str(new_stock) + '') #今の在庫は32本

※strは文字列に変換する

if文

■比較演算

if stock < 10:
    print('在庫は10本より少ない') #条件①:stock変数が10以下の時にこの表示出す
elif stock == 10:
    print('在庫は10本') #条件②:stock変数が10の時にこの表示出す
else:
    print('在庫は10本より多い') #条件③:それ以外の時にこの表示出す

list、for文

■ループの利用
for 変数 in リスト
※リストの要素を1つずつ変数に代入しながら、forの下に実行する

week = ['', '', '', '', ''] #リスト名week
for day in week:
    print(day)
出力結果
月
火
水
木
金

■リストへ値の追加

変数.append('追加要素') #末尾に追加
変数.insert(位置.追加要素) #位置は0始まり

■リスト値の削除

変数.pop() #末尾を削除
変数.pop(n) #末尾からn番目を削除。始まりは0

■リストの要素の変更

リスト名[0] = 120 #1番目の要素を120に変更
リスト名[-1] = 50 #末尾の要素を50に変更
リスト名[-2] = 5 #後ろから2番目の要素を5に変更
2
1
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
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?