Code Fellows 401
Day 07 Notes
Home
Reading
NOTE This is a long reading. The portion to make sure you cover is on global and nonlocal keywords. You can skim the rest.
Python Scope
Videos
NOTE* The repeated Big O content is intentional. This is a big concept and worth hearing about from multiple presenters. TIP: watch on faster speed if you like.
Don’t be CONFUSED by BIG O notation anymore!
Bookmark and Review
Rolling Dice Examples
Things I want to know more about
- Variables in Python have a scope, which determines where they can be accessed in the code.
- There are two types of scopes: global and local.
- Global variables are defined outside of functions and can be accessed from anywhere in the code.
- Local variables are defined within a function and can only be accessed within that function.
- When a variable is accessed, Python uses the LEGB rule to determine the scope in which the variable is defined: Local, Enclosed, Global and Built-in.
- The LEGB rule checks for the variable in the following order: Local, Enclosed, Global, Built-in.
- The first place where the variable is found is where it is used.
- If a variable is defined in multiple scopes, the innermost scope takes precedence over the outer scopes.