Skip to content

Commit db8489d

Browse files
authored
Merge pull request #668 from realpython/python-function
Sample code for the article on functions
2 parents 1a15c92 + 842197e commit db8489d

File tree

18 files changed

+205
-0
lines changed

18 files changed

+205
-0
lines changed

python-function/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Defining Your Own Python Function
2+
3+
This folder provides the code examples for the Real Python tutorial [Defining Your Own Python Function](https://realpython.com/defining-your-own-python-function/).

python-function/average.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def cumulative_average(numbers):
2+
total = 0
3+
for items, number in enumerate(numbers, 1):
4+
total += number
5+
yield total / items
6+
7+
8+
values = [5, 3, 8, 2, 5] # Simulates a large data set
9+
10+
for cum_average in cumulative_average(values):
11+
print(f"Cumulative average: {cum_average:.2f}")
12+
13+
14+
def average(*args):
15+
return sum(args) / len(args)
16+
17+
18+
print(average(1, 2, 3, 4, 5, 6))
19+
print(average(1, 2, 3, 4, 5, 6, 7))
20+
print(average(1, 2, 3, 4, 5, 6, 7, 8))

python-function/calculate.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def calculate(x, y, *, operator):
2+
if operator == "+":
3+
return x + y
4+
elif operator == "-":
5+
return x - y
6+
elif operator == "*":
7+
return x * y
8+
elif operator == "/":
9+
return x / y
10+
else:
11+
raise ValueError("invalid operator")
12+
13+
14+
calculate(3, 4, operator="+")
15+
calculate(3, 4, operator="-")
16+
calculate(3, 4, operator="*")
17+
calculate(3, 4, operator="/")

python-function/calculations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def add(a: int | float, b: int | float) -> int | float:
2+
return a + b

python-function/closure.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def function():
2+
value = 42
3+
4+
def closure():
5+
print(f"The value is: {value}!")
6+
7+
return closure
8+
9+
10+
reveal_number = function()
11+
reveal_number()

python-function/cost.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def calculate_cost(item, quantity, price):
2+
print(f"{quantity} {item} cost ${quantity * price:.2f}")
3+
4+
5+
print(calculate_cost("bananas", 6, 0.74))

python-function/double.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def double(numbers):
2+
for i, _ in enumerate(numbers):
3+
numbers[i] *= 2
4+
5+
6+
numbers = [1, 2, 3, 4, 5]
7+
print(double(numbers))

python-function/format_name.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def format_name(first_name, last_name, /, title=None):
2+
full_name = f"{first_name} {last_name}"
3+
if title is not None:
4+
full_name = f"{title} {full_name}"
5+
return full_name
6+
7+
8+
print(format_name("Jane", "Doe"))
9+
print(format_name("John", "Doe", title="Dr."))
10+
print(format_name("Linda", "Brown", "PhD."))

python-function/greeting.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# def greet(name):
2+
# print(f"Hello, {name}!")
3+
4+
5+
def greet(name, verbose=False):
6+
if verbose:
7+
print(f"Hello, {name}! Welcome to Real Python!")
8+
else:
9+
print(f"Hello, {name}!")
10+
11+
12+
greet("Pythonista", verbose=True)
13+
greet("Pythonista")

python-function/hypotenuse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def hypotenuse(a, b, /):
2+
return (a**2 + b**2) ** 0.5

0 commit comments

Comments
 (0)