Part 1: Python as a calculator / Python を電卓として使う

Numbers / 数値:

Integer / 整数 0, 1, 2, 3, -1, -2, -3
Real / 実数 0., 3.1415926, -2.05e30, 1e-4
(must contain a dot or an exponent)
(小数点または指数部を付けます)
Imaginary/Complex / 虚数/複素数 1j, -2.5j, 3+4j
(the last one is a sum) (最後のは和です)
Addition / 足し算 3+4, 42.+3, 1+0j
Subtraction / 引き算 2-5, 3.-1, 3j-7.5
Multiplication / 掛け算 4*3, 2*3.14, 1j*3j
Division / 割り算 1/3, 1./3., 5/3j
Power / べき乗 1.5**3, 2j**2, 2**-0.5

When the two numbers are not of the same type, the result is of the higher type in the order integer-real-complex.

2数が同じ型でないときは、結果は高位(整数-実数-複素数の順)の方の型になります。

Caution: 1/3 is an integer, hence zero. 1./3. is a real number, as expected.

注意: 1/3 は整数なのでゼロです。1./3. は(期待される通りの)実数です。

The precision of real and complex numbers is that of the type double of the C compiler used to generate the Python interpreter. On most systems, this corresponds to a precision of about 16 decimal digits.

実数と複素数の精度は、Python インタープリタを生成したときに使われた C コンパイラの倍精度になります。これは殆どのシステムでは、約 16 桁の精度に相当します。

Mathematical functions / 算術関数:

The standard mathematical functions (sqrt, log, log10, exp, sin, cos, tan, arcsin, arccos, arctan, sin, cosh, plus some others to be mentioned later, as well as the constants pi and e, are not part of the basic language, but contained in a module called Numeric. You must therefore import them before using them:

標準的な算術関数 (sqrt, log, log10, exp, sin, cos, tan, arcsin, arccos, arctan, sin, cosh, 他にいくつか後述) と pie といった定数は、言語に標準装備されてはいなくて、Numeric というモジュールに含まれています。 そこで、使う前にそれらを import する(取込む)必要があります。

They can be imported individually,
それらは個別に import 出来ます:

from Numeric import sqrt
or in groups,
あるいは、まとめて import しても良く:
from Numeric import sin, cos, tan
or you can import everything from that module:
または、そのモジュールから全部を import することも出来ます:
from Numeric import *

You can also import just the module:
さらに、モジュールそのものを import することも可能です:

import Numeric
and then use "extended" names for the functions: Numeric.sqrt, Numeric.exp etc.
この場合、Numeric.sqrtNumeric.exp などの "拡張名" を使います。

Example / 例:

from Numeric import pi, sin
print sin(pi/3)

Text strings / テキスト文字列:

Two forms: 'abc' or "abc"

二つの形式があります: 'abc' または "abc"

Line breaks are indicated by \n: "abc\ndef"

改行は \n で示します: "abc\ndef"

Concatenation: "abc"+'def' gives "abcdef"

結合: "abc"+'def'"abcdef" になります。

Repetition: 3*"ab" gives "ababab"

反復: 3*"ab""ababab" になります。

Vectors / ベクトル:

Vectors are not a fundamental data type in Python. They are defined in the module Scientific.Geometry and must be imported from it.

ベクトルは Python の標準データ型には無く、Scientific.Geometry モジュール内で定義されているので、そこから import する必要があります:

from Scientific.Geometry import Vector

Notation / 記法: Vector(1,0,0)

Addtition, subtraction / 足し算、引き算: Vector(1,0,0)+Vector(0,-1,3), Vector(,1,0)-Vector(1.5,4,0)

Multiplication by a scalar / スカラー倍: 3.5*Vector(1,1,0), Vector(0,0,1)*4.

Division by a scalar / スカラー除算: Vector(1,1,0)/2.

Dot product / 内積: Vector(1,2.5,0)*Vector(0,-1,3.1)

Cross product / 外積: Vector(1,2.5,0).cross(Vector(0,-1,3.1))

Length / 長さ: Vector(2.5, 3.4, 1.).length()

Normalization / 規格化: Vector(1.,4.,2.).normal()

Angle between two vectors / 2ベクトル間の角度: Vector(1,2.5,0).angle(Vector(0,-1,3.1)) (the result is in radians) (結果はラジアン単位)

Accessing components / 要素のアクセス: Vector(1,0,3)[0], Vector(1,4.,3).normal()[1] (indices run from 0 to 2) (添字は 0 から 2)

Variables / 変数:

Variables are used to store and give names to values:

値に名前を付けて格納するために変数を使います:

x = 2.
sum = x + 25
greeting = "hello"
a_very_special_value = 42

Variable names can be arbitrarily long and contain letters and digits. They must begin with a letter. Upper and lower case letters are considered to be different.

変数名は文字と数字を含む任意の長さのものが使えます。始まりは文字でなくてはなりません。大文字と小文字は区別されます。


Your first Python program / 初めての Python プログラム

This program defines three points forming a triangle and prints the distances between all the pairs.

このプログラムは、三角形の3点を定義し、全ての対の距離を印字します。

from Scientific.Geometry import Vector

a = Vector(0, 1, 0)
b = Vector(4.3, -2.4, 0.005)
c = Vector(-3.2, 5.1, -3.)

print "a-b:", (a-b).length()
print "a-c:", (a-c).length()
print "b-c:", (b-c).length()

Exercises / 練習問題


Table of Contents / 目次