Skip to content

some additional explanations #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Finished/Ch_1/bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# demonstrates what is treated as False

if not None:
print("None -> False")

if not 0:
print("0 -> False")

if not '':
print("'' -> False")

if not []:
print("[] -> False")

if not {}:
print("{} -> False")

2 changes: 2 additions & 0 deletions Finished/Ch_2/docstrings_finished.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Example file for Advanced Python: Language Features by Joe Marini
# Demonstrate the use of lambda functions

# Refer to:
# https://peps.python.org/pep-0257

def my_function(arg1, arg2=None):
"""my_function(arg1, arg2=None) --> Doesn't really do anything special.
Expand Down
1 change: 1 addition & 0 deletions Finished/Ch_2/keyargs_finished.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


# use keyword-only arguments to help ensure code clarity
# the `*` parameter enforces the next parameter to be specified by the keyword
def my_function(arg1, arg2, *, suppressExceptions=False):
print(arg1, arg2, suppressExceptions)

Expand Down
2 changes: 1 addition & 1 deletion Finished/Ch_3/listcomp_finished.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
map(lambda e: e**2, filter(lambda e: e > 4 and e < 16, evens)))
print(evenSquared)

# Derive a new list of numbers frm a given list
# Derive a new list of numbers from a given list
evenSquared = [e ** 2 for e in evens]
print(evenSquared)

Expand Down
2 changes: 2 additions & 0 deletions Finished/Ch_4/comparison_finished.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Example file for Advanced Python: Language Features by Joe Marini
# Use special methods to compare objects to each other

# Refer to:
# https://docs.python.org/3/reference/datamodel.html

class Employee():
def __init__(self, fname, lname, level, years_service):
Expand Down
5 changes: 4 additions & 1 deletion Finished/Ch_4/compattrs_finished.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def __getattr__(self, attr):
return (self.red, self.green, self.blue)
elif attr == "hexcolor":
return f"#{self.red:02x}{self.green:02x}{self.blue:02x}"
elif attr == "hex": # a method hex()
return lambda: f"#{self.red:02x}{self.green:02x}{self.blue:02x}"
else:
raise AttributeError(f"{attr} is not a valid attribute")

Expand All @@ -28,14 +30,15 @@ def __setattr__(self, attr, val):

# use dir to list the available properties
def __dir__(self):
return ("rgbolor", "hexcolor")
return ("rgbcolor", "hexcolor")


# create an instance of myColor
cls1 = MyColor()
# print the value of a computed attribute
print(cls1.rgbcolor)
print(cls1.hexcolor)
print(cls1.hex())

# set the value of a computed attribute
cls1.rgbcolor = (125, 200, 86)
Expand Down
1 change: 1 addition & 0 deletions Finished/Ch_4/enums_finished.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ class Fruit(Enum):
myFruits = {}
myFruits[Fruit.BANANA] = "Come Mr. Tally-man"
print(myFruits[Fruit.BANANA])
print(myFruits)
3 changes: 3 additions & 0 deletions Finished/Ch_4/numeric_finished.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Example file for Advanced Python: Language Features by Joe Marini
# give objects number-like behavior

# Refer to:
# https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types


class Point():
def __init__(self, x, y):
Expand Down
2 changes: 2 additions & 0 deletions Finished/Ch_5/simple_finished.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Example file for Advanced Python: Language Features by Joe Marini
# Simple pattern matching using literal values

# Note: match-case requires at least Python 3.10

x = 0

# Literal patterns are explicit values like integers, strings, Booleans, etc
Expand Down