From 7b50228ab0d8eee83431dbaa638ff40bd3a6cc64 Mon Sep 17 00:00:00 2001 From: Dan Kjellerson Date: Mon, 11 Mar 2024 19:11:06 -0600 Subject: [PATCH 1/4] updates to latest version of datacamp-light and sets python version to 3.6 --- static/js/datacamp.js | 2 +- templates/base.html | 2 +- templates/index-python.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/static/js/datacamp.js b/static/js/datacamp.js index 16411b6ac..4296139e7 100644 --- a/static/js/datacamp.js +++ b/static/js/datacamp.js @@ -1,5 +1,5 @@ $(function() { - var template = "
{{code}}
", + var template = "
{{code}}
", LINEHEIGHT = 14, EXTRA = 100; diff --git a/templates/base.html b/templates/base.html index 1d4c94bc1..0e8f56420 100644 --- a/templates/base.html +++ b/templates/base.html @@ -202,7 +202,7 @@ {% if domain_data.language == "python" and domain_data.language_code == 'en' %} - + {% endif %} diff --git a/templates/index-python.html b/templates/index-python.html index 54eeaac4f..c5f0e5f11 100644 --- a/templates/index-python.html +++ b/templates/index-python.html @@ -47,7 +47,7 @@

{{ page_title }}

{% if page_title %} -
+
{{tutorial_data.code}} {{tutorial_data.solution}} From 10ff8806bbe6c543ba8624cc914d82cebdd4a88d Mon Sep 17 00:00:00 2001 From: Dan Kjellerson Date: Mon, 11 Mar 2024 19:11:36 -0600 Subject: [PATCH 2/4] adds python tutorial for literal string interpolation --- .../en/Literal String Interpolation.md | 62 +++++++++++++++++++ tutorials/learnpython.org/en/Welcome.md | 1 + 2 files changed, 63 insertions(+) create mode 100644 tutorials/learnpython.org/en/Literal String Interpolation.md diff --git a/tutorials/learnpython.org/en/Literal String Interpolation.md b/tutorials/learnpython.org/en/Literal String Interpolation.md new file mode 100644 index 000000000..9b0d1510a --- /dev/null +++ b/tutorials/learnpython.org/en/Literal String Interpolation.md @@ -0,0 +1,62 @@ +Tutorial +-------- + +In Python 3.6 another way of formatting strings was introdcued called Literal String Interpolation or more commonly known as f-strings due to the f character that proceeds the string. + +String interpolation is the process of replacing the placeholders in the string literal with their corresponding values. This is the same as the C-style formatting from the previous tutorial where the placeholders of %s, %d, etc. are replaced with the values provided after the string literal. + + # This prints out "John is 23 years old." + name = "John" + age = 23 + + print("%s is %d years old." % (name, age)) + +%s is a placeholder for name and %d is a placeholder for age. When the string is interpolated the values of name and age replace the %s and %d symbols. + +F-strings makes string interpolation easier by placing the desired interpolated value in-line with the string inside of curly brackets {}. So instead of `"Hello, %s" % name`, we simply use `f"Hello, {name}"` + + # This also prints out "John is 23 years old." + name = "John" + age = 23 + + print(f"{name} is {age} years old.") + +You may also have noticed this eliminates the need to specify the value's type, as f-strings handle this for you. + +A poweful benefit of F-strings is that they allow you execute expressions or arthmetic inside the curly brackets. + + # This prints out "2 * 6 = 12" + a = 2 + b = 6 + + print(f"{a} * {b} = {a * b}") + +Exercise +-------- + +Rewrite the code using f-strings to produce the following output: + `Hello John Doe. Your new balance is $63.54.` + +Tutorial Code +------------- + +first_name = "John" +last_name = "Doe" +balance = 53.44 +deposit = 10.10 + +print("Hello %s %s. Your current balance is $%f" & (first_name, last_name, balance)) + +Expected Output +--------------- +test_output_contains("Hello John Doe. Your new balance is $63.54.", no_output_msg= "Remember to add the `f` before the string and compute the new balance by adding the deposit") +success_msg('Great work!') + +Solution +-------- +first_name = "John" +last_name = "Doe" +balance = 53.44 +deposit = 10.10 + +print(f"Hello {first_name} {last_name}. Your new balance is ${balance + deposit}") diff --git a/tutorials/learnpython.org/en/Welcome.md b/tutorials/learnpython.org/en/Welcome.md index d9fdd61e7..55e131339 100644 --- a/tutorials/learnpython.org/en/Welcome.md +++ b/tutorials/learnpython.org/en/Welcome.md @@ -22,6 +22,7 @@ Just click on the chapter you wish to begin from, and follow the instructions. G - [[Lists]] - [[Basic Operators]] - [[String Formatting]] +- [[Literal String Interpolation]] - [[Basic String Operations]] - [[Conditions]] - [[Loops]] From 7156a496f76d89d3f56160c9d1445f555f7c7f81 Mon Sep 17 00:00:00 2001 From: Dan Kjellerson Date: Mon, 11 Mar 2024 19:13:43 -0600 Subject: [PATCH 3/4] adds name to list of contributors --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5ed8d094a..4a22f21d7 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ Contributors - qwong95 - AddaxSoft - derco0n +- Kjentleman - ......

This project is supported by:

From 0857aa83c6a656e392e08e79ae266efe8f7753b5 Mon Sep 17 00:00:00 2001 From: Dan Kjellerson Date: Wed, 13 Mar 2024 10:54:15 -0600 Subject: [PATCH 4/4] reworks exercise --- .../en/Literal String Interpolation.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tutorials/learnpython.org/en/Literal String Interpolation.md b/tutorials/learnpython.org/en/Literal String Interpolation.md index 9b0d1510a..b05db49d2 100644 --- a/tutorials/learnpython.org/en/Literal String Interpolation.md +++ b/tutorials/learnpython.org/en/Literal String Interpolation.md @@ -35,28 +35,29 @@ Exercise -------- Rewrite the code using f-strings to produce the following output: - `Hello John Doe. Your new balance is $63.54.` + `Hello John Doe. You have 14 fruit.` Tutorial Code ------------- first_name = "John" last_name = "Doe" -balance = 53.44 -deposit = 10.10 +apples = 10 +pears = 4 -print("Hello %s %s. Your current balance is $%f" & (first_name, last_name, balance)) +# Rewrite this print statement using f-strings +print("Hello %s %s. You have %d apples." % (first_name, last_name, apples)) Expected Output --------------- -test_output_contains("Hello John Doe. Your new balance is $63.54.", no_output_msg= "Remember to add the `f` before the string and compute the new balance by adding the deposit") +test_output_contains("Hello John Doe. You have 14 fruit.", no_output_msg= "Remember to add the `f` before the string and compute the total number of fruit.") success_msg('Great work!') Solution -------- first_name = "John" last_name = "Doe" -balance = 53.44 -deposit = 10.10 +apples = 10 +pears = 4 -print(f"Hello {first_name} {last_name}. Your new balance is ${balance + deposit}") +print(f"Hello {first_name} {last_name}. You have {apples + pears} fruit.")