-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_functions.py
More file actions
67 lines (50 loc) · 2 KB
/
Copy path04_functions.py
File metadata and controls
67 lines (50 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Module 04: Functions
Learn: def, parameters, defaults, *args, **kwargs, lambda
In the advanced courses, you'll see:
def calculate_similarity(v1, v2): ...
def execute_tool(name: str, args: dict) -> str: ...
results.sort(key=lambda x: x[1], reverse=True)
"""
# --- Basic function ---
def greet(name):
return f"Hello, {name}!"
print(greet("Param"))
# --- Default parameters ---
def create_message(content, role="user"):
return {"role": role, "content": content}
print(create_message("What are top products?"))
print(create_message("You are helpful.", role="system"))
# --- Multiple return values ---
def analyze_text(text):
words = text.split()
return len(words), len(text)
word_count, char_count = analyze_text("Hello world from Python")
print(f"Words: {word_count}, Characters: {char_count}")
# --- *args and **kwargs ---
def log_event(*args, **kwargs):
"""Accepts any number of positional and keyword arguments."""
print(f"Args: {args}")
print(f"Kwargs: {kwargs}")
log_event("query", "SQL", user="param", score=5)
# --- Lambda functions (small inline functions) ---
# Used in sorting — very common in AI code
results = [("Quinoa Bowl", 0.88), ("Mac & Cheese", 0.53), ("Tofu Tacos", 0.71)]
results.sort(key=lambda x: x[1], reverse=True)
print("Ranked results:")
for dish, score in results:
print(f" [{score:.2f}] {dish}")
# --- Functions calling functions ---
def get_score(item):
return item[1]
# Same result, different style
results.sort(key=get_score, reverse=True)
# --- Useful built-in functions ---
token_counts = [150, 320, 85, 210, 475]
print(f"Total tokens: {sum(token_counts)}") # 1240
print(f"Min tokens: {min(token_counts)}") # 85
print(f"Max tokens: {max(token_counts)}") # 475
# sorted() returns a new sorted list (doesn't modify the original)
print(f"Sorted: {sorted(token_counts)}")
print(f"Top 3: {sorted(token_counts, reverse=True)[:3]}")
# 🎯 Exercise: Write a function `truncate(text, max_len=50)` that returns text[:max_len] + "..."