Skip to content

Commit eaae842

Browse files
authored
Merge branch 'master' into python-dicts
2 parents 61aa67d + 4a1c7ba commit eaae842

File tree

12 files changed

+123
-2
lines changed

12 files changed

+123
-2
lines changed

basic-input-output-in-python/adventure_game.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
enemy_health = 3
55

66
while health > 0 and enemy_health > 0:
7-
if input("Attack or Run? ").lower() == "attack":
7+
# Normalize input to handle extra spaces and case variations.
8+
action = input("Attack or Run? ").strip().lower()
9+
if action not in {"attack", "run"}:
10+
print("Invalid choice. Please type 'Attack' or 'Run'.")
11+
continue
12+
13+
if action == "attack":
814
enemy_health -= 1
915
print("You hit the enemy!")
1016
# Implement a 50% chance that the enemy strikes back.

basic-input-output-in-python/guess_the_number.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
if guess == number:
77
print("You got it!")
88
else:
9-
print(f"Sorry, the number was {number}.")
9+
print("Sorry, the number was", number)

python-range/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22

33
This repository holds the code for Real Python's [Python `range()`: Represent Numerical Ranges](https://realpython.com/python-range/) tutorial.
44

5+
## reverse_range()
6+
7+
In [`reverse_range.py`](reverse_range.py), you can find an explicit implementation of a function that can reverse a general range.
8+
9+
```python
10+
>>> from reverse_range import reverse_range
11+
12+
>>> reverse_range(range(1, 20, 4))
13+
range(17, 0, -4)
14+
15+
>>> list(reverse_range(range(1, 20, 4)))
16+
[17, 13, 9, 5, 1]
17+
```
18+
19+
In practical applications, you should use `reversed(range(1, 20, 4))` or `range(1, 20, 4)[::-1]` instead.
20+
521
## PiDigits
622

723
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()`:

python-range/reverse_range.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def reverse_range(rng):
2+
"""Explicitly calculate necessary parameters to reverse a general range.
3+
4+
In practice, you should use reversed() or [::-1] instead.
5+
"""
6+
adj = 1 if rng.step > 0 else -1
7+
return range(
8+
(rng.stop - adj) - (rng.stop - rng.start - adj) % rng.step,
9+
rng.start - adj,
10+
-rng.step,
11+
)
12+
13+
14+
if __name__ == "__main__":
15+
numbers = range(1, 20, 4)
16+
17+
print("\nOriginal:")
18+
print(numbers)
19+
print(list(numbers))
20+
21+
print("\nReversed:")
22+
print(reverse_range(numbers))
23+
print(list(reverse_range(numbers)))
24+
25+
print("\nTwice reversed, has the same elements as the original:")
26+
print(reverse_range(reverse_range(numbers)))
27+
print(list(reverse_range(reverse_range(numbers))))

python-set-comprehension/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Set Comprehensions: How and When to Use Them
2+
3+
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/).

python-set-comprehension/colors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
colors = {"blue", "red", "green", "orange", "green"}
2+
print(colors)
3+
colors.add("purple")
4+
print(colors)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2+
3+
print({number**2 if number % 2 == 0 else number**3 for number in numbers})
4+
5+
result_set = set()
6+
for number in numbers:
7+
if number % 2 == 0:
8+
value = number**2
9+
else:
10+
value = number**3
11+
result_set.add(value)
12+
13+
print(result_set)

python-set-comprehension/emails.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
emails = {
2+
3+
4+
5+
6+
7+
8+
}
9+
10+
print({email.strip().lower() for email in emails})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
emails_set = {
2+
3+
4+
5+
6+
7+
}
8+
9+
print({email for email in emails_set if email.endswith(".com")})

python-set-comprehension/matrix.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
matrix = [
2+
[9, 3, 8, 3],
3+
[4, 5, 2, 8],
4+
[6, 4, 3, 1],
5+
[1, 0, 4, 5],
6+
]
7+
8+
print({value**2 for row in matrix for value in row})

0 commit comments

Comments
 (0)