Skip to content

Commit

Permalink
Merge branch 'master' into python-dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
bzaczynski authored Nov 26, 2024
2 parents 61aa67d + 4a1c7ba commit eaae842
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 2 deletions.
8 changes: 7 additions & 1 deletion basic-input-output-in-python/adventure_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion basic-input-output-in-python/guess_the_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
16 changes: 16 additions & 0 deletions python-range/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`:
Expand Down
27 changes: 27 additions & 0 deletions python-range/reverse_range.py
Original file line number Diff line number Diff line change
@@ -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))))
3 changes: 3 additions & 0 deletions python-set-comprehension/README.md
Original file line number Diff line number Diff line change
@@ -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/).
4 changes: 4 additions & 0 deletions python-set-comprehension/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
colors = {"blue", "red", "green", "orange", "green"}
print(colors)
colors.add("purple")
print(colors)
13 changes: 13 additions & 0 deletions python-set-comprehension/complex_expression.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions python-set-comprehension/emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
emails = {
" [email protected] ",
"[email protected]",
"[email protected]",
"[email protected]",
" [email protected]",
"[email protected]",
}

print({email.strip().lower() for email in emails})
9 changes: 9 additions & 0 deletions python-set-comprehension/filter_emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
emails_set = {
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
}

print({email for email in emails_set if email.endswith(".com")})
8 changes: 8 additions & 0 deletions python-set-comprehension/matrix.py
Original file line number Diff line number Diff line change
@@ -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})
18 changes: 18 additions & 0 deletions python-set-comprehension/text.py
Original file line number Diff line number Diff line change
@@ -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
""".lower()

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)
7 changes: 7 additions & 0 deletions python-set-comprehension/tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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)

0 comments on commit eaae842

Please sign in to comment.