Eingebaute Funktionen

Entwicklung mit Python für Fortgeschrittene

Jasmin Ludolf

Senior Data Science Content Developer

Was wir behandeln werden

  • Kapitel 1:
    • Eingebaute Funktionen, Module und Pakete

 

  • Kapitel 2:
    • Benutzerdefinierte, individuelle Funktionen

 

  • Kapitel 3:
    • Fehlerbehandlung
Entwicklung mit Python für Fortgeschrittene

Funktionen, die wir kennen

# 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
Entwicklung mit Python für Fortgeschrittene

Eingebaute Funktionen

  • Helfen, Funktionen mit weniger Code zu entwickeln

$$

$$

$$

  • Dashboard zur Leistungsüberwachung 🎯

Essensliefer-App

Entwicklung mit Python für Fortgeschrittene

max() und 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
Entwicklung mit Python für Fortgeschrittene

sum() und 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
Entwicklung mit Python für Fortgeschrittene

len()

  • Zählt die Anzahl der Elemente
# Count the number of orders
print(len(preparation_times))
7
# Calculate average preparation time
print(sum(preparation_times) / len(preparation_times))
28.4585714
Entwicklung mit Python für Fortgeschrittene

len()

  • Zählt die Anzahl der Zeichen, auch Leerzeichen
# Length of a string
print(len("Burger Hub"))
10
Entwicklung mit Python für Fortgeschrittene

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']
Entwicklung mit Python für Fortgeschrittene

Vorteile von Funktionen

  • Lösen komplexe Aufgaben mit weniger Code
# Find total preparation time
print(sum(preparation_times))
199.21
Entwicklung mit Python für Fortgeschrittene

Vorteile von Funktionen

# 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() ist wiederverwendbar, kürzer, sauberer, und weniger anfällig für Fehler!
19.23
34.9
83.47
106.92
118.98
153.54
199.21
Entwicklung mit Python für Fortgeschrittene

Lass uns üben!

Entwicklung mit Python für Fortgeschrittene

Preparing Video For Download...