Skip to content

Commit fe6ef2f

Browse files
authored
Merge pull request #579 from realpython/syntactic-sugar-python
Sample code for the article on syntactic sugar
2 parents e52d11b + ca54e62 commit fe6ef2f

File tree

9 files changed

+146
-0
lines changed

9 files changed

+146
-0
lines changed

syntactic-sugar-python/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Syntactic Sugar: Why Python is Sweet and Pythonic
2+
3+
This folder provides the code examples for the Real Python tutorial [Syntactic Sugar: Why Python is Sweet and Pythonic](https://realpython.com/python-syntactic-sugar/).
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
number = 42
2+
if __debug__:
3+
if not number > 0:
4+
raise AssertionError("number must be positive")
5+
6+
number = -42
7+
if __debug__:
8+
if not number > 0:
9+
raise AssertionError("number must be positive")

syntactic-sugar-python/balance.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
debit = 300
2+
credit = 450
3+
4+
print(
5+
f"Debit: ${debit:.2f}, Credit: ${credit:.2f}, Balance: ${credit - debit:.2f}"
6+
)
7+
8+
print(
9+
"Debit: ${:.2f}, Credit: ${:.2f}, Balance: ${:.2f}".format(
10+
debit, credit, credit - debit
11+
)
12+
)
13+
14+
print(
15+
"Debit: $"
16+
+ format(debit, ".2f")
17+
+ ", Credit: $"
18+
+ format(credit, ".2f")
19+
+ ", Balance: $"
20+
+ format(credit - debit, ".2f")
21+
)

syntactic-sugar-python/circle.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from math import pi
2+
3+
4+
class Circle:
5+
def __init__(self, radius):
6+
self.radius = radius
7+
8+
def area(self):
9+
return pi * self.radius**2
10+
11+
def circumference(self):
12+
return 2 * pi * self.radius
13+
14+
15+
circle = Circle(10)
16+
print(circle.radius)
17+
print(circle.area())
18+
print(circle.circumference())
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def is_member(value, iterable):
2+
for current_value in iterable:
3+
if current_value == value:
4+
return True
5+
return False
6+
7+
8+
print(is_member(5, [1, 2, 3, 4, 5]))
9+
print(not is_member(5, [1, 2, 3, 4, 5]))
10+
print(is_member(100, [1, 2, 3, 4, 5]))
11+
print(not is_member(100, [1, 2, 3, 4, 5]))

syntactic-sugar-python/stack.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Stack:
2+
def __init__(self):
3+
self.items = []
4+
5+
def push(self, item):
6+
self.items.append(item)
7+
8+
def pop(self):
9+
return self.items.pop()
10+
11+
def __iter__(self):
12+
yield from self.items
13+
14+
# def __iter__(self):
15+
# return iter(self.items)
16+
17+
# def __iter__(self):
18+
# for item in self.items:
19+
# yield item
20+
21+
22+
stack = Stack()
23+
stack.push("one")
24+
stack.push("two")
25+
stack.push("three")
26+
27+
for item in stack:
28+
print(item)
29+
30+
print(stack.pop())

syntactic-sugar-python/timing.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import functools
2+
import time
3+
4+
5+
def timer(func):
6+
@functools.wraps(func)
7+
def _timer(*args, **kwargs):
8+
start = time.perf_counter()
9+
result = func(*args, **kwargs)
10+
end = time.perf_counter()
11+
print(f"Execution time: {end - start:.4f} seconds")
12+
return result
13+
14+
return _timer
15+
16+
17+
@timer
18+
def delayed_mean(sample):
19+
time.sleep(1)
20+
return sum(sample) / len(sample)
21+
22+
23+
print(delayed_mean([10, 2, 4, 7, 9, 3, 9, 8, 6, 7]))

syntactic-sugar-python/twice.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Twice:
2+
def __init__(self, items):
3+
self.items = list(items)
4+
5+
def __iter__(self):
6+
yield from self.items
7+
print("Halfway there!")
8+
yield from self.items
9+
10+
11+
for number in Twice([1, 2, 3]):
12+
print(f"-> {number}")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# With assignment operator
2+
while (line := input("Type some text: ")) != "stop":
3+
print(line)
4+
5+
6+
# With assignment before loop
7+
line = input("Type some text: ")
8+
9+
while line != "stop":
10+
print(line)
11+
line = input("Type some text: ")
12+
13+
14+
# With condition inside loop
15+
while True:
16+
line = input("Type some text: ")
17+
if line == "stop":
18+
break
19+
print(line)

0 commit comments

Comments
 (0)