Learn Python Complete Series.

Lecture 4: Outline

  • Global Variable
  • Global Keyword
  • Rules for Global Keyword

Global Variable(s);

Variable(s) created outside the function body is called GK(global variables).

Moreover, GlobalVariable accessed by everybody at any place because it has global scope.

However, GK can be used by everyone; both inside and outer side of function(s).

We will discuss functions very deeply in upcoming lectures; so don’t worry about this. Right now, take a concept of global variables using or without using functions; regarding inside and outside of their scope.

Example;

Declare a variable outside and use it inside of the function;

Python Complete Series - 04 1 Top10.Digital

Look at the example above, variable ‘x’ having a value of “awesome,”; is global because outside the function body and the function ‘myfunc’ has printed some output of “python is “ plus includes an ‘x’ value which is global; at the end; the function call is created to print the whole output. The result is Python is awesome;

Click here to learn Python complete series

If creating a variable with the same name plus inner side of function(s); the variable will be a ‘local’ and can access the inner part of a function; not the outer. The GlobalVariable with the same name will remain as it is.

Example;

The variable inner side of a function with same name GK(GlobalVariable)

python series

Global Keyword;

Usually, when creating variable inside or inner part of the function scope, that variable will remain “local.” This can be used as the inner side of the function scope and couldn’t access the outside of the function body. If you want to access that variable outside the function body; use GlobalKeyword.

GlobalKeyword permits you to change or update; the variable’s value outer side of the current space. Create a GlobalVariable and do some changes; at the ‘local’ side of function.

GK(global keyword) allows us to modify the variable outer side of actual value. It is used to create a GlobalVariable and make changes at the ‘local side.’

Rules for Global Keyword;

When creating a variable inside the function body in Python; it’s local by default;

When defining a variable outside the function body; it’s global by default and no need to use a global keyword;

By using a global keyword; we read and write a global variable inside the function body;

Use of GK(global keyword) outside the function does not affect;

To make it global variable inner side of a function; you can use the GLOBAL keyword;

Example;

To use “GLOBAL” keyword, the variable belongs to the global scope;

Python Complete Series - 04 2 Top10.Digital

Also, use the GlobalKeyword; if you want to change a GlobalVariable inside the function.

Example;

Use GK(GlobalKeyword) to change the value of a GlobalVariable at the inner side.

Let’s have a look example below:

learn python

Example;

Retrieving global Var. From Inside of Function

a = 10 #globalVariable because it is outside of function body in Python

def addsum(): #function body start

    print(a) #printing variable

addsum() #function call

Moreover, The text with the # sign is a comment part to guide you; we already discussed it in the previous lectures.

While executing this code, what will happen?

When we run the above program, the result will be: 10

Learn python complete series for free and be an expert. Furthermore, visit the official site of Python here.