😽
Pythonの初心者レッスンーーPython3の組み込み関数
シーリズの目次
Pythonの初心者レッスンをここにまとめています。
以下は、Python3の組み込み関数とその用法を示すテーブルです。
関数 | 説明 | 例 |
---|---|---|
abs() | 絶対値を返す | abs(-5) => 5 |
all() | すべての要素が真である場合にTrueを返す | all([True, True, False]) => False |
any() | いずれかの要素が真である場合にTrueを返す | any([True, False, False]) => True |
ascii() | オブジェクトの印字可能な文字列を返す | ascii('こんにちは') => '\\u3053\\u3093\\u306b\\u3061\\u306f' |
bin() | 整数を2進数文字列に変換する | bin(10) => '0b1010' |
bool() | 値をブール値に変換する | bool(1) => True |
bytearray() | バイト配列を返す | bytearray('abc', 'utf-8') => bytearray(b'abc') |
bytes() | 不変のバイトオブジェクトを返す | bytes('abc', 'utf-8') => b'abc' |
callable() | オブジェクトが呼び出し可能かをチェックする | callable(len) => True |
chr() | Unicodeコードポイントの文字を返す | chr(97) => 'a' |
classmethod() | メソッドをクラスメソッドに変換する | class C: @classmethod def f(cls): pass |
compile() | ソースコードをコードオブジェクトにコンパイルする | compile('print("Hello")', '', 'exec') |
complex() | 複素数を作成する | complex(1, 2) => (1+2j) |
delattr() | オブジェクトの属性を削除する | class C: pass; c = C(); c.x = 1; delattr(c, 'x') |
dict() | 辞書を作成する | dict(a=1, b=2) => {'a': 1, 'b': 2} |
dir() | オブジェクトの属性とメソッドを表示する | dir([]) => ['append', 'clear', 'copy', ...] |
divmod() | 商と余りを返す | divmod(7, 3) => (2, 1) |
enumerate() | 列挙オブジェクトを返す | list(enumerate(['a', 'b', 'c'])) => [(0, 'a'), (1, 'b'), (2, 'c')] |
eval() | 文字列式を評価し結果を返す | eval('1 + 2') => 3 |
exec() | 文字列やコンパイルされたコードオブジェクトを実行する | exec('print("Hello")') |
filter() | 述語がTrueを返す要素を含む新しいイテレータを構築する | list(filter(lambda x: x > 0, [-1, 0, 1, 2])) => [1, 2] |
float() | 値を浮動小数点数に変換する | float('3.14') => 3.14 |
format() | フォーマットされた文字列を返す | format(3.14159, '.2f') => '3.14' |
frozenset() | 凍結されたセット(不変)を返す | frozenset([1, 2, 3]) => frozenset({1, 2, 3}) |
getattr() | オブジェクトの属性値を取得する | getattr(obj, 'name', 'default') |
globals() | 現在のグローバルシンボルテーブルを返す | globals() |
hasattr() | オブジェクトが指定された属性を持っているかを判断する | hasattr(obj, 'name') => True |
hash() | オブジェクトのハッシュ値を返す | hash('test') |
help() | 組み込みのヘルプシステムを呼び出す | help(len) |
hex() | 整数を16進数文字列に変換する | hex(255) => '0xff' |
id() | オブジェクトの一意の識別子を返す | id(obj) |
input() | ユーザー入力を受け取る | input('Enter your name: ') |
int() | 値を整数に変換する | int('123') => 123 |
isinstance() | オブジェクトが指定された型のインスタンスかを判断する | isinstance(123, int) => True |
issubclass() | クラスが他のクラスのサブクラスかを判断する | issubclass(bool, int) => True |
iter() | イテレータを返す | iter([1, 2, 3]) |
len() | オブジェクトの長さまたは項目数を返す | len([1, 2, 3]) => 3 |
list() | リストを作成する | list('abc') => ['a', 'b', 'c'] |
locals() | 現在のローカルシンボルテーブルを返す | locals() |
map() | マッピングされたイテレータを返す | list(map(lambda x: x * 2, [1, 2, 3])) => [2, 4, 6] |
max() | イテレータの最大値を返す | max([1, 2, 3]) => 3 |
memoryview() | メモリビューオブジェクトを返す | memoryview(b'abc') |
min() | イテレータの最小値を返す | min([1, 2, 3]) => 1 |
next() | イテレータの次の項目を返す | it = iter([1, 2, 3]); next(it) => 1 |
object() | 新しい空のオブジェクトを作成する | object() |
oct() | 整数を8進数文字列に変換する | oct(8) => '0o10' |
open() | ファイルを開き、ファイルオブジェクトを返す | open('file.txt', 'r') |
ord() | 文字のUnicodeコードポイントを返す | ord('a') => 97 |
pow() | xのy乗を返す | pow(2, 3) => 8 |
print() | オブジェクトをテキストストリームに印刷する | print('Hello, World!') |
property() | プロパティ属性を返す | property(fget=None, fset=None, fdel=None, doc=None) |
range() | 数列オブジェクトを返す | range(0, 5) => [0, 1, 2, 3, 4] |
repr() | オブジェクトの公式の文字列表現を返す | repr('test') => "'test'" |
reversed() | 反転したイテレータを返す | reversed([1, 2, 3]) => [3, 2, 1] |
round() | 浮動小数点数の丸められた値を返す | round(3.14159, 2) => 3.14 |
set() | セットを作成する | set([1, 2, 3]) => {1, 2, 3} |
setattr() | オブジェクトの属性値を設定する | setattr(obj, 'name', 'value') |
slice() | スライスオブジェクトを返す | slice(0, 5, 2) |
sorted() | ソートされたリストを返す | sorted([3, 1, 2]) => [1, 2, 3] |
staticmethod() | メソッドを静的メソッドに変換する | staticmethod(func) |
str() | オブジェクトを文字列に変換する | str(123) => '123' |
sum() | イテレータの合計を返す | sum([1, 2, 3]) => 6 |
super() | 親クラスのインスタンスを返す | super().__init__() |
tuple() | タプルを作成する | tuple([1, 2, 3]) => (1, 2, 3) |
type() | オブジェクトの型を返す | type(123) => <class 'int'> |
vars() | オブジェクトの__dict__属性を返す | vars(obj) |
zip() | イテレータのアグリゲーターを返す | zip([1, 2, 3], ['a', 'b', 'c']) => [(1, 'a'), (2, 'b'), (3, 'c')] |
import() | import文によって呼び出される | __import__('math') |
Discussion