Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
How to Declare a Global Variable in Python

How to Declare a Global Variable in Python

Posted on January 26, 2025January 26, 2025 By Rishabh saini No Comments on How to Declare a Global Variable in Python

Global Variable in Python

In this tutorial, we will explore how to declare a global variable in Python, understand its scope, and learn how to modify it within functions.

Complete Python Course with Advance topics:-Click here

What is a Global Variable?

A variable that is available both inside and outside of functions and that is defined outside of any function is called a global variable. These variables have a global scope, meaning they can be used anywhere in the program unless overridden locally.

Let’s dive into the concept with an example.

Example 1:How to Define and Use a Global Variable

# Simple Python program to show how to define a global variable    
# Defining a function    
def func():    
    print("Inside the defined function the string is:", var)
    # Printing the global variable within the function    

# Declaring the global variable    
var = "Declaring the Global Variable"    
# Calling the function    
func()     
print("Outside the defined function the string is:", var)    
# Printing the global variable outside the function  

Output:

Inside the defined function the string is: Declaring the Global Variable
Outside the defined function the string is: Declaring the Global Variable

Accessing the Global Variable

The variable var is accessed both inside and outside of the function func() in the example above. It can be utilized across the program because it is declared in the global scope.

Overriding Global Variables with Local Variables

The global variable won’t be impacted if a variable of the same name is declared inside a function; instead, it will be handled as a local variable.

Example 2: Local and Global Variable Conflict

# Defining a function    
def func():    
    var = "Global Variable"  # Declared in local scope    
    print("The local scope variable is:", var)              
    # Printing the local variable

# Declaring the global variable    
var = "I am learning Python"    
func()  # Calling the function  
print("The global scope variable is:", var)    
# Printing the global variable outside the function

Output:

The local scope variable is: Global Variable
The global scope variable is: I am learning Python

In this case, the global variable is unaffected by the local variable var inside the function.

Modifying Global Variables within a Function

If we try to modify a global variable inside a function without declaring it explicitly as global, Python will raise an error.

Example 3: Making an attempt to change a global variable without using a global keyword

# Defining a function    
def func():    
    var = var + " Global Variable"    
    print("Inside the defined function:", var)
    # Trying to modify the global variable

# Declaring the global variable    
var = "Python Tutorial"    
func()  # Calling the function

Output:

UnboundLocalError: local variable 'var' referenced before assignment

Using the global Keyword

The global keyword must be used in order to change a global variable inside of a function.

Example 4: How to Adjust a Global Variable Correctly

# Defining a function    
def func():    
    global var  # Declaring the variable as global  
    var = var + " Global Variable"    
    print("Inside the defined function:", var)    

# Declaring the global variable    
var = "Python Tutorial"    
func()  # Calling the function  
print("Outside the function:", var)   
# Checking if the global variable is modified

Output:

Inside the defined function: Python Tutorial Global Variable
Outside the function: Python Tutorial Global Variable

We tell Python that the variable inside the function refers to the globally defined variable by using the global keyword.

Combining Global and Local Variables

Let’s look at an example that demonstrates the interplay between global and local variables in multiple functions.

Example 5: Managing Local and Global Variables

# Declaring a global variable    
var = 10      

# Function without modifying the global variable

def func1():    
    print('Inside the first function:', var)    

# Function with a local variable

def func2():    
    var = 15    
    print('Inside the second function:', var)    

# Function modifying the global variable

def func3():    
    global var    
    var += 3    
    print("Inside the third function:", var)    

print('Global value of variable:', var)    
func1()    
print('Global value of variable:', var)    
func2()    
print('Global value of variable:', var)    
func3()    
print('Global value of variable:', var)    

Output:

Global value of variable: 10
Inside the first function: 10
Global value of variable: 10
Inside the second function: 15
Global value of variable: 10
Inside the third function: 13
Global value of variable: 13

Best Practices for Using Global Variables

  • Use global variables sparingly to prevent unforeseen consequences.
  • In order to avoid conflicts, give your variables meaningful names.
  • Use constants (declared in uppercase) when variables should remain unchanged.

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Conclusion

  • Global variables allow data sharing across multiple functions.
  • If you want to change a global variable inside a function, use the global keyword.
  • Steer clear of using global variables excessively as this can complicate troubleshooting.

By understanding the proper use of global variables, you can write cleaner and more efficient Python programs.

Stay tuned to UpdateGadh for more Python tutorials and programming insights!


How to Declare a Global Variable in Python
Global Variable in Python
local variable in python
global variable in python class
local and global variables in python with example
python global keyword
use global variable in function, python
how to change global variable in function python
global function python
python global variable across files
online python compiler
lambda function in python
global variable in python example
global variable in python w3schools
global variable in python pdf
global variable in python geeksforgeeks

Post Views: 489
Python Interview Question Tags:global, global and local variables in python, global keyword in python, global variable, global variable in python, global variables, global variables in python, learn python, local and global variables in python, local variable, local variables in python, Python, python global, python global variables, python local & global variables, python local variables, Python Tutorial, python variable scope, python variables, variable scope in python

Post navigation

Previous Post: Customer Relationship Management (CRM) in Django
Next Post: SQL SELECT from Multiple Tables

More Related Articles

is Python a Scripting Language Is Python a Scripting Language? Python Interview Question
How to Compare Two Lists in Python: Methods and Examples - How to Compare Two Lists in Python How to Compare Two Lists in Python: Methods and Examples Python Interview Question
How to Clear the Python Shell How to Clear the Python Shell Python Interview Question

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. How to Install Python on Windows: A Step-by-Step Guide
  2. How to Run Python Program: A Comprehensive Guide for Python Programmers
  3. How to Append Elements to a List in Python
  4. How to Create a DataFrame in Python
  5. Strong Number in Python
  6. Insertion Sort in Python

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,612)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,210)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,866)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme