Skip to content

Commit 704e17e

Browse files
authored
Merge pull request #351 from realpython/python-assignment-statements
Sample code for the assignment statements article
2 parents 0ba39d6 + c026afc commit 704e17e

File tree

11 files changed

+99
-0
lines changed

11 files changed

+99
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's Assignment Operator: Write Robust Assignments
2+
3+
This folder provides the code examples for the tutorial [Python's Assignment Operator: Write Robust Assignments](https://realpython.com/python-assignment-operator/).
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def bubble_sort_list(a_list):
2+
n = len(a_list)
3+
for i in range(n):
4+
is_sorted = True
5+
for j in range(n - i - 1):
6+
if a_list[j] > a_list[j + 1]:
7+
a_list[j], a_list[j + 1] = a_list[j + 1], a_list[j]
8+
is_sorted = False
9+
if is_sorted:
10+
break
11+
return a_list
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from math import sqrt
2+
3+
a, b, c = 2.0, -1.0, -4.0
4+
5+
x1, x2 = (
6+
(-b - sqrt(b**2 - 4 * a * c)) / (2 * a),
7+
(-b + sqrt(b**2 - 4 * a * c)) / (2 * a),
8+
)
9+
10+
print(f"{x1=}, {x2=}")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
with open("hello.txt", mode="r") as hello:
2+
print(f"File object: {hello}")
3+
print(f"Memory address: {id(hello)}")
4+
print("File content:")
5+
for line in hello:
6+
print("> ", line)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello, Pythonista!
2+
Welcome to Real Python!
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from platform import python_version
2+
3+
interning = [x for x, y in zip(range(-10, 500), range(-10, 500)) if x is y]
4+
5+
print(
6+
f"Interning interval for Python {python_version()} is:"
7+
f" ({interning[0]} to {interning[-1]})"
8+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# def mean(sample):
2+
# n = len(sample)
3+
# if n == 0:
4+
# raise ValueError("input data required")
5+
# return sum(sample) / n
6+
7+
8+
def mean(sample):
9+
if (n := len(sample)) == 0:
10+
raise ValueError("input data required")
11+
return sum(sample) / n
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Point:
2+
@property
3+
def x(self):
4+
return self._x
5+
6+
@x.setter
7+
def x(self, value):
8+
try:
9+
self._x = float(value)
10+
except ValueError:
11+
raise ValueError('"x" must be a number') from None
12+
13+
@property
14+
def y(self):
15+
return self._y
16+
17+
@y.setter
18+
def y(self, value):
19+
try:
20+
self._y = float(value)
21+
except ValueError:
22+
raise ValueError('"y" must be a number') from None
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Coordinate:
2+
def __set_name__(self, owner, name):
3+
self._name = name
4+
5+
def __get__(self, instance, owner):
6+
return instance.__dict__[self._name]
7+
8+
def __set__(self, instance, value):
9+
try:
10+
instance.__dict__[self._name] = float(value)
11+
except ValueError:
12+
raise ValueError(f'"{self._name}" must be a number') from None
13+
14+
15+
class Point:
16+
x = Coordinate()
17+
y = Coordinate()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def custom_reverse(sequence):
2+
index = len(sequence) - 1
3+
while index >= 0:
4+
yield sequence[index]
5+
index -= 1

0 commit comments

Comments
 (0)