LoginSignup

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 1 year has passed since last update.

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

Last updated at Posted at 2022-04-18

動画リンク

課題について

  • マニュアル通りにできること vs 動作を理解して命令をpcに伝えること
  • 今回の課題は,文字と数値,全角と半角,フォントなんかがキーワード
  • 途中までとは,日本語で引っかかるから
  • 最終課題は,全てを提出すべき

こんばんは。 ”Pythonを動かしたときにVS Codeに表示されたエラー”と書いてあるけどそのエラーが見つけられないんですが、どうしたらよろしいでしょうか。 そして、,py で書いているときは日本語でのコードも書いたけど、.txt に変えると日本語でのコードがなくなったのもどうしたらいいですか。

Mark Down

VSCodeでMarkDown言語を使ってメモを作りなさい.

pythonの基本要素(basic elements)

要素

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

実装

./python_codes/c4_basic_elements.py
# variable(変数)
a = 1
b = 2
print(a+b)

# for-loop
# ':' colon, 'tab' indent
for i in [1,2,3,4,5]:
    print(i)
print("こんにちは")

for i in range(5):
    print(i+1)
print("こんにちは")

for i in 'こんにちは':
    print(i)

# while-loop
total = 0
a = 1
while total <= 50:
    total = total + a
    a = a + 1
    print(a, total)

# if-else
for a in range(1,10+1):
    print(str(a)+"\t", end='')
    if a <= 5:
        print("小さいです")
    else:
        print("大きいです")

for a in range(1,10+1):
    print(str(a)+"\t", end='')
    if a % 2 == 0:
        print("", end='')
    if a % 3 == 0:
        print("×", end='')
    if (a % 3 == 0) and (a % 2 == 0):
        print("", end='')
    print("")

# while True if break
total = 0
a = 1
while True:
    total = total + a
    a = a + 1
    if total > 50:
        break
print(a, total)

# function, tashizan -> sum, 1 -> i, print debug
# 引数(argument)
def tashizan(a, b):
    total = 0
    for i in range(a, b+1):
        total = total + 1
        print(i, total)
    return total

c = tashizan(1,5)
print(c)

# local    scope
print("local scope")
a = 'abc'

def test():
    a ='def'
    print("inside:"+a)
    return
test()
print("outside:"+a)

# 置換 ctrl-f, ctrl-h, Enterでひとつずつ選択,置換
# global scope
print("global scope")
a = 'abc'

def test():
    global a
    a ='def'
    print("inside:"+a)
    return
test()
print("outside:"+a)

# import
import calendar
print(calendar.month(1961,3))

# テキスト通り'c'とすると,うえのほうで定義したcとぶつかる
import calendar as cal
print(cal.month(1961,3))

from calendar import month
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を全て入力して,一つの添付ファイル(c4_python_basic_elements.py)として提出してください.
    • コードの中にコメントを挿入して,見直したときに読みやすいように工夫しなさい.
  • 先週と同様に出力をファイル保存して添付しなさい.
  • mdで見やすく整形して提出した場合,ボーナス点をつけます.

  • source ~/Desktop/lecture_22s/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