내장 함수

개발자를 위한 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...