You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 9, 2023. It is now read-only.
Lesson 5 - Writing Reusable and Self-explanatory Programs
Ch2 Shortcut Assignments
Swapping x, y = y, x
Augmented assignment operators
+=, -=, *=, /=, //=, %=, **=
Ch4 Refactoring
Break down program into smaller, manageable, and reusable chunks
Chunks should be independent
Function naming
Verb phrases
Indicates return value (bools)
has_something
is_something
Top down approach to designing the program
One function should focus on one thing
Ch5 Advanced Function Features
Default arguments, so that users don't have to explicitly provide a value for everything
Non-default followed by default
Keyword arguments, specify which default value to override
Multiple returns
Returns a Tuple object
Made up of more than one basic object
Ch6 Function Scope
Any variables created inside a function is local to the function
Can't be seen or used outside the function definition
Global scope
When the program is ran
Python gives you a box named global (officially named stack frames)
Every name (including functions) will be put into the box
List of names in each box is called the namespace
Local scope
When python identifies a function
A new local box is created
Local to the function
Global will be made as the new box's parent
Since the function is defined in the global namespace
Finding variables
If a name is not found in the local namespace
Python will go to the global namespace to fetch the name
Ch7 Styling
lowercase_with_underscores()
Use verbs / verb phrases for names
No spaces before parenthesis
No spaces around =
Surround top-level function definition with 2 blank lines
Indentation
Align with opening bracket
Next line, one tab over
Returns
Function must return an expression or never return an expression
Docstrings
Google style
"""Summary line. Extended description of function. Args: arg1 (int): Description of arg1 arg2 (str): Description of arg2 Returns: bool: Description of return value """