1/15
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
How do we write code?
so far we… open a file → type some code/sequences of instructions (may contain assignments, loops, conditionals,etc.) to solve a particular problem given.. using only a SINGLE file
Problems of only using a Single file
Easy for small-scale problems but messy for larger problems
hard to keep track of details (to know if the right info is supplied to the right part of code)
Good Programming
more code not necessarily a good thing, instead it’s measured by the amount of functionality (introduce functions!)
Functions
mechanism to achieve decomposition and abstraction; reusable pieces of chunks of code that need to be written; aren’t run in a program until they are “called” or “invoked” in a program
Decomposition
in programming, divide code into modules (achieved with functions and classes); idea is that different devices/ parts of codes (modules) work together to achieve a common end goal; used to create structure
modules
self-contained; used to break up code; intended to be reusable (can be used with different inputs); keeps code organized and coherent
Abstraction
Used to suppress details; in programming, think of a piece of code as a black box (cannot see unnecessary nor wanted tedious coding details) which can be achieved with function specifications or docstrings; idea is to not need to know how program works to use it (just need to know inputs and outputs)
Functions characteristics
has a name
has parameters (0 or more)
a docstring (optional but recommended)
a body
returns something
Sample function
def (name) (parameters):
(indent) “““specification/docstring(multiline comment)”””
(indent) body(run commands and include return statement with expression to evaluate)
Scope
mapping of names to objects
If NO return statement..
Python returns the value None (not Boolean, NoneType) which represents the absence of a value
return vs print
Define a function with a variable that’s also defined outside of function →
no interference
function called with a variable that’s only defined outside of function →
use outside definition
function tries to increment/modify a variable that’s only defined outside →
not allowed in Python but could try global variables
global variables
not recommended to use in Python but they allow for the modification of variables (that are only defined outside of a function) inside a function (defeats purpose of functions)