EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Python 3 Global Variable

Secondary Sidebar
Python 3 Tutorial
  • Python 3 Tutorial
    • Python 3 Commands
    • Python 3 Array
    • Python 3 Operators
    • Python 3 NumPy
    • Python 3 Webserver
    • Python 3 yield
    • Python 3 Pip
    • Python 3 Install
    • Python 3 raw_input
    • Python 3 HTTP Server
    • Python 3 Threading
    • Python 3 Requests
    • Python 3 Module Index
    • Python 3 String Format
    • Python 3 Unicode
    • Python 3 GUI
    • Python 3 xrange
    • Python 3 Tuple
    • Python 3 input
    • Python 3 JSON
    • Python 3 string
    • Python 3 try-except
    • Python 3 RegEx
    • Python 3 Object-Oriented Programming
    • Python 3 zip
    • Python 3 Exception
    • Python 3 write to file
    • Python 3 Functions
    • Python 3 List Function
    • Python 3 While Loop
    • Python 3 Global Variable
    • Python 3 String Methods
    • Python 3 interpreter
    • Python 3 REPL
    • Python 3 else if
    • Python 3 basics
    • Python 3 cheat sheet
    • Python 3 Print
    • Python 3 For Loop
    • Python 3 range
    • Python 3 Dictionary
    • Python 3 has_key
    • Python 3 URLlib
Home Software Development Software Development Tutorials Python 3 Tutorial Python 3 Global Variable

Python 3 Global Variable

Definition of Python 3 Global Variable

Python 3 global variables are variable in Python which was declared outside of scope or function. Global variable can be accessed from both within and outside the function. Inside and outside of functions, has access to global variables. When we create variables into the function, it becomes local and can only be used within the function. The same-named global variable will remain global and with its original value.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)

What is Python 3 Global Variables?

  • Global variables are defined outside of function and the scope of this variable is global, whereas local variables are defined within a function and have a scope that is limited to that function only.
  • To put it another way, local variables are only accessible within the function in which they were initialized, global variables are accessed across the program and throughout each function. Global variables are very useful in Python.

How to Use Python 3 Global Variables?

  • Once defining the global variables in python, we can use the same in the whole program. We have no need to define the same again. The below steps show how to use global variables in python are as follows.

1) A global variable in Python has a scope throughout the program, which implies that the value of a global variable is available throughout the program unless it is shadowed.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

2) The top of the Python program is frequently defined as a global variable. Global variables, to put it another way, are variables defined outside of a function.

3) Below is the syntax of python 3 global variables are as follows.

Syntax:

variable_name = (value of global variable)
def name_of_function ()

4) To keep the shared information and global variables between Python modules inside the same program, we use the config.py module in Python.

5) Below example shows how to use global variables in python are as follows. We are creating a global variable name as “py”.

Code:

py = "global variables"
def globle_fun ():
  	print ("Python 3 " + py)
globle_fun ()

Output:

Python 3 Global Variable 5

  • In the above example, we can see we have used the global variable name as py and we have used this variable outside the function.

6) Below example shows how to use global variables in python are as follows. We are creating a global variable name as “py”. In the below example, we have used a global variable inside the function.

Code:

py = "Global variables"
def global_func ():
  		py = "variables"
  		print ("Python 3 " + py)
global_func ()
print ("Python 3 " + py)

Output:

Python 3 Global Variable 6

Python 3 Global Variables Keyword

  • If we want to conduct assignments or alter the global variable, we merely need to utilise keyword of global a function. For printing and access, global is not required.
  • Python “assumes” that we want a local variable, hence the first sentence throws an error. If a global variable is not declared and is updated or created inside a function, it’s considered local. We must use the keyword “global” to tell Python that we wish to use the global variable.
  • It’s usually local, meaning it may only be utilized within that function. The global keyword can be used to declare a global variable within a function.
  • The keyword of global is used to change a global variable’s scope and meaning outside of its present context. In a local context, it’s utilized to update a global variable. Inside a function, the keyword ‘Global’ can also be used to declare or create a global variable.
  • Below syntax show, global keywords are as follows.

Syntax:

def fun_name ()
Global variable
  • The below example shows after using the global keyword the scope of a variable is belongs to the global. We are creating the global keyword name as py.

Code:

def py_fun ():
  	global py
  	py = "variables"
py_fun ()
print ("Python 3 global " + py)

Output:

Python 3 Global Variable 7

  • The below example shows global variables value inside the function by using global keyword are as follows.

Code:

py = "variables"
def py_fun ():
  	global py
  	py = "variables"
py_fun ()
print("Python 3 global " + py)

Output:

Python 3 Global Variable 8

Python 3 global variables rules

Below is the rule of global variables in python are as follows. Python 3 global variable is defining its own rules.

1) By default, any variable created within a function is local.

2) By default, when a variable is defined outside, it’s global. The usage of a global keyword is not required.

3) To read and write a global variable in a function, we utilize the global keyword.

4) Outside of a function, using the global keyword has no impact.

5) At the time defining a variable with the same name both inside and outside the scope of a function, the value specified within the function will be printed instead of the global value.

6) If we want to conduct assignments or alter the global variable, we merely need to utilize the keyword of global in a function.

Global variable Modules

  • Creating a custom module is the classic technique to communicate data between modules inside a single program. Simply include a module of config in all of our application’s modules, and the module will appear as a global name.
  • Any modifications which were made in the object of the module are mirrored everywhere because each module has just one instance.
  • Modules should be imported at the top of the file. This makes it apparent what other modules our code needs and eliminates any doubts about module name.
  • It’s easier to add and remove module imports when we use one import per line, but utilizing several imports per line saves screen space.
  • Below is the example global variable modules are as follows.

1. First create config.py file

Code:

py = 0
py1 = "null"

a

2. Create update.py file

Code:

import config
config.py = 15
config.py1 = "alpha"

e

3. Create main.py file

Code:

import config
import update
print(config.py)
print(config.py1)

AR

4. Run main.py file

Code:

python main.py

w

Conclusion

When we are creating a variable (a local variable) inside a function, it is usually limited to that function. The keyword of global comes in handy, allowing us to establish global variables within the function that may be accessed from anywhere in the world. Global variables are very important in python.

Recommended Articles

This is a guide to Python 3 global variable. Here we discuss the definition, What is python 3 global variables, How to use python 3 global variables, examples with code implementation. You may also look at the following articles to learn more-

  1. Python 3 Cheat Sheet
  2. Python 3 Commands
  3. Python 3 vs Python 2
  4. Python 2 vs Python 3
Popular Course in this category
Python Certifications Training Program (40 Courses, 13+ Projects)
  40 Online Courses |  13 Hands-on Projects |  215+ Hours |  Verifiable Certificate of Completion
4.8
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Python Certifications Training Program (40 Courses, 13+ Projects) Learn More