def Function in Python Example

def Function in Python Example. A Python program to print the string using the global declaration method. In this program, we can print the string which is declared global. In Python, a global variable is a variable that is defined outside of the function or in the global scope. Both inside and outside of the code, a global variable can be accessed. 

Let’s have a look at how to create a global variable in Python with an example. The global keyword is used to change a global variable’s scope and meaning. It’s used to make changes to a global variable in a local context. The keyword ‘Global’ is also used to construct or define a global variable within a function.

def myfunc():
global x
x = "awesome in"
print("Python is " + x)



Python is awesome in

Global Variable in Python

In Python, the top of the program is frequently specified as a global variable. In other words, global variables are variables that are defined outside of a function. Both within and outside the function, Python allows you to access global variables. The term global is used before a variable to define it global. The scope of all variables is the block in which they are declared and defined. They can only be utilised once they have been declared.

Local Variable in Python

The definition of a local variable in Python or any other programming language is the same: “A variable specified inside the function is termed a local function.” We can access a local variable within the function but not outside of it.

Types of Variable in Python

Global variables and local variables are the two sorts of variables. Global variables have a scope that spans the whole program, whereas local variables have a scope that is confined to the function in which they are declared. Variables specified outside of a function are known as global variables. Variables specified inside a function are known as local variables. While global variables cannot be modified directly in a function, you may design a function that changes the value of a global variable by using the global keyword. With this program we can execute global and local variables in python.