
Overview
Scope refers to the availability of a variable. Typically, a variable created inside a function is only available from within that function. This is called local scope.
Local Scope
A variable created inside a function belongs to the local scope of that function, and can only be used inside that function.
def myFunction():
x = "Hello World"
print(x)
myFunction()
Note
In the above, the variable “x” is available only within the function “myFunction”. It will ALSO be available for any functions nested within the function “my Function”.
Output:
Hello World
Global Scope
A variable created in the main body of the Python code is a global variable and is available from within any scope, global and local.
x = "Hello World"
def myFunction():
print(x) #refer to the global variable from within a function
myFunction()
print(x) #refer to the global variable outside of any functions
Output:
Hello World
Hello World
Same-Name Variables and Scope
If there are variables with the same name inside and outside of a function, Python will treat them as two separate variables – one global and one local depending on where they are created.
x = 88 #x is global
def myFunction():
x = 44 #x is local to the function
print(x)
myFunction()
print(x)
Output:
44
88
Using the Global Keyword
To create or modify a global variable while in the local scope, the global keyword is used.
Example (creating a global variable locally):
def myFunction():
global x
x = 44
myFunction()
print(x)
Output:
44
The global keyword can also be used to make a change to a global variable inside a function.
Example (changing a global variable locally):
x = 44 #x is global
def myFunction():
global x #make x within the function global so that the value can be changed globally
x = 88
myFunction()
print(x) #though printed outside the function, the value has been updated
Output:
88
Nonlocal Keyword
The nonlocal keyword is used to work with variables inside nested functions, making the variable belong to the outer function.
def myFunction1():
x = 44
def myFunction2():
nonlocal x #makes "x" belong to myFunction1
x = 88
myFunction2()
return x
print(myFunction1())
Output:
88
Python Notes:
- The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
- Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
- Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
- Python string methods return new values, and DO NOT change the original string
- Python tuples are unchangeable after created (their items CANNOT be changed or re-ordered at a later point)
- Python sets are unordered (may appear in random orders when called), unchangeable (the value of individual items cannot be changed after creation), unindexed (items cannot be referred to by index or key), and duplicates are NOT ALLOWED
- As of v3.7, Python dictionaries are ordered and duplicates ARE ALLOWED; in v3.6 and earlier, dictionaries were unordered (did not have a defined order and could not be referred to using an index)
- Python does not have built-in support for arrays, but Python lists can be used as pseudo “arrays”; therefore, all Python list methods will work with these pseudo “arrays”
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.