• Python is not a strongly typed language. That means, unlike some other languages, you don't have to declare a variable before using it
• In python, variables can't be re-assigned??
# Use assign operator (=) to define variable
>>> a = 3
• Cannot start with a number [0-9]
• Cannot conflict with a language keyword
• Can contain: [A-Za-z0-9_-]
• Naming convention: use underscores to separate long variable name
Buckets vs. Labels
• A variable is not a bucket
• A variable is a label that can attach to any buckets
Shadowing/ Rebinding Built-ins
• Don't do it
foo = "hello world!"
print = "this is wrong!" # don't do it
• Variables are said to be defined in the scope in which they are created
• Variables created outside of a function or class are defined at “module scope”
◇ Can be accessed by every function and class created within that module
• Variables created inside of a function or class are defined at “local scope”
◇ Can be accessed by statements executing within the local scope in which they're created
◇ Function arguments are locally scoped variables
"""Demonstrate module vs. locally scoped variables."""
# Create a module variable
module_variable = "I am a module variable."
# Define a function that expects to receive a value for an argument variable
"""Showing how module, argument, and local variables are used."""
# Create a local variable
local_variable = "I am a local variable."
print(module_variable, "...and I can be accessed inside a function.")
print(argument_variable, "...and I can be passed to a function.")
print(local_variable, "...and I can ONLY be accessed inside a function.")
# Call the function; supplying the value for the argument variable
my_function(argument_variable="I am a argument variable.")
# Let's try accessing that local variable here at module scope
print("\nTrying to access local_variable outside of its function...")
except NameError as error:
• The module_variable is created outside of any function or class
• The local_variable is created inside a function
• The argument_variable's name is defined as part of the function definition, but its value isn't assigned until the function is called