Bail Reform and Machine Learning
Reading Questions
Quiz
Function Scope
- Variables created inside a function only exist in a function
- They are local to the function - def do_something(): x = 1 do_something() print(x)
- Variables created outside a function can be read inside the function
- They are global variables
- This is desirable when working with constant variable, otherwise discouraged - x = 1 def do_something(): return x + 1 print(do_something())
- Variables created outside a function can not be modified inside the function
- Instead it creates a new variable inside the function with the same name as the variable outside
- The local variable shadows the global variable
- This is desirable, it allows you to name variables inside of functions without having to worry about the function affecting code elsewhere - x = 1 def do_something(): x = 2 do_something() print(x)
- These same rules apply to parameters
- This is common when variables are used as the input to a function - x = 1 def do_something(x): x = 2 do_something() print(x)
Functions Calling Functions
- Functions can call other functions - def do_something(x): b = x + 1 return b def do_something_else(x): a = do_something(x) c = a - 1 return c print(do_something_else(2))