Funkcje wbudowane

Python dla programistów – poziom średniozaawansowany

Jasmin Ludolf

Senior Data Science Content Developer

Co omówimy

  • Rozdział 1:
    • Funkcje wbudowane, moduły i pakiety

 

  • Rozdział 2 :
    • Funkcje niestandardowe

 

  • Rozdział 3:
    • Obsługa błędów
Python dla programistów – poziom średniozaawansowany

Znane funkcje

# 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 dla programistów – poziom średniozaawansowany

Funkcje wbudowane

  • Pomagają budować funkcjonalności przy mniejszej ilości kodu

$$

$$

$$

  • Panel monitorowania wydajności 🎯

Aplikacja do dostawy jedzenia

Python dla programistów – poziom średniozaawansowany

max() i 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 dla programistów – poziom średniozaawansowany

sum() i 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 dla programistów – poziom średniozaawansowany

len()

  • Zlicza liczbę elementów
# Count the number of orders
print(len(preparation_times))
7
# Calculate average preparation time
print(sum(preparation_times) / len(preparation_times))
28.4585714
Python dla programistów – poziom średniozaawansowany

len()

  • Zlicza znaki, w tym spacje
# Length of a string
print(len("Burger Hub"))
10
Python dla programistów – poziom średniozaawansowany

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 dla programistów – poziom średniozaawansowany

Zalety funkcji

  • Złożone zadania przy mniejszej ilości kodu
# Find total preparation time
print(sum(preparation_times))
199.21
Python dla programistów – poziom średniozaawansowany

Zalety funkcji

# 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() jest wielokrotnego użytku, krótsza, czytelniejsza i mniej podatna na błędy!
19.23
34.9
83.47
106.92
118.98
153.54
199.21
Python dla programistów – poziom średniozaawansowany

Czas na ćwiczenia!

Python dla programistów – poziom średniozaawansowany

Preparing Video For Download...