hello world
6
Lecture 09 - Global and Local Variables
30 September, 2024
def
and return
if
statementsglobal
keywordapply
and map
functions to apply functions to many variables at once.py
files and how to import them as modulesx = 10
is a global variableNameError
x
is a local variable to the function print_x()
# This is an example where we define a quadratic function
# (x,y) are both local variables of the function
#
# When we call the function, only the arguments matter.
# any intermediate value inside the function
def fn_square(x):
y = x**2
return(y)
x = 5
y = -5
print(fn_square(x = 1))
print(x)
print(y)
1
5
-5
global
keywordglobal
keyword tells Python that you want to use the global variable, not create a new local variableglobal
in my codemodify_x()
again?global y
inside fn_square
?print()
, len()
, sum()
, etc['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BaseExceptionGroup', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EncodingWarning', 'EnvironmentError', 'Exception', 'ExceptionGroup', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '__IPYTHON__', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'aiter', 'all', 'anext', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'display', 'divmod', 'enumerate', 'eval', 'exec', 'execfile', 'filter', 'float', 'format', 'frozenset', 'get_ipython', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed', 'round', 'runfile', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
pandas
is the main library for data manipulation in Python 🐼numpy
and matplotlib
, and has a gazillion functions to work with data 😁R
already, think about it as the dplyr
of Python
dplyr
and pandas
apply
function is used to apply a function to a dataset
df.apply(function)
df.apply(function, axis=0)
applies the function to each column (default)df.apply(function, axis=1)
applies the function to each row# The first two functions return True/False depending on age constraints
# The third function returns the sum of two numbers
# The fourth function returns a string with the age bracket
fn_iseligible_vote = lambda age: age >= 18
fn_istwenties = lambda age: (age >= 20) & (age < 30)
fn_sum = lambda x,y: x + y
def fn_agebracket(age):
if (age >= 18):
status = "Adult"
elif (age >= 10) & (age < 18):
status = "Adolescent"
else:
status = "Child"
return(status)
data["age"]
columndata["can_vote"] = data["age"].apply(fn_iseligible_vote)
data["in_twenties"] = data["age"].apply(fn_istwenties)
data["age_bracket"] = data["age"].apply(fn_agebracket)
display(data)
age | num_underage_siblings | num_adult_siblings | can_vote | in_twenties | age_bracket | |
---|---|---|---|---|---|---|
0 | 18 | 0 | 1 | True | False | Adult |
1 | 29 | 0 | 0 | True | True | Adult |
2 | 15 | 1 | 0 | False | False | Adolescent |
3 | 32 | 1 | 1 | True | False | Adult |
4 | 6 | 0 | 0 | False | False | Child |
apply
functionage | num_underage_siblings | num_adult_siblings | can_vote | in_twenties | age_bracket | new_var | |
---|---|---|---|---|---|---|---|
0 | 18 | 0 | 1 | True | False | Adult | True |
1 | 29 | 0 | 0 | True | True | Adult | True |
2 | 15 | 1 | 0 | False | False | Adolescent | False |
3 | 32 | 1 | 1 | True | False | Adult | True |
4 | 6 | 0 | 0 | False | False | Child | False |
drop
functionmap
function is used to apply a function to a list, an array, or a series
map
works very similarly to the apply
function, and they are interchangeable when working with seriesmap
can be faster than apply
for simple functions, but apply
is more flexible as it can be used with DataFrames (many columns)list01 = [1,2,3]
), you should use map
instead of apply
apply
is not a built-in Python functionmap
with a list and an arrayapply
with a list or an array will raise an error---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[168], line 5
2 list01 = [1,2,3,4,5]
4 # Apply a function to the list
----> 5 list02 = list(apply(lambda x: x**2, list01))
7 print(list02)
NameError: name 'apply' is not defined
num_siblings
\(\ge 1\)has_siblings
apply()
data_raw/features.csv
mpg
\(\ge\) 29mpg_above_29
which is True/False
if mpg
\(\ge\) 29data_clean/features.csv
{fruit,color}
"A {fruit} is {color}"
list_fruits = ["banana","strawberry","kiwi"]
list_colors = ["yellow","red","green"]
list(map())
function to output a list with the form["A banana is yellow","A strawberry is red","A kiwi is green"]
.ipynb
files are great for learning and teaching, they are not the best for sharing code.py
file, which is a Python scriptnum_siblings
\(\ge 1\)has_siblings
apply()
data_raw/features.csv
mpg
\(\ge\) 29mpg_above_29
which is True/False
if mpg
\(\ge\) 29data_clean/features.csv
data_raw = pd.read_csv("data_raw/features.csv")
data_raw["mpg_above_29"] = data_raw["mpg"].apply(lambda mpg: mpg >= 29)
display(data_raw[["mpg","mpg_above_29"]])
data_raw.to_csv("data_clean/features.csv", index = False)
mpg | mpg_above_29 | |
---|---|---|
0 | 18.0 | False |
1 | 15.0 | False |
2 | 18.0 | False |
3 | 16.0 | False |
4 | 17.0 | False |
... | ... | ... |
393 | 27.0 | False |
394 | 44.0 | True |
395 | 32.0 | True |
396 | 28.0 | False |
397 | 31.0 | True |
398 rows × 2 columns
{fruit,color}
"A {fruit} is {color}"
list_fruits = ["banana","strawberry","kiwi"]
list_colors = ["yellow","red","green"]
list(map())
function to output a list with the form["A banana is yellow","A strawberry is red","A kiwi is green"]