組み込み関数

開発者向け中級 Python

Jasmin Ludolf

Senior Data Science Content Developer

今後の学習内容

  • 第1章:
    • 組み込み関数、モジュール、パッケージ
  • 第2章:
    • カスタム関数
  • 第3章
    • エラー処理
開発者向け中級 Python

これまでに学習した関数

# Printing
print("Password is accepted!")
Password is accepted!
# Checking data types
type(print)
builtin_function_or_method
# Password attempts
for attempt in range(1, 4):
    print("Attempt", attempt)
Attempt 1
Attempt 2
Attempt 3
開発者向け中級 Python

組み込み関数

  • コードを少なくして機能構築を支援

$$

$$

$$

  • パフォーマンス監視ダッシュボード 🎯

フードデリバリーアプリ

開発者向け中級 Python

max() と min()

# List of preparation times (minutes)
preparation_times = [19.23, 15.67, 48.57, 23.45, 12.06, 34.56, 45.67]

# Find the longest preparation time print(max(preparation_times))
48.57
# Find the shortest preparation time
print(min(preparation_times))
12.06
開発者向け中級 Python

sum() と round()

# Calculate the total preparation time
print(sum(preparation_times))
199.21
# Store total time
total_time = sum(preparation_times)

# Round to one decimal place print(round(total_time, 1))
199.2
開発者向け中級 Python

len()

  • 要素数を数える
# Count the number of orders
print(len(preparation_times))
7
# Calculate average preparation time
print(sum(preparation_times) / len(preparation_times))
28.4585714
開発者向け中級 Python

len()

  • テキスト文字列内の文字数を数える
# Length of a string
print(len("Burger Hub"))
10
開発者向け中級 Python

sorted()

# Sort a list in ascending order
print(sorted(preparation_times))
[12.06, 15.67, 19.23, 23.45, 34.56, 
45.67, 48.57]
# Sort a string alphabetically
print(sorted("George"))
['G', 'e', 'e', 'g', 'o', 'r']
開発者向け中級 Python

関数の利点

  • 少ないコードで複雑な処理を実行
# Find total preparation time
print(sum(preparation_times))
199.21
開発者向け中級 Python

関数の利点

# Find total preparation time
# Create a variable to increment
time_count = 0

# Loop through preparation times for time in preparation_times:
# Add each time to time_count time_count += time
print(time_count)
  • sum():再利用可能で短くて読みやすい エラーが起こりにくい!
19.23
34.9
83.47
106.92
118.98
153.54
199.21
開発者向け中級 Python

練習しましょう!

開発者向け中級 Python

Preparing Video For Download...