From eb1b800b652703a5493413297c1c624b480e2571 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Fri, 1 Nov 2024 13:24:13 +0100 Subject: [PATCH 1/5] Sample code for the article on set comprehension --- python-set-comprehension/README.md | 3 +++ python-set-comprehension/colors.py | 4 ++++ python-set-comprehension/complex_expression.py | 13 +++++++++++++ python-set-comprehension/emails.py | 11 +++++++++++ python-set-comprehension/filter_emails.py | 9 +++++++++ python-set-comprehension/matrix.py | 8 ++++++++ python-set-comprehension/text.py | 18 ++++++++++++++++++ python-set-comprehension/tools.py | 8 ++++++++ 8 files changed, 74 insertions(+) create mode 100644 python-set-comprehension/README.md create mode 100644 python-set-comprehension/colors.py create mode 100644 python-set-comprehension/complex_expression.py create mode 100644 python-set-comprehension/emails.py create mode 100644 python-set-comprehension/filter_emails.py create mode 100644 python-set-comprehension/matrix.py create mode 100644 python-set-comprehension/text.py create mode 100644 python-set-comprehension/tools.py diff --git a/python-set-comprehension/README.md b/python-set-comprehension/README.md new file mode 100644 index 0000000000..a0022a23fe --- /dev/null +++ b/python-set-comprehension/README.md @@ -0,0 +1,3 @@ +# Python Set Comprehensions: How and When to Use Them + +This folder provides the code examples for the Real Python tutorial [Python Set Comprehensions: How and When to Use Them](https://realpython.com/python-set-comprehension/). diff --git a/python-set-comprehension/colors.py b/python-set-comprehension/colors.py new file mode 100644 index 0000000000..e99606dad4 --- /dev/null +++ b/python-set-comprehension/colors.py @@ -0,0 +1,4 @@ +colors = {"blue", "red", "green", "orange"} +print(colors) +colors.add("purple") +print(colors) diff --git a/python-set-comprehension/complex_expression.py b/python-set-comprehension/complex_expression.py new file mode 100644 index 0000000000..b23821e1b2 --- /dev/null +++ b/python-set-comprehension/complex_expression.py @@ -0,0 +1,13 @@ +numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + +print({number**2 if number % 2 == 0 else number**3 for number in numbers}) + +result_set = set() +for number in numbers: + if number % 2 == 0: + value = number**2 + else: + value = number**3 + result_set.add(value) + +print(result_set) diff --git a/python-set-comprehension/emails.py b/python-set-comprehension/emails.py new file mode 100644 index 0000000000..2c15763608 --- /dev/null +++ b/python-set-comprehension/emails.py @@ -0,0 +1,11 @@ +emails = [ + " alice@example.org ", + "BOB@example.com", + "charlie@EXAMPLE.com", + "alice@example.org", + "David@example.net", + " bob@example.com", + "JohnDoe@example.com", +] + +print({email.strip().lower() for email in emails}) diff --git a/python-set-comprehension/filter_emails.py b/python-set-comprehension/filter_emails.py new file mode 100644 index 0000000000..9552a7e326 --- /dev/null +++ b/python-set-comprehension/filter_emails.py @@ -0,0 +1,9 @@ +emails_set = { + "alice@example.org", + "bob@example.com", + "johndoe@example.com", + "charlie@example.com", + "david@example.net", +} + +print({email for email in emails_set if email.endswith(".com")}) diff --git a/python-set-comprehension/matrix.py b/python-set-comprehension/matrix.py new file mode 100644 index 0000000000..91ba5d3f66 --- /dev/null +++ b/python-set-comprehension/matrix.py @@ -0,0 +1,8 @@ +matrix = [ + [9, 3, 8, 3], + [4, 5, 2, 8], + [6, 4, 3, 1], + [1, 0, 4, 5], +] + +print({value**2 for row in matrix for value in row}) diff --git a/python-set-comprehension/text.py b/python-set-comprehension/text.py new file mode 100644 index 0000000000..3b394bfc18 --- /dev/null +++ b/python-set-comprehension/text.py @@ -0,0 +1,18 @@ +unique_words = set() +text = """ +Beautiful is better than ugly. +Explicit is better than implicit. +Simple is better than complex. +Complex is better than complicated. +""" + +for word in text.split(): + unique_words.add(word) + +print(unique_words) + +print(set(text.split())) + +unique_words = {word for word in text.split()} + +print(unique_words) diff --git a/python-set-comprehension/tools.py b/python-set-comprehension/tools.py new file mode 100644 index 0000000000..347b1b7803 --- /dev/null +++ b/python-set-comprehension/tools.py @@ -0,0 +1,8 @@ +tools = ["Python", "Django", "Flask", "pandas", "NumPy"] +tools_set = {tool.lower() for tool in tools} + +print(tools_set) +print("python".lower() in tools_set) +print("Pandas".lower() in tools_set) +print("Numpy".lower() in tools_set) +print("Numpy".lower() in tools_set) From 4b0e71fe7b81aaeb19fb48719193fb011bd8aec9 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Fri, 8 Nov 2024 14:11:19 +0100 Subject: [PATCH 2/5] TR updates, first round --- python-set-comprehension/text.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python-set-comprehension/text.py b/python-set-comprehension/text.py index 3b394bfc18..0e75e62995 100644 --- a/python-set-comprehension/text.py +++ b/python-set-comprehension/text.py @@ -1,10 +1,10 @@ unique_words = set() text = """ -Beautiful is better than ugly. -Explicit is better than implicit. -Simple is better than complex. -Complex is better than complicated. -""" +Beautiful is better than ugly +Explicit is better than implicit +Simple is better than complex +Complex is better than complicated +""".lower() for word in text.split(): unique_words.add(word) From 27b84752c77756a901ec2132b18a135c83679f1c Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Fri, 8 Nov 2024 17:26:39 +0100 Subject: [PATCH 3/5] TR updates --- python-set-comprehension/colors.py | 2 +- python-set-comprehension/emails.py | 5 ++--- python-set-comprehension/tools.py | 1 - 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/python-set-comprehension/colors.py b/python-set-comprehension/colors.py index e99606dad4..a7fab40509 100644 --- a/python-set-comprehension/colors.py +++ b/python-set-comprehension/colors.py @@ -1,4 +1,4 @@ -colors = {"blue", "red", "green", "orange"} +colors = {"blue", "red", "green", "orange", "green"} print(colors) colors.add("purple") print(colors) diff --git a/python-set-comprehension/emails.py b/python-set-comprehension/emails.py index 2c15763608..138c79ca95 100644 --- a/python-set-comprehension/emails.py +++ b/python-set-comprehension/emails.py @@ -1,11 +1,10 @@ -emails = [ +emails = { " alice@example.org ", "BOB@example.com", "charlie@EXAMPLE.com", - "alice@example.org", "David@example.net", " bob@example.com", "JohnDoe@example.com", -] +} print({email.strip().lower() for email in emails}) diff --git a/python-set-comprehension/tools.py b/python-set-comprehension/tools.py index 347b1b7803..ee3f1dea32 100644 --- a/python-set-comprehension/tools.py +++ b/python-set-comprehension/tools.py @@ -5,4 +5,3 @@ print("python".lower() in tools_set) print("Pandas".lower() in tools_set) print("Numpy".lower() in tools_set) -print("Numpy".lower() in tools_set) From 7f54af4cc4acea09086c4784d2fb95cc3a07180f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Zaczy=C5=84ski?= Date: Thu, 21 Nov 2024 13:05:30 +0100 Subject: [PATCH 4/5] Sync materials with the tutorial --- basic-input-output-in-python/adventure_game.py | 8 +++++++- basic-input-output-in-python/guess_the_number.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/basic-input-output-in-python/adventure_game.py b/basic-input-output-in-python/adventure_game.py index e634754a35..1d3e363d81 100644 --- a/basic-input-output-in-python/adventure_game.py +++ b/basic-input-output-in-python/adventure_game.py @@ -4,7 +4,13 @@ enemy_health = 3 while health > 0 and enemy_health > 0: - if input("Attack or Run? ").lower() == "attack": + # Normalize input to handle extra spaces and case variations. + action = input("Attack or Run? ").strip().lower() + if action not in {"attack", "run"}: + print("Invalid choice. Please type 'Attack' or 'Run'.") + continue + + if action == "attack": enemy_health -= 1 print("You hit the enemy!") # Implement a 50% chance that the enemy strikes back. diff --git a/basic-input-output-in-python/guess_the_number.py b/basic-input-output-in-python/guess_the_number.py index 3d485326ef..437b5d7af3 100644 --- a/basic-input-output-in-python/guess_the_number.py +++ b/basic-input-output-in-python/guess_the_number.py @@ -6,4 +6,4 @@ if guess == number: print("You got it!") else: - print(f"Sorry, the number was {number}.") + print("Sorry, the number was", number) From c3287780f247dfeeb6a606f17dd930f9e879d809 Mon Sep 17 00:00:00 2001 From: Geir Arne Hjelle Date: Thu, 21 Nov 2024 13:55:59 +0100 Subject: [PATCH 5/5] Add reverse_range() (#615) --- python-range/README.md | 16 ++++++++++++++++ python-range/reverse_range.py | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 python-range/reverse_range.py diff --git a/python-range/README.md b/python-range/README.md index 138a72e91d..6b06fe92b6 100644 --- a/python-range/README.md +++ b/python-range/README.md @@ -2,6 +2,22 @@ This repository holds the code for Real Python's [Python `range()`: Represent Numerical Ranges](https://realpython.com/python-range/) tutorial. +## reverse_range() + +In [`reverse_range.py`](reverse_range.py), you can find an explicit implementation of a function that can reverse a general range. + +```python +>>> from reverse_range import reverse_range + +>>> reverse_range(range(1, 20, 4)) +range(17, 0, -4) + +>>> list(reverse_range(range(1, 20, 4))) +[17, 13, 9, 5, 1] +``` + +In practical applications, you should use `reversed(range(1, 20, 4))` or `range(1, 20, 4)[::-1]` instead. + ## PiDigits The file [`pi_digits.py`](pi_digits.py) shows the implementation of `PiDigits` which is an integer-like type that can be used as arguments to `range()`: diff --git a/python-range/reverse_range.py b/python-range/reverse_range.py new file mode 100644 index 0000000000..4b776905f1 --- /dev/null +++ b/python-range/reverse_range.py @@ -0,0 +1,27 @@ +def reverse_range(rng): + """Explicitly calculate necessary parameters to reverse a general range. + + In practice, you should use reversed() or [::-1] instead. + """ + adj = 1 if rng.step > 0 else -1 + return range( + (rng.stop - adj) - (rng.stop - rng.start - adj) % rng.step, + rng.start - adj, + -rng.step, + ) + + +if __name__ == "__main__": + numbers = range(1, 20, 4) + + print("\nOriginal:") + print(numbers) + print(list(numbers)) + + print("\nReversed:") + print(reverse_range(numbers)) + print(list(reverse_range(numbers))) + + print("\nTwice reversed, has the same elements as the original:") + print(reverse_range(reverse_range(numbers))) + print(list(reverse_range(reverse_range(numbers))))