<class 'list'>
True
False
September 8, 2025
<class 'list'>
True
False
mylist = []
creates an empty listtype(mylist)
returns the type of the listisinstance
returns True
or False
for provided typeisinstance(mylist, list)
checks if mylist
is a list
def generator_example(n):
for i in range(n):
yield i
print(type(generator_example))
print(type(generator_example(5)))
<class 'function'>
<class 'generator'>
type
shows that functions and generators have their own typesreturn
and yield
influence a function’s behavior?type
functionu = (3,4)
v = (3,6)
def add(a, b):
return (a[0] + b[0], a[1] + b[1])
def subtract(a,b):
return (a[0] - b[0], a[1] - b[1])
def dot(a, b):
return (a[0] * b[0] + a[1] * b[1])
def norm(a):
return (a[0] * a[0] + a[1] * a[1]) ** 0.5
def isvertical(a):
return a[0] == 0
print(norm(u))
print(add(u,v))
print(u + v)
print(isvertical(subtract(v, u)))
5.0
(6, 10)
(3, 4, 3, 6)
True
tuple
for encoding stateclass Vector:
def __init__(self, x, y):
try:
self.x = float(x)
self.y = float(y)
except ValueError:
self.x = 0.0
self.y = 0.0
def norm(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
def __add__(self, other):
newx = self.x + other.x
newy = self.y + other.y
return Vector(newx, newy)
def __str__(self):
return "({:.2f}, {:.2f})".format(self.x, self.y)
u = Vector(3,4)
v = Vector(3,6)
print(u + v)
(6.00, 10.00)
Vector
, what is the state and behavior? What are the strengths and weaknesses of this approach to representing vectors? How could this approach lead to program defects?Triangle
Triangle
object have?Triangle
object?Square
Square
object have?Square
object?__init__
and __str__
?Square
and a Triangle
?Polygon
class is a superclass of Triangle
and Square
Triangle
and Square
classes are subclasses of Polygon
Polygon
class is a generalization of Triangle
and Square
Triangle
and Square
to the Polygon
SuperclassTriangle
and Polygon
and also Square
and Polygon
MyLimitedList
class “has-a” list inside of it called _L
MyLimitedList
class is a “is-a” of list
Document
can maintain state and exhibit behavior:
Document
classfrom typing import Dict
from datetime import datetime
class Document:
"""A base class representing a document in our document engineering system."""
def __init__(self, title: str, author: str, content: str = ""):
"""Initialize a document with basic properties."""
self.title = title
self.author = author
self.content = content
self.created_date = datetime.now()
self.word_count = len(content.split()) if content else 0
self.metadata: Dict[str, str] = {}
def add_metadata(self, key: str, value: str) -> None:
"""Add metadata to the document."""
self.metadata[key] = value
def get_summary(self) -> str:
"""Generate a summary of the document."""
return f"'{self.title}' by {self.author} ({self.word_count} words)"
def __str__(self) -> str:
"""String representation of the document."""
return self.get_summary()
Document
classDocument
offers the state and behaviors of a document!Document
TechnicalDocument
TechnicalDocument
builds on Document
by adding a new property called level
! What else could we add to this subclass?
DocumentSection
Tips for effective document engineering setup
Devote time outside class to installing and configuring tools
Confirm that most tools work during the this week’s lab session
Create and render test documents with the provided examples
Complete the first document engineering project on time
Contribute to collaborative documentation projects
Prepare for first document engineering skill check
Prosegrammers