1: python言語入門nakamich/cs/2020/cs-q3-w1.pdf1: python言語入門...

18
仲道 嘉夫 September 2020 コンピュター・サイエンス第一 Introduction to Computer Science 1 : Python 言語入門

Upload: others

Post on 08-Feb-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

  • 仲道 嘉夫

    September 2020

    コンピュター・サイエンス第一Introduction to Computer Science

    1: Python言語入門

  • コンピューターって何?

    2

    コンピューターは

    機械

    情報 情報

    94.3 53.9 76.5 87.8 92.3 30.9 65.3 65.5 65.3 12.465.9 80.1 76.0 80.3 18.7 77.5 93.2 50.5 70.2 20.664.9 58.2 14.6 23.7 35.4 57.1 31.2 81.3 14.4 60.580.5 11.0 54.2 77.3 74.0 34.5 39.8 98.3 43.6 86.389.3 29.9 16.8 97.3 31.5 22.3 36.0 67.5 55.7 41.282.1 75.5 73.4 30.6 68.2 17.9 91.4 81.2 98.9 55.585.1 47.7 39.5 80.8 65.5 67.3 43.9 19.2 27.6 40.639.2 27.2 26.2 44.2 75.7 69.5 12.1 68.2 47.9 46.251.8 35.8 94.1 28.8 10.4 57.6 58.7 83.2 41.6 65.411.2 32.9 61.4 19.0 87.2 57.5 55.6 46.0 29.0 78.933.8 23.4 20.7 38.5 65.9 81.7 29.9 51.0 58.2 17.445.9 63.1 86.6 55.4 65.1 41.3 58.7 79.5 94.4 53.543.1 43.5 57.9 19.5 89.4 66.3 60.5 72.8 86.2 21.780.2 46.4 94.4 51.1 68.3 27.6 51.4 20.7 50.8 95.551.7 66.4 59.7 24.2 47.1 99.9 47.2 45.5 40.7 54.090.5 92.8 45.3 70.8 12.3 54.1 97.4 43.3 23.2 74.437.5 20.7 75.0 39.3 61.9 31.0 55.0 22.9 61.5 37.427.3 42.0 83.8 67.9 18.7 33.6 85.0 32.9 25.0 33.759.1 89.9 37.2 17.2 43.4 12.9 19.6 45.4 17.6 92.089.2 97.5 72.6 63.2 38.5 63.8 61.5 32.0 62.2 38.478.2 62.5 42.2 89.4 92.1 31.7 87.6 22.9 95.7 62.911.0 21.9 60.3 79.3 34.3 28.7 69.3 74.8 44.1 60.819.0 94.8 24.6 27.5 40.7 35.6 24.0 35.1 71.2 65.345.6 25.4 41.0 91.3 59.7 46.2 71.2 22.8 84.6 62.545.8 64.4 82.5 93.5 71.8 41.7 48.5 30.0 52.6 47.777.3 32.6 77.7 74.8 20.6 66.6 21.4 45.9 47.6 80.818.5 60.1 54.3 44.0 66.0 41.8 37.6 88.3 92.5 41.793.4 50.8 25.9 34.0 44.4 30.0 80.1 57.9 67.0 80.647.6 16.4 75.3 94.8 98.4 61.7 77.3 61.5 77.2 47.230.8 93.9 80.9 80.7 60.7 71.8 63.1 17.9 59.2 12.889.1 21.2 67.5 85.0 73.6 16.6 23.2 99.7 44.2 16.663.6 63.4 62.1 53.2 27.6 68.3 91.7 96.7 19.2 74.667.3 33.3 67.4 16.5 63.7 19.0 73.3 33.7 56.1 30.981.0 88.6 78.3 76.1 88.5 30.3 80.1 87.3 58.1 85.089.5 29.0 90.4 43.4 70.8 24.0 23.6 26.0 94.6 62.498.5 86.0 19.3 42.6 69.1 93.0 60.8 67.8 40.1 96.782.1 35.6 59.7 44.3 59.3 59.5 66.9 40.4 97.6 73.482.4 97.6 27.0 27.2 93.7 16.3 81.1 89.9 92.2 95.756.3 11.9 73.0 40.1 22.5 14.0 94.7 56.2 13.7 40.736.0 17.6 73.8 54.4 54.7 12.2 79.3 90.6 96.8 96.610.6 50.5 72.2 80.6 42.3 31.1 34.2 79.8 24.0 60.738.3 85.5 95.9 52.7 40.7 12.0 49.4 81.0 58.9 81.112.4 11.4 59.2 14.9 40.8 75.8 80.4 58.8 64.6 31.372.9 48.0 57.4 40.6 45.1 20.9 81.1 47.9 91.0 84.355.8 74.9 42.0 53.9 97.7 10.1 18.5 89.1 18.3 88.833.4 41.6 35.5 70.4 23.0 63.6 53.6 42.7 23.9 54.289.5 22.5 51.5 20.1 86.4 34.2 46.3 16.4 44.1 10.792.2 66.6 57.1 40.9 85.7 73.5 36.0 84.8 18.5 12.254.0 87.6 17.9 85.2 71.8 51.3 76.0 85.1 77.0 62.532.0 14.5 50.6 59.7 53.4 65.0 21.9 22.7 75.8 64.827.3 69.4 16.5 72.4 80.9 44.9 53.3 40.9 88.8 31.538.0 61.6 90.1 50.9 35.0 51.7 41.2 83.4 37.4 88.456.4 78.8 64.0 65.0 49.4 89.1 72.5 89.8 54.3 86.291.5 67.4 23.1 21.6 82.2 42.8 88.4 60.1 97.0 27.741.6 49.5 97.3 83.2 29.2 29.3 53.4 28.2 96.4 35.6In Out

  • 94.3 53.9 76.5 87.8 92.3 30.9 65.3 65.5 65.3 12.465.9 80.1 76.0 80.3 18.7 77.5 93.2 50.5 70.2 20.664.9 58.2 14.6 23.7 35.4 57.1 31.2 81.3 14.4 60.580.5 11.0 54.2 77.3 74.0 34.5 39.8 98.3 43.6 86.389.3 29.9 16.8 97.3 31.5 22.3 36.0 67.5 55.7 41.282.1 75.5 73.4 30.6 68.2 17.9 91.4 81.2 98.9 55.585.1 47.7 39.5 80.8 65.5 67.3 43.9 19.2 27.6 40.639.2 27.2 26.2 44.2 75.7 69.5 12.1 68.2 47.9 46.251.8 35.8 94.1 28.8 10.4 57.6 58.7 83.2 41.6 65.411.2 32.9 61.4 19.0 87.2 57.5 55.6 46.0 29.0 78.933.8 23.4 20.7 38.5 65.9 81.7 29.9 51.0 58.2 17.445.9 63.1 86.6 55.4 65.1 41.3 58.7 79.5 94.4 53.543.1 43.5 57.9 19.5 89.4 66.3 60.5 72.8 86.2 21.780.2 46.4 94.4 51.1 68.3 27.6 51.4 20.7 50.8 95.551.7 66.4 59.7 24.2 47.1 99.9 47.2 45.5 40.7 54.090.5 92.8 45.3 70.8 12.3 54.1 97.4 43.3 23.2 74.437.5 20.7 75.0 39.3 61.9 31.0 55.0 22.9 61.5 37.427.3 42.0 83.8 67.9 18.7 33.6 85.0 32.9 25.0 33.759.1 89.9 37.2 17.2 43.4 12.9 19.6 45.4 17.6 92.089.2 97.5 72.6 63.2 38.5 63.8 61.5 32.0 62.2 38.478.2 62.5 42.2 89.4 92.1 31.7 87.6 22.9 95.7 62.911.0 21.9 60.3 79.3 34.3 28.7 69.3 74.8 44.1 60.819.0 94.8 24.6 27.5 40.7 35.6 24.0 35.1 71.2 65.345.6 25.4 41.0 91.3 59.7 46.2 71.2 22.8 84.6 62.545.8 64.4 82.5 93.5 71.8 41.7 48.5 30.0 52.6 47.777.3 32.6 77.7 74.8 20.6 66.6 21.4 45.9 47.6 80.818.5 60.1 54.3 44.0 66.0 41.8 37.6 88.3 92.5 41.793.4 50.8 25.9 34.0 44.4 30.0 80.1 57.9 67.0 80.647.6 16.4 75.3 94.8 98.4 61.7 77.3 61.5 77.2 47.230.8 93.9 80.9 80.7 60.7 71.8 63.1 17.9 59.2 12.889.1 21.2 67.5 85.0 73.6 16.6 23.2 99.7 44.2 16.663.6 63.4 62.1 53.2 27.6 68.3 91.7 96.7 19.2 74.667.3 33.3 67.4 16.5 63.7 19.0 73.3 33.7 56.1 30.981.0 88.6 78.3 76.1 88.5 30.3 80.1 87.3 58.1 85.089.5 29.0 90.4 43.4 70.8 24.0 23.6 26.0 94.6 62.498.5 86.0 19.3 42.6 69.1 93.0 60.8 67.8 40.1 96.782.1 35.6 59.7 44.3 59.3 59.5 66.9 40.4 97.6 73.482.4 97.6 27.0 27.2 93.7 16.3 81.1 89.9 92.2 95.756.3 11.9 73.0 40.1 22.5 14.0 94.7 56.2 13.7 40.736.0 17.6 73.8 54.4 54.7 12.2 79.3 90.6 96.8 96.610.6 50.5 72.2 80.6 42.3 31.1 34.2 79.8 24.0 60.738.3 85.5 95.9 52.7 40.7 12.0 49.4 81.0 58.9 81.112.4 11.4 59.2 14.9 40.8 75.8 80.4 58.8 64.6 31.372.9 48.0 57.4 40.6 45.1 20.9 81.1 47.9 91.0 84.355.8 74.9 42.0 53.9 97.7 10.1 18.5 89.1 18.3 88.833.4 41.6 35.5 70.4 23.0 63.6 53.6 42.7 23.9 54.289.5 22.5 51.5 20.1 86.4 34.2 46.3 16.4 44.1 10.792.2 66.6 57.1 40.9 85.7 73.5 36.0 84.8 18.5 12.254.0 87.6 17.9 85.2 71.8 51.3 76.0 85.1 77.0 62.532.0 14.5 50.6 59.7 53.4 65.0 21.9 22.7 75.8 64.827.3 69.4 16.5 72.4 80.9 44.9 53.3 40.9 88.8 31.538.0 61.6 90.1 50.9 35.0 51.7 41.2 83.4 37.4 88.456.4 78.8 64.0 65.0 49.4 89.1 72.5 89.8 54.3 86.291.5 67.4 23.1 21.6 82.2 42.8 88.4 60.1 97.0 27.741.6 49.5 97.3 83.2 29.2 29.3 53.4 28.2 96.4 35.6

    コンピューターって何?

    3In Out

    def next_matrix(matX, matY):n = len(matX)matZ = [ [0] * n for _ in range(n) ]for i in range(n):

    for j in range(n):tmp = INFTYfor k in range(n):

    d = matX[i][k] + matY[k][j]tmp = min(tmp, d)

    matZ[i][j] = tmpreturn matZ

    def sp_length(matA):## Minplus algorithm## TODO: improve complexityn = len(matA)matD = [ [ matA[i][j] for j in range(n) ]

    for i in range(n) ]t = 1while t < n-1:Program

    情報 情報

    コンピューターは

    機械

  • 基本設定と環境

  • ターミナル(Mac)

    Finderを立ち上がる

    アプリケーションを開く

    ユーティリティを開く

    ターミナルを立ち上がる

    5

    $

  • ターミナル(Windows)

    検索窓で

    cmd と入力

    後はここにコマンドを入力していく

    6

    >

    >

  • Google Colab: Python環境

    7

  • Google Colab: Python環境

    +コード を押す

    新しいコードセルができるので,

    そこにプログラムを書き

    ▶(プレイボタン)または「command+return」または「Ctrl+Enter」

    8

  • Google Colab: Python環境

    ファイル

    ドライブにコピーを保存

    で,保存される

    要:google account

    次回以降

    ファイル ノートブックを開く

  • 演習

  • hello_me.py

    11

    person = "Xavier"print("Hello", person)

    person = "Xavier"print(f"Hello {person}")

    person = input("What's your name? ")print(f"Hello {person}")

  • calculate.py

    Try‣ x = 10 ; y = 2‣ x = 5 ; y = 2‣ x = 5 ; y = -2‣ x = 5 ; y = 0

    12

    x = int(input("x = "))y = int(input("y = "))print(f"x+y = {x + y}")print(f"x-y = {x - y}")print(f"x*y = {x * y}")print(f"x/y = {x / y}")print(f"x div y = {x // y}")print(f"x mod y = {x % y}")

  • calculate_better.py

    13

    x = int(input("x = "))y = int(input("y = "))print(f"x+y = {x + y}")print(f"x-y = {x - y}")print(f"x*y = {x * y}")if not y == 0:

    print(f"x/y = {x / y}")print(f"x div y = {x // y}")print(f"x mod y = {x % y}")

    else:print("Division by zero")

  • even_odd.py

    14

    x = int(input("x = "))if x % 2 == 0:

    print(f"x ({x}) is even 偶数")else:

    print(f"x ({x}) is odd 奇数")

    if x > 0:print(f"x ({x}) is positive")

    elif x == 0:print("x is null (zero)")

    else:print(f"x ({x}) is negative")

  • loo.py

    15

    n = int(input("n = "))print ("----------")

    if n < 0 or n > 50:print (f"bad value of n ({n})")exit(1)

    total = 0while n >= 0:

    print (f"n = {n}")n = n - 1total = total + 1

    print("done.")print(f"total = {total} iterations")

  • コマンドラインで実行してみよう

    Mac,Win 共通する基本的な流れ

    プログラムをファイルに保存

    Pythonインタープリタに,ファイルを食わせる

    画面に出力される

  • プログラムをファイルに保存

    好きなエディターを起動‣Windows notepad?‣Mac 標準は何だろう?エディタの中に,プログラムを入力

    ファイル名を付けて保存(例:name.py 等)

    person = input("What's your name? ")print(f"Hello {person}")

  • 実行

    プログラムをpythonインタープリターに食わせる

    名前を入力するとそれが表示される

    <用語>

    プログラムを1行ずつ解釈実行→インタープリタ

    プログラムをまとめて機械語に変換する

    →コンパイラ(機械語の実行ファイルが作成される)

    >python name.py

    >