Skip to content

Commit 4a58799

Browse files
committed
Update resources for completion
Added additional code files, e.g. `shape.py` so that readers can draw the turtle shape. Also renamed files to match the tutorial, and updated the README file.
1 parent 8880d92 commit 4a58799

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

python-sequences/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
Final version of `shaped_abc.py`, which contains the `ShapesPoints` class definition.
1+
# Python Sequences: A Comprehensive Guide
2+
3+
This folder contains source code for the Real Python tutorial [Python Sequences: A Comprehensive Guide](https://realpython.com/python-sequences-comprehensive-guide/).
4+
5+
For more code, intermediate steps, and additional explanation, check out the associated tutorial.

python-sequences/draw_shape.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import turtle
2+
3+
from shape import ShapePoints
4+
5+
triangle = ShapePoints([(100, 100), (-200, 100), (-200, -200)])
6+
7+
# Draw the shape using turtle graphics
8+
turtle.penup()
9+
# Move to the first point
10+
turtle.setposition(triangle[0])
11+
turtle.pendown()
12+
# Draw lines to the other points
13+
for point in triangle[1:]:
14+
turtle.setposition(point)
15+
16+
turtle.done()

python-sequences/shape.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class ShapePoints:
2+
"""
3+
A ShapePoints object represents a collection of points
4+
5+
Attributes:
6+
- points: sequence of points, where each point is a
7+
tuple (x, y)
8+
"""
9+
10+
def __init__(self, points):
11+
self.points = list(points)
12+
if points and self.points[0] != self.points[-1]:
13+
self.points.append(self.points[0])
14+
15+
def __repr__(self):
16+
return f"ShapePoints({self.points})"
17+
18+
def __getitem__(self, index):
19+
return self.points[index]
20+
21+
def __len__(self):
22+
if self.points:
23+
return len(self.points) - 1
24+
return 0
25+
26+
def __iter__(self):
27+
return iter(self.points)

0 commit comments

Comments
 (0)