LoginSignup
2
6

More than 5 years have passed since last update.

『難しそうなプログラミングをやさしく教えてくれる本』のPythonのプログラム

Last updated at Posted at 2017-09-19

『難しそうなプログラミングをやさしく教えてくれる本』のPythonのプログラムを写経します.
ちなみに,amazon で評価は低いのですが,その理由はタイトルの「やさしく教えてくれ」ていないからのようです.
逆に言えば,Qiita を見るような人には,データ構造,ポインタ,クラス,オブジェクト指向などについて丁寧に書かれている本書はなかなか良書のように思います.
もちろんステマではありません.

p.128 リスト1

ball.py
a=4/3
p=3.14
r=15/2
r3=r**3

print(a*p*r3)

p.129 リスト3

ball.py
a=4/3
p=3.14
r=15/2
r3=r**3

print(a*p*r3)

def show(name, val):
    return name + '=' + str(val)

print( show("a", a) )
print( show("p", p) )
print( show("r", r) )
print( show("r3", r3) )

p.129 図14

>>> varNames = ['a', 'p', 'r', 'r3']
>>> for i in range(len(varNames)):
        print(varNames[i])
>>> for d in varNames:
    print(d)

p.129 図15

>>> varNames = ['a', 'p', 'r']
>>> varNames.remove('a')
>>> varNames
>>> varNames.insert(0, 'a')
>>> varNames
>>> varNames.append('r3')
>>> varNames

p.130 図B

import ui
import console

def button_tapped(sender):
    console.alert("Hello", "ボタンが押された", "OK")

button = ui.Button(title="ボタン")
button.action = button_tapped
button.present("sheet")

p.185 リスト1 ユークリッドの互除法を利用して最大公約数(Greatest Common Divisor, gcd)を計算する


def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
print(gcd(342, 162))
2
6
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
6