Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • TypeError: type object is not subscriptable strings
  • How to add a date when a record is created SQL
  • IndexError: list index out of range Index Error
  • R Tutorial: How to pass data between functions R Programming
  • What is the difference between CHAR and VARCHAR2 in SQL? SQL
  • Python Tutorial: Pandas groupby columns ( video 2) Python
  • Python tutorial: Pandas groupby ( Video 1) Python
  • How to run Python directly from Javascript Flask

What are Local and Global Variables in Python?

Posted on March 18, 2023March 18, 2023 By admin

Estimated reading time: 2 minutes

In Python, a variable’s scope determines where the variable can be accessed from within a program. The two main scopes for variables are local and global.

What are Local Variables?

Local variables are defined within a function and can only be accessed within that function. They are not accessible outside the function. Once the function execution is completed, the local variables are destroyed. For example, consider the following code:

# This is an example of a local variable
def my_function_local():
    x = 10
    print(x)

my_function_local()

Output:
10

In this code, x is a local variable that is defined within the my_function_local() function. It can only be accessed within that function, and when the function is completed, the variable x is destroyed.

What are Global Variables?

Global variables, on the other hand, are defined outside of any function and can be accessed throughout the program. They can be accessed from within any function or block of code within the program. For example:

x = 20

def my_function_global():
    print(x)

my_function_global()

Output:
20

In this code, x is a global variable that is defined outside of the my_function_global() function. It can be accessed within the function because it is a global variable.

How should Local and global variables be used?

It is generally recommended to limit the use of global variables in your code and use local variables whenever possible, as global variables can make it difficult to debug and maintain code.

As the code gets larger, it can also be harder to pinpoint where a problem has occurred using global variables, as it may be accessed and used in multiple places.

As a result, an analysis of each piece of code referencing the global variable will need to be undertaken. This may lead to project implementation delays and maintenance costs down the line if not managed correctly.

Python, Python Functions Tags:global variable, local variable

Post navigation

Previous Post: What is Julia used for?

Related Posts

  • How To Pass Data Between Functions Python Functions
  • Regular expressions python Python
  • What are Lambda functions in Python? Python
  • Python tutorial: Create an input box in Tkinter Python
  • How to check if a file is empty Python
  • How Would You Change The Name Of a Key in a Python Dictionary Python

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • How To Fix TypeError: unhashable type ‘slice’ python dictionaries
  • How to create a class in Python class
  • TypeError: ‘int’ object is not callable Python
  • how to remove spaces from a string regular expressions
  • data cleansing in a business environment Articles
  • TypeError: ‘float’ object is not callable Python
  • Why choose Julia over other programming languages? Julia programming
  • How to Automate Testing With Python | unittest automation

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT