Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

コンピュータ演習A 21 Day 4th(5/17): c4(Python basic elements)

Posted at

pythonの基本要素(basic elements)

要素

  • variable(変数)
  • for-loop
    • zero indexed array
  • while-loop
  • if-else
  • function(関数) (def)
    • scope
  • import module

実装

./python/c4_python_basic_elements.py
  1  # variable(変数)
  2  from calendar import month
  3  import calendar as cal
  4  import calendar
  5  a = 1
  6  b = 2
  7  print(a+b)
  8  
  9  # for-loop
 10  # ':' colon, 'tab' indent
 11  for i in [1, 2, 3, 4, 5]:
 12      print(i)
 13  print("こんにちは")
 14  
 15  # zero initialization
 16  for i in range(5):
 17      print(i+1)
 18  print("こんにちは")
 19  
 20  for i in 'こんにちは':
 21      print(i)
 22  
 23  # while-loop
 24  total = 0
 25  a = 1
 26  while total <= 50:
 27      total = total + a
 28      a = a + 1
 29      print(a, total)
 30  
 31  # if-else
 32  for a in range(1, 10+1):
 33      print(str(a)+"\t", end='')
 34      if a <= 5:
 35          print("小さいです")
 36      else:
 37          print("大きいです")
 38  
 39  for a in range(1, 10+1):
 40      print(str(a)+"\t", end='')
 41      if a % 2 == 0:
 42          print("", end='')
 43      if a % 3 == 0:
 44          print("×", end='')
 45      if (a % 3 == 0) and (a % 2 == 0):
 46          print("", end='')
 47      print("")
 48  
 49  # while True if break
 50  total = 0
 51  a = 1
 52  while True:
 53      total = total + a
 54      a = a + 1
 55      if total > 50:
 56          break
 57  print(a, total)
 58  
 59  # function(関数) def,
 60  # 引数(argument), return
 61  # tashizan -> sum, 1 -> i, print debug
 62  
 63  
 64  def tashizan(a, b):
 65      total = 0
 66      for i in range(a, b+1):
 67          total = total + 1
 68          print(i, total)
 69      return total
 70  
 71  
 72  c = tashizan(1, 5)
 73  print(c)
 74  
 75  # local scope
 76  print("local scope")
 77  a = 'abc'
 78  
 79  
 80  def test():
 81      a = 'def'
 82      print("inside:"+a)
 83      return
 84  
 85  
 86  test()
 87  print("outside:"+a)
 88  
 89  # 置換 ctrl-f, ctrl-h, Enterでひとつずつ選択,置換
 90  # global scope
 91  print("global scope")
 92  a = 'abc'
 93  
 94  
 95  def test():
 96      global a
 97      a = 'def'
 98      print("inside:"+a)
 99      return
100  
101  
102  test()
103  print("outside:"+a)
104  # import module
105  # codeの途中でimportしても,実行段階でheader部分に強制的に移動させられる.
106  print(calendar.month(1961, 3))
107  
108  # テキスト通り'c'とすると,うえのほうで定義したcとぶつかる
109  print(cal.month(1961, 3))
110  
111  print(month(1961, 3))

VS Codeのコツ

coding開始の初動作業

  • ctrl-n で新しいファイルを生成
  • ctrl-k m で言語を設定,pyとか打てばそのあとは補完してくれるので,途中でreturn
  • ctrl-s でファイルを保存
  • 右上の青矢印でfileをpythonで動かす
  • ctrl-shift-p で前のコマンドを呼び出されるので,以降はこれが便利

編集ショートカット

  • ctrl-x cut
  • ctrl-c copy
  • ctrl-v paste
  • 矢印keyで移動
  • fn - 左向き矢印key で行の先頭へ
  • fn - 右向き矢印key で行の末尾へ
  • fn - 上向き矢印key でpage up
  • fn - 下向き矢印key でpage down
  • ctrl-f で検索
  • ctrl-h で置換
  • ctrl-d で多重選択(multiple selection)で編集

LUNA提出課題

  • テキスト第4章のpythonの基本要素となるcodeを全て試して,LUNAに一つの添付ファイル(c4_python_basic_elements.py)として提出してください.コードの中にコメントアウトを挿入して,見直したときに読みやすいように工夫しなさい.

  • source ~/Desktop/lecture_21s/CompAInfo/c4_python_basic_elements.org
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?