静岡理工科大学 | 菅沼ホーム | 目次 | 索引 |
>>> "文字列" '文字列' >>> '文字列' '文字列' >>> b"abc" b'abc' >>> b"文字列" # エラー( ASCII 文字だけ可能)
>>> print("a\nbc") a bc >>> print("a\\nbc") a\nbc >>> print(r"a\nbc") a\nbc
>>> 2 * "abc" + "efg" + "hij" 'abcabcefghij' >>> 2 * "abc" "efg" "hij" 'abcefghijabcefghij'
>>> """ ... abc ... def ... ghi ... """ '\nabc\ndef\nghi\n'
>>> """\ ... abc\ ... def ... ghi\ ... """ 'abcdef\nghi'
\newline 行末に \ を記述するとその行の改行が無視される \\ バックスラッシュ(円記号) \' 一重引用符(シングル クォーテーション) \" 二重引用符(ダブル クォーテーション) \a ベル \b バック スペース \f 改ページ \n 行送り \r 復帰 \t 水平タブ \v 垂直タブ \ooo 8進表記による ASCII 文字 \xhh 16進表記による ASCII 文字
\N{name} Unicode データベース中で name という名前の文字 \uxxxx 16-bit の十六進値 xxxx を持つ文字 \Uxxxxxxxx 32-bit の十六進値 xxxxxxxx を持つ文字
>>> 15 15 >>> 0xf 15 >>> 0o17 15 >>> 0b1111 15
>>> True True >>> False False >>> 10 + True 11 >>> 10 + False 10
>>> 12345.01 12345.01 >>> 1234501e-2 12345.01
>>> 3.14j # 3.14J でも良い 3.14j >>> 2.5 + 3.14j (2.5+3.14j) >>> complex(2.5, 3.14) (2.5+3.14j) >>> complex(2.5, 3.14).real # 実数部の参照(参照だけ可能) 2.5 >>> complex(2.5, 3.14).imag # 虚数部の参照(参照だけ可能) 3.14
int x = 10;
int x = 10; x = 20; x = 1.3; x = "abc";
>>> a = 10 >>> c = complex(1, 2) >>> a = "abc"
False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise
< : より小さい a < b > : より大きい a > b <= : 以下 a <= b >= : 以上 a >= b == : 等しい a == b != : 等しくない a != b
is : 同じオブジェクト a is b is not : 同じオブジェクトではない a is not b
# 整数の場合,文字列の場合も同じ 01 >>> a = 10 02 >>> b = 10 03 >>> a == b, a is b 04 (True, True) 05 >>> b = a 06 >>> a == b, a is b 07 (True, True) 08 >>> b = 20 09 >>> a == b, a is b 10 (False, False) 11 >>> a, b 12 (10, 20) # 浮動小数点数の場合,虚数(複素数)の場合も同じ 13 >>> c = 1.2 14 >>> d = 1.2 15 >>> c == d, c is d 16 (True, False) 17 >>> d = c 18 >>> c == d, c is d 19 (True, True) 20 >>> d = 1.3 21 >>> c == d, c is d 22 (False, False) 23 >>> c, d 24 (1.2, 1.3)
// const 01 const int c10 = 10, c20 = 20; 02 const int *a1, *b1; // a1,b1 は int 型に対するポインタ 03 a1 = &c10; // Python における a = 10 に対応 04 b1 = &c10; // Python における b = 10 に対応 05 printf("%d %d\n", *a1, *b1); // 出力は 10 10(間接演算子 * の使用) 06 b1 = &c20; // Python における b = 20 に対応 07 printf("%d %d\n", *a1, *b1); // 出力は 10 20(間接演算子 * の使用) // const でない 08 int cc10 = 10; 09 int *a2, *b2; 10 a2 = &cc10; 11 b2 = a2; // a2 と b2 の値(アドレス)が等しくなる 12 printf("%d %d\n", *a2, *b2); // 出力は 10 10(間接演算子 * の使用) 13 *b2 = 20; // ポインタ a1 が指す値も変化(間接演算子 * の使用) 14 printf("%d %d\n", *a2, *b2); // 出力は 20 20(間接演算子 * の使用)
式1 or 式2 : 論理和(式1 または 式2 が True であれば True,そうでなければ False) 式1 and 式2 : 論理積(式1 及び 式2 が True であれば True,そうでなければ False) not 式 : 否定(式 が False であれば True,そうでなければ False)
>>> (3 < 2) and (2 < 5) False >>> 3 < 2 < 5 False >>> 3 < 4 < 5 True
+ : 加算(複素数に対しても可能),結合 x + y - : 減算(複素数に対しても可能),符号の変更 x - y, -x * : 乗算(複素数に対しても可能),繰り返し x * y, *x ** : 冪乗(複素数に対しても可能,C/C++ では,pow / pow を利用) x ** y / : 除算(結果は浮動小数点数,複素数に対しても可能) x / y // : 除算(小数点以下を切り捨て,C/C++ では,コメントを表す) x // y % : 剰余(浮動小数点数に対しても可能) x % y
>>> a = 10 >>> a += 5 # a = a + 5 と同じ >>> a 15 >>> a / 2 7.5 >>> a // 2 7
c.conjugate() : 複素数 c の共役複素数 c.real : 複素数 c の実部 c.imag : 複素数 c の虚部
>>> a = complex(1, 2) >>> a (1+2j) >>> a.conjugate() (1-2j) >>> a.real 1.0 >>> a.real = 5 // エラー,参照だけが可能
>>> a = 3 >>> bin(a) '0b11' >>> a.bit_length() 2
>>> a = -3 >>> bin(a) '-0b11' >>> a.to_bytes(4, byteorder='big', signed=True) b'\xff\xff\xff\xfd' >>> int.from_bytes(b'\xff\xff\xff\xfd', byteorder='big', signed=True) -3
>>> a = (0.25).as_integer_ratio() >>> a (1, 4)
>>> (3.2).is_integer() False >>> (3.0).is_integer() True
>>> (3.14).hex() '0x1.91eb851eb851fp+1' >>> float.fromhex('0x1.91eb851eb851fp+1') 3.14
#include <iostream> #include <complex> using namespace std; int main() { complex<double> x1(1.0, 2.0), x2(3.0), x3 = 2.0, x4 = 2i, x5; cout << "x1:" << x1 << ",x2:" << x2 << ",x3:" << x3 << ",x4:" << x4 << ",x5:" << x5 << endl; cout << "x1 + x2 = " << (x1 + x2) << endl; cout << "x1 - x2 = " << (x1 - x2) << endl; cout << "x1 * x2 = " << (x1 * x2) << endl; cout << "x1 / x2 = " << (x1 / x2) << endl; cout << "abs(x1) = " << abs(x1) << endl; cout << "x1 の共役複素数 = " << conj(x1) << endl; return 0; }
x1:(1,2),x2:(3,0),x3:(2,0),x4:(0,2),x5:(0,0) x1 + x2 = (4,2) x1 - x2 = (-2,2) x1 * x2 = (3,6) x1 / x2 = (0.333333,0.666667) abs(x1) = 2.23607 x1 の共役複素数 = (1,-2)
<< : 左にビットシフト a << 3 >> : 右にビットシフト a >> 3 & : ビットごとの論理積 x & y | : ビットごとの論理和 x | y ^ : ビットごとの排他的論理和 x ^ y ~ : 1 の補数( 0 と 1 の逆転) ~x
静岡理工科大学 | 菅沼ホーム | 目次 | 索引 |