Skip to content

Commit 4e7617b

Browse files
committed
Sample code for the article on syntactic sugar
1 parent 11f745c commit 4e7617b

File tree

8 files changed

+92
-0
lines changed

8 files changed

+92
-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 in Python: Make Your Code Pythonic
2+
3+
This folder provides the code examples for the Real Python tutorial [Syntactic Sugar in Python: Make Your Code 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
debit = 300.00
2+
credit = 450.00
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+
)

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 perimeter(self):
12+
return 2 * pi * self.radius
13+
14+
15+
circle = Circle(10)
16+
print(circle.radius)
17+
print(circle.area())
18+
print(circle.perimeter())
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def is_member(value, iterable):
2+
for current_value in iterable:
3+
if current_value == value:
4+
return True
5+
return False

syntactic-sugar-python/stack.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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

syntactic-sugar-python/timing.py

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

0 commit comments

Comments
 (0)