Intro to Functions: Continued Flashcards
Variable
A variable is a name that references a value, or piece of data, stored in computer memory. We can assign values to variables, and read values from variables.
Variable Declaration
The line of code where we first introduce a variable, usually the first place where we give a variable a name and a value
Scope
The area of a program where a given variable can be accessed or used
Local Variable
A kind of variable that has the smallest scope: local scope. The most common kind of variable.
Function
Lines of code (1 or more) that are related, grouped together, and named. Once defined, these lines of code are reusable and can be called over and over again.
Global Variable
A kind of variable that has the largest scope: global scope.
Debugging Variable Scope: NameError
When we try to access a variable that is not in scope, the Python interpreter raises a runtime error NameError for that variable.
Observe and run this code that attempts to access my_undefined_fruit even though my_undefined_fruit is not defined within this code.
No definition of my_undefined_fruit found here!
print(f”An {my_undefined_fruit} a day keeps the doctor away?”)
Our error message gives us a clue that the variable my_undefined_fruit isn’t defined within this scope. Therefore, we got an error for accessing a variable not in scope.
NameError: name ‘my_undefined_fruit’ is not defined
How does a variable’s scope get determined? What are the rules that say what a variable’s scope is?
Variable scope is determined by:
(1) The kind of variable that’s defined
(2) The location (line of code) where the variable is defined
Documentation (aka docs)
Any written text, illustrations or video that describe a software or program to its users
Source code
The original code that creates a program, usually the human-readable version
API
A set of rules and tools that allows software to communicate with other software. Specifically designed to allow access to a wide audience.
Documentation Varies
When comparing the documentation between the pandas library API and the Slack API . We see that they’re dramatically different in a lot of ways:
how they look the literal words how the words are structured how to navigate the page
Object Identity
An object’s unique identity that never changes once it’s been created. Usually a number. Similar to an object’s address in memory. Find this with id(obj). In a comparison that uses is, Python compares object IDs.