a1 = abs(-3) a2 = abs(7.1) a3 = abs(3+4j) a4 = abs((3+4j)/(1-2j)) print(a1,a2,a3,a4)→ 3 7.1 5.0 2.23606797749979
| 要素1 | 要素2 | 要素n-1 | 要素n | all() | any() | |
| True | True | … | True | True | True | True |
| True | True | … | True | False | False | True |
| True | True | … | False | False | False | True |
| True | False | … | False | False | False | True |
| False | False | … | False | False | False | False |
L1 = []
D1 = {}
S1 = set()
T1 = ()
str1 = ''
B1 = all(L1)
B2 = all(D1)
B3 = all(S1)
B4 = all(T1)
B5 = all(str1)
print(B1, B2, B3, B4, B5)
→ True True True True True[ビルドイン関数のall()]自分が使うときに,この約束に従うのは不合理だと考えるときは,そのプログラムでだけall()関数を次のように上書きして使えば,空データの場合にFalseが返される.
def all(iterable): for element in iterable: if not element: return False return True
def all(iterable):
if len(iterable) == 0:
return False
for element in iterable:
if not element:
return False
return True
これに対して,any()関数はつぎのコードに等しく,空データの場合にFalseを返すので,ビルドイン関数のままで常識感覚と合う.
[ビルドイン関数のany()]
def any(iterable): for element in iterable: if element: return True return False
L1 = [[], {}, set(), (), '']
B1 = any(L1)
print(B1)
→ FalseL1 = [ 2 > 1, -1, 3.1, 1+2j, '0', '',1 == 1.0] B1 = all(L1) print(B1)→ True
L1 = [[], [0],[0,0]] B1 = all(L1) B2 = any(L1) print(B1, B2)→ False True
L1 = [[[]], [(),()]] B1 = all(L1) B2 = any(L1) print(B1, B2)→ True True
B1 = (2 > 3) B2 = (2 <= 4) B3 = (3 > 0) B4 = (1/3*3 == 1) B5 = (1/3 != 0.333) B6 = (1/2 is 0.5) B7 = (1/2 is not 0.4) B8 = (2 in range(3)) B9 = (5 not in range(5)) print(B1 and B2 and B3 and B4 and B5 and B6 and B7 and B8 and B9) L1 = [B1,B2,B3,B4,B5,B6,B7,B8,B9] print(all(L1)) print(B1 or B2 or B3 or B4 or B5 or B6 or B7 or B8 or B9) print(any(L1))→
B1 = (not True)
B2 = (not None)
B3 = (not 0)
B4 = (not 0.0)
B5 = (not 0+0j)
B6 = (not '')
B7 = (not [])
B8 = (not {})
B9 = (not set())
B10 = (not ())
print(B1 and B2 and B3 and B4 and B5 and
B6 and B7 and B8 and B9 and B10)
L1 = [B1,B2,B3,B4,B5,B6,B7,B8,B9,B10]
print(all(L1))
print(B1 or B2 or B3 or B4 or B5 or B6 or
B7 or B8 or B9 or B10)
print(any(L1))
→ print(0 and 2.1) print(1.3 and [] and 'a') print(-1 and 'x' and () and 1.5)→
print(2.1 or 0)
print([] or 3 or 'a')
print(() or '' or {})
→ n1 = 14 n2 = -17 print(bin(n1),oct(n1),hex(n1)) print(bin(n2),oct(n2),hex(n2))→
b1 = '0b1101'
o1 = '-0o107'
x1 = '0xab'
print(int(b1,2), int(o1,8),int(x1,16))
print(int('21',3),int('43',5), int('61',7))
→ print(int(3.14), int(-2.1),int(3/2))→
def n2dec(n_str, base):
t1 = n_str.rpartition('.')
n1 = int(t1[0], base)
d1 = int(t1[2], base)/base**len(t1[2])
return (n1+d1)
print(n2dec('123.1', 4))
→ print(divmod(14,4)) print(divmod(-2.3, 2)) print(divmod(2.5, 1.2))→
def gcd1(a1, b1):
if b1 == 0:
return a1
else:
return gcd1(b1, a1% b1)
def gcd2(a1, b1):
while b1 != 0:
(a1, b1) = (b1, a1 % b1)#(*)
return a1
→ f1 = float(5) f2 = float(-3.1) print(f1,f2)→ 5.0 整数は小数に変換される
f5 = float(3+1.2)+float(4.1-5.4) f6 = float(2/7) print(f5,f6)→ 2.8999999999999995 float()の結果は数値だから,結果を足したり引いたりできる(2.9)
f8 = float('12.3')
f9 = float('-23.5')
f10 = float('4.1e3')
f11 = float('0.3e-3')
print(f8, f9, f10, f11)
→
12.3 10進数と見なせる文字列は小数になるf3 = float(++2.1) f4 = float(4.3---3.1) print(f3,f4)→ 2.1 符号の+は何個書いても,値を変化させない
f12 = float('1,200')
f13 = float('1:2')
f14 = float('2;3')
f15 = float('3=2')
f16 = float([2,3])
f17 = float((2,3))
f1 = float('inf')
f2 = float('-inf')
f3 = float('infinity')
f4 = float('nan')
print(f1,f2,f3,f4)
print(1/f1, 1/f2, 1/f3, 1/f4)
print(f1*2)
→
inf -inf inf nan 無限は'inf','infinity'のいずれも可
• まず,グローバル空間とローカル空間を単純化すれば,左図のようになるでしょう.
str1 = 'abc'
n1 = 12
L1 = [1,2,3]
#1
def fun1(a1, b1):
x1 = a1
y1 = b1
f1 = fun1(3,4)
#2
n2 = 13
#3
→
• globals()が表す名前空間の辞書{変数名:値, ...}は,その時点で読み込まれたものを示し,#1においてglobals()を呼び出すと,システムが使用している __name__ の形のダブルアンダースコアで挟まれたオブジェクトの他,'str1': 'abc', 'n1': 12, 'L1': [1, 2, 3]の3つのグローバル変数がある.#1で読み書き可能な変数は,これだけであることが分かる.
n1 = 1
if 1+1 == 3:
n2 = 5
print(globals())
→
• 変数n2はglobals()の呼び出しよりも前に記述されているが,1+1は3でないから,実際には呼び出されておらず,globals()を呼び出すと,システムが使用している __name__ の形のダブルアンダースコアで挟まれたオブジェクトの他には,{'n1':1}だけが表示される.
str1 = 'abc'
n1 = 12
L1 = [1,2,3]
def fun1(a1, b1):
x1 = a1
y1 = b1
n2 = 5
global n1
→
str1 = 'abc'
n1 = 56
def fun1(a1, b1):
print(locals())#1
x1 = a1
y1 = b1
print(locals())#2
fun1(3,4)
→
str1 = 'abc'
n1 = 56
def fun1(a1, b1):
x1 = a1
y1 = b1
print(globals())#3
fun1(3,4)
→
n0 = 0
def fun1():
n1 = 1
print(n0,n1)#1
def fun2():
n2 = 2
print(n0,n1,n2)#2
→
n0 = 0
def fun1():
n1 = 1
def fun2():
n2 = 2
return
#1
return
#0
関数fun1()の外部#0において,変数n1を読もうとするとエラーになる.(is not defined)
n0 = 0
n1 = 1
def fun1():
#1
def fun2():
#2
return
fun2()
return
fun1()
n0 = 0
def fun1():
n1 = 1
def fun2():
nonlocal n1
n1 = 2
fun2()
print(n1)
return
fun1()
x1 = 3
str1 = 'abc'
def fun1():
y1 = 5
return y1
f1 = fun1()
print(id(x1),id(str1),id(fun1),id(f1))
→ この形で,それぞれの変数,関数のID値(その時の実行環境で割り当てられたメモリアドレス)が分かる.全く同じプログラムでも,1秒後にもう一度実行したとき,同じID値になるとは限らない.x1 = 3 x2 = 4 print(id(x1),id(x2))#1 x2 = x1 print(id(x1), id(x2))#1→ この形で行うと,#1ではx2のID値がx1よりも16だけ後ろになる.(int型は一連の場所に配置されるようだ)
変数x1,x2には,参照先オブジェクトのアドレスが書かれている.
x1 = 3
def fun1():
y1 = 4
return y1
f1 = fun1()
print(id(x1),id(fun1), id(f1))
print(globals())
print(hex(id(fun1)))
• x1はint型,fun1()の戻り値f1もint型だから,id(x1), id(f1)は連番(16だけ大きい).
def fun1():
pass
print(type(3), type('ab'),type([4,5]),type((6,7)),type({1:'a'}))
print(type(fun1))
→
<class 'int'> <class 'str'> <class 'list'> <class 'tuple'> <class 'dict'>