情報学部 | 菅沼ホーム | 目次 | 索引 |
>>> abs(-3) 3 >>> abs(1+2j) 2.23606797749979
>>> x = 5 >>> lt = [6 > x, 7 > x] >>> all(lt) True
>>> x = 5 >>> lt = [6 > x, 7 < x] >>> any(lt) True
>>> bin(5) '0b101'
>>> bool(5 > 7) False
>>> bytearray(b"abc") bytearray(b'abc')
>>> bytes(b"abc") b'abc'
>>> chr(87) 'W'
>>> complex(2,3) (2+3j) >>> complex(2 + 3j) (2+3j) >>> complex("2+3j") (2+3j)
>>> a = dict(one=1, two=2, three=3) >>> a {'two': 2, 'one': 1, 'three': 3} >>> b = dict(zip(['one', 'two', 'three'], [1, 2, 3])) >>> b {'one': 1, 'two': 2, 'three': 3} >>> c = dict([('two', 2), ('one', 1), ('three', 3)]) >>> c {'two': 2, 'one': 1, 'three': 3} >>> d = dict({'three': 3, 'one': 1, 'two': 2}) >>> d {'one': 1, 'two': 2, 'three': 3}
>>> divmod(3.14, 2) (1.0, 1.1400000000000001)
>>> dic = {"x" : 1, "y" : 2} >>> x = 10 >>> y = 20 >>> eval("x + y") # 現在の環境で実行 30 >>> eval("x + y", dic) # 辞書 dic に基づき実行 3
>>> dic1= dict() >>> dic2= {"a" : 10} >>> exec("a += 20; b = 50", dic1, dic2) >>> dic2 {'b': 50, 'a': 30}
>>> def fun(x) : ... if x > 0 : ... return True ... else : ... return False ... >>> list(filter(fun, [1, 2, -3, -5, 3])) [1, 2, 3]
>>> float("-3.14") -3.14
>>> frozenset([1, 2, 3]) frozenset({1, 2, 3})
>>> class Test : ... abc = 10 ... def fun(self) : ... self.xxx = 20 ... return self.xxx ... >>> x = Test() >>> hasattr(Test, "abc") True >>> hasattr(Test, "fun") True >>> hasattr(Test, "xxx") False >>> hasattr(x, "abc") True >>> hasattr(x, "fun") True >>> hasattr(x, "xxx") False >>> x.fun() 20 >>> hasattr(x, "xxx") # メソッド fun を呼ぶ前とは結果が異なる True
>>> hex(127) '0x7f'
>>> a = input("data? ") data? 10 '10' >>> a '10'
>>> int('010', 2) 2 >>> int(3.14) 3
>>> dic = {"aaa" : 1, "bbb" : 2, "ccc" : 3} >>> it = iter(dic) >>> next(it) 'bbb' >>> next(it) 'aaa' >>> next(it) 'ccc' # 以下,ファイルの読み込みの例 >>> with open('test.txt') as fp: ... for line in iter(fp.readline, ''): ... print(line) ... aaaaaa bbbbbb
>>> len("abc") 3
>>> list("12345") ['1', '2', '3', '4', '5']
>>> def fun(a, b) : ... return a + b ... >>> list(map(fun, [1, 2, 3, 4], [2, 3, 4])) [3, 5, 7]
>>> def func1(y) : ... return y[0] ... >>> def func2(y) : ... return int(y[1]) ... >>> def func3(y) : ... return str(y[1]) ... >>> max(10, 20, 30) 30 >>> a = [10, 20, 30] >>> max(a) 30 >>> x = [(1, 4), (2, '111'), (3, 5)] >>> max(x, key=func1) (3, 5) >>> max(x, key=func2) (2, '111') >>> max(x, key=func3) (3, 5)
>>> def func1(y) : ... return y[0] ... >>> def func2(y) : ... return int(y[1]) ... >>> def func3(y) : ... return str(y[1]) ... >>> min(10, 20, 30) 10 >>> a = [10, 20, 30] >>> min(a) 10 >>> x = [(1, 4), (2, '111'), (3, 5)] >>> min(x, key=func1) (1, 4) >>> min(x, key=func2) (1, 4) >>> min(x, key=func3) (2, '111')
>>> dic = {"aaa" : 1, "bbb" : 2, "ccc" : 3} >>> it = iter(dic) >>> next(it) 'bbb' >>> next(it) 'aaa' >>> next(it) 'ccc'
>>> oct(20) '0o24'
>>> f = open("test.txt", "w") # 出力 >>> f.write("10 20 30\naaaaaa\n菅沼義昇\n") 21 >>> f.close() >>> f = open("test.txt", "r") # 入力 >>> f.read() '10 20 30\naaaaaa\n菅沼義昇\n' >>> f.seek(0) # ファイルの先頭に移動 0 >>> f.readline() '10 20 30\n' >>> f.readline() 'aaaaaa\n' >>> f.readline() '菅沼義昇\n' >>> f.readline() '' # ファイルの終わりを示す >>> f.close()
>>> ord("W") 87
>>> pow(2, 8) 256 >>> pow(2, 8, 10) 6
>>> print(2, 3) 2 3
>>> list(range(1, 10, 2)) [1, 3, 5, 7, 9]
>>> bit = reversed([1, 2, 3, 4]) >>> next(bit) 4 >>> next(bit) 3
>>> round(0.5) 0 >>> round(1.5, 0) 2.0
>>> set([1, 2, 3]) {1, 2, 3}
>>> a = slice(0, 5) >>> a slice(0, 5, None) >>> a.start 0 >>> a.stop 5
>>> sorted([3, 1, 5, 4, 2]) [1, 2, 3, 4, 5] >>> sorted([3, 1, 5, 4, 2], key=None, reverse=True) [5, 4, 3, 2, 1] >>> sorted(['Xyz', 'ABc', 'aaa']) ['ABc', 'Xyz', 'aaa'] >>> sorted(['Xyz', 'ABc', 'aaa'], key=str.lower) ['aaa', 'ABc', 'Xyz']
>>> str(3.14) '3.14'
>>> sum([1, 2, 3], 10) 16
>>> class Par : ... def fun(self) : ... return "値は " ... >>> class Chi(Par) : ... def fun(self, a) : ... str = super().fun() # str = super(Chi, self).fun() と同じ ... print(str, a) ... >>> x = Chi() >>> x.fun(10) 値は 10
>>> tuple(["abc", "xxx", "yyy"]) ('abc', 'xxx', 'yyy')
>>> x = [1, 2, 3] >>> y = [4, 5, 6] >>> z = [7, 8, 9] >>> list(zip(x, y, z)) [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
情報学部 | 菅沼ホーム | 目次 | 索引 |