Skip to content

Commit a1511c0

Browse files
authored
Merge branch 'master' into python-mappings
2 parents 4ac1323 + b2f1ab8 commit a1511c0

File tree

16 files changed

+336
-0
lines changed

16 files changed

+336
-0
lines changed

python-string-formatting/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python String Formatting: Available Tools and Their Features
2+
3+
This folder provides the code examples for the Real Python tutorial [Python String Formatting: Available Tools and Their Features](https://realpython.com/python-string-formatting/).
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name = "Bob"
2+
print(f"Hello, {name}!")
3+
4+
print(f"The number is {42}")
5+
a = 5
6+
b = 10
7+
print(f"{a} plus {b} is {a + b}")
8+
9+
debit = 300.00
10+
credit = 450.00
11+
print(f"Debit: ${debit}, Credit: ${credit}, Balance: ${credit - debit}")
12+
print(
13+
f"Debit: ${debit:.2f}, Credit: ${credit:.2f}, "
14+
f"Balance: ${credit - debit:.2f}"
15+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
number_template = "The number is {}"
2+
sum_template = "{0} plus {1} is {2}"
3+
4+
print(number_template.format(42))
5+
6+
a = 5
7+
b = 10
8+
print(sum_template.format(a, b, a + b))
9+
10+
debit = 300.00
11+
credit = 450.00
12+
template = "Debit: ${0:.2f}, Credit: ${1:.2f}, Balance: ${2:.2f}"
13+
print(template.format(debit, credit, credit - debit))
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
student = {
2+
"name": "John Doe",
3+
"subjects": [
4+
{
5+
"name": "Mathematics",
6+
"grade": 88,
7+
"comment": "Excellent improvement.",
8+
},
9+
{
10+
"name": "Science",
11+
"grade": 92,
12+
"comment": "Outstanding performance.",
13+
},
14+
{
15+
"name": "History",
16+
"grade": 78,
17+
"comment": "Needs to participate more.",
18+
},
19+
{"name": "Art", "grade": 85, "comment": "Very creative."},
20+
],
21+
}
22+
23+
24+
def build_student_report(student):
25+
report_header = f"Progress Report. Student: {student['name']}"
26+
27+
total = sum(subject["grade"] for subject in student["subjects"])
28+
average = total / len(student["subjects"])
29+
average_report = f"Average: {average:.2f} / 100\n"
30+
31+
subject_report = "Course Details:\n"
32+
for subject in student["subjects"]:
33+
subject_report += (
34+
f"{subject['name']:<15} "
35+
f"Grade: {subject['grade']:3d} "
36+
f"Comment: {subject['comment']}\n"
37+
)
38+
39+
return f"""
40+
{report_header}
41+
{average_report}
42+
{subject_report}
43+
Thank you for reviewing the progress report.
44+
"""
45+
46+
47+
print(build_student_report(student))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
SUBJECT_TEMPLATE = "{name:<15} Grade: {grade:3} Comment: {comment}"
2+
REPORT_TEMPLATE = """
3+
Progress Report. Student: {name}
4+
Average: {average:.2f} / 100
5+
6+
Course Details:
7+
{subjects_report}
8+
9+
Thank you for reviewing the progress report.
10+
"""
11+
12+
student = {
13+
"name": "John Doe",
14+
"subjects": [
15+
{
16+
"name": "Mathematics",
17+
"grade": 88,
18+
"comment": "Excellent improvement.",
19+
},
20+
{
21+
"name": "Science",
22+
"grade": 92,
23+
"comment": "Outstanding performance.",
24+
},
25+
{
26+
"name": "History",
27+
"grade": 78,
28+
"comment": "Needs to participate more.",
29+
},
30+
{"name": "Art", "grade": 85, "comment": "Very creative."},
31+
],
32+
}
33+
34+
35+
def build_student_report(student):
36+
data = {
37+
"name": student["name"],
38+
"average": sum(subject["grade"] for subject in student["subjects"])
39+
/ len(student["subjects"]),
40+
"subjects_report": "\n".join(
41+
SUBJECT_TEMPLATE.format(**subject)
42+
for subject in student["subjects"]
43+
),
44+
}
45+
46+
return REPORT_TEMPLATE.format(**data)
47+
48+
49+
print(build_student_report(student))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
print("%d" % 123.123) # As an integer
2+
print("%o" % 42) # As an octal
3+
print("%x" % 42) # As a hex
4+
print("%e" % 1234567890) # In scientific notation
5+
print("%f" % 42) # As a floating-point number
6+
7+
8+
# Named replacement fields
9+
jane = {"first_name": "Jane", "last_name": "Doe"}
10+
print("Full name: %(first_name)s %(last_name)s" % jane)
11+
# Minimal width of 15 chars
12+
print("%-15f" % 3.1416) # Aligned to the left
13+
print("%15f" % 3.1416) # Aligned to the right
14+
# Dynamic width
15+
width = 15
16+
print("%-*f" % (width, 3.1416))
17+
print("%*f" % (width, 3.1416))
18+
# Precision
19+
print("%.2f" % 3.141592653589793)
20+
print("%.4f" % 3.141592653589793)
21+
print("%.8f" % 3.141592653589793)
22+
23+
24+
print("%o" % 10)
25+
print("%#o" % 10)
26+
print("%x" % 31)
27+
print("%#x" % 31)
28+
29+
print("%05d" % 42)
30+
print("%10d" % 42)
31+
print("%-10d" % 42)
32+
print("% d" % 42)
33+
print("% d" % -42)
34+
print("%+d" % 42)
35+
print("%+d" % -42)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
REPORT_TEMPLATE = """
2+
Monthly Sales Report
3+
--------------------
4+
Report Date Range: {start_date} to {end_date}
5+
6+
Number of Transactions: {sep:.>20} {transactions:,}
7+
Average Transaction Value: {sep:.>11} ${avg_transaction:,.2f}
8+
9+
Total Sales: {sep:.>23} ${total_sales:,.2f}
10+
"""
11+
12+
13+
def build_sales_report(sales_data, report_template=REPORT_TEMPLATE):
14+
total_sales = sum(sale["amount"] for sale in sales_data)
15+
transactions = len(sales_data)
16+
avg_transaction = total_sales / transactions
17+
18+
return report_template.format(
19+
sep=".",
20+
start_date=sales_data[0]["date"],
21+
end_date=sales_data[-1]["date"],
22+
total_sales=total_sales,
23+
transactions=transactions,
24+
avg_transaction=avg_transaction,
25+
)
26+
27+
28+
sales_data = [
29+
{"date": "2024-04-01", "amount": 100},
30+
{"date": "2024-04-02", "amount": 200},
31+
{"date": "2024-04-03", "amount": 300},
32+
{"date": "2024-04-04", "amount": 400},
33+
{"date": "2024-04-05", "amount": 500},
34+
]
35+
36+
print(build_sales_report(sales_data))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# String Interpolation in Python: Exploring Available Tools
2+
3+
This folder provides the code examples for the Real Python tutorial [String Interpolation in Python: Exploring Available Tools](https://realpython.com/python-string-interpolation/).
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Article:
2+
def __init__(self, title, author, pub_date):
3+
self.title = title
4+
self.author = author
5+
self.pub_date = pub_date
6+
7+
def __str__(self):
8+
return (
9+
f"Article: {self.title}\n"
10+
f"Author: {self.author}\n"
11+
f"Published: {self.pub_date}\n"
12+
)
13+
14+
def __repr__(self):
15+
return (
16+
f"{type(self).__name__}("
17+
f"title={self.title}, "
18+
f"author={self.author}, "
19+
f"pub_date={self.pub_date})"
20+
)
21+
22+
23+
if __name__ == "__main__":
24+
article = Article(
25+
title="String Interpolation in Python: Exploring Available Tools",
26+
author="Real Python",
27+
pub_date="2024-05-15",
28+
)
29+
30+
print(f"{article!s}")
31+
print(f"{article!r}")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "Pythonista"
2+
day = "Friday" # Of course 😃
3+
4+
print("Hello, " + name + "! Today is " + day + ".")

0 commit comments

Comments
 (0)