Python3 組み込み関数

Pythonは多数の組み込み関数(Built-in Functions)を提供しており、ライブラリを一切インポートせずに直接使用できます。これらの関数はデータ型変換、数学計算、反復操作、リフレクション機構などの一般的なシーンをカバーしており、すべてのPython初心者が必ず習得すべき基礎ツールです。

なぜ組み込み関数を習得する必要があるのか?

  • コード量の削減:1行で複数行分に相当(例えばsum()、max())
  • 可読性の向上:意味が明確で、手書きのロジックよりも明瞭
  • 性能がより高い:内部はCで実装されており、通常のPythonループより高速

一般的な分類

  • 数値計算:abs()、round()、min()、max()、sum()

  • 型変換:int()、float()、str()、list()、tuple()

  • 反復と関数型:code>map()、filter()、zip()、enumerate()

  • リフレクションとオブジェクト:type()、isinstance()、getattr()、setattr()

  • 入出力:print()、input()、open()

よく使う関数の例

# 求和
sum([1, 2, 3])  # 6

# 最大值
max([3, 6, 2])  # 6

# 类型转换
int("123")      # 123

# 枚举
for i, v in enumerate(['a', 'b']):
    print(i, v)

# map
list(map(lambda x: x * 2, [1,2,3]))  # [2,4,6]

関数早見表

検索ボックス
組み込み関数
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round() reload()
delattr() hash() memoryview() set()
その他の拡張