Python as we already know, it is an intuitive and implicitly driven language which is extremely user-friendly but relies on a specific structure to implement the code. It does not have a compiler but instead uses an interpreter to debug codes line by line. This function of python calls for specific definitions of keywords within each scope.
A keyword in python are reserved words for specific tasks that one can perform such as list, int, and other various different keywords that are meant to apply a distinct function.
A global keyword on the other hand is used to declare a global variable that is defined outside/inside the scope of any given function and can be accessed globally or inside or outside any given function. Local and Non-Local variables follow suit where local variables are reserved only for a limited scope and non-local variables generally work as pointers or a reference to the nearest scope available.
Global Keyword in Python
A global keyword is pretty self-explanatory, it is a keyword that is accessible globally and in all scopes, either out of or in a function. It can be manipulated or called anywhere in the code as the developer pleases. Let’s create a global variable with the global keyword.
def f1():
global a
a = 1234
f1()
print(a) //even though we have declared the variable inside the function, it will still be accessible outside the function because it is a global variable.
Local Keyword in Python
In a similar manner to a global keyword, a local variable defined with a local keyword will only be available in the scope it is declared in. It will not be accessible outside the given scope/function, let’s take a look at an example below:
def f2():
a = 1234
print(a)
f2() //Since python is an implicit language, we do not need to specify the 'local' keyword, it will automatically set it as a local variable.
If we were to call a print statement with the variable ‘a’, it will present us with an error, take a look at the syntax below:
def f2():
a = 1234
print(a) #This will print it
f2()
print(a) //This print statement will not work and will present an error that the scope of the variable is not defined/detected.
Non-Local Keyword in Python
A non-local variable declared with a nonlocal keyword will only be accessible in a limited/nested scope, it can access the local variable above it and act as a reference variable for the nested scope. It cannot reference a global variable and can neither access a local variable in some other scope. Let’s take a look on how it works:
def f3():
a = 1234 // This is a local variable
Def f4():
nonlocal a
a = 5555 //Turning a value in the nested scope, it will overwrite the local variable
print(a) // this will print the nonlocal variable
print(a) //This will print the overwritten local variable
f3() // calling the parent function
print(a) //This will not print and will present an error as the variable is out of its scope and is declared locally in f3.
Modules in Python
Modules are the collection of keywords and statements that perform specific tasks in python, each module in python can contain an array of functions specified for a certain task which the user can define, let’s make a module:
def add(num):
num = num +10
print(num)
You can now simply save this code in a file with a .py extension such as module.py
Importing a Module With and Without an Alias
You can import modules with an alias or a nickname for it:
Import module
module.add(55)
Import module as m1
m1.add(55)
Apart from creating your own module, there are multiple built-in modules that are available for use in Python.
Packages in Python
A package contains several modules in a singular directory which can be called and imported in a Python environment, to create a package, follow the steps:
1. Create a folder named pack1
2. Place all your .py module files in there and create a separate file named __init__.py
3. You can import your package using the following syntax:
From pack1 import module
module.add(55)
from pack1.module import add
add(100)
Here is how you can manipulate the created packages, there are multiple built-in packages in python or libraries such as:
Math
Time
Statistics