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
  • How to remove characters from an imported CSV file Python Tutorial
  • YouTube channel lists – Python Lists Python Lists
  • python classes class
  • How To Add Values to a Python Dictionary Python
  • how to write subqueries in SQL SQL
  • How many python functions are there? Python
  • YouTube channel lists – Tuples Python Tuples
  • R tutorial – How to sort lists using rstudio R Programming

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?
Next Post: What are bitwise operators in Julia?

Related Posts

  • What are the reserved keywords in Python Definition
  • TypeError: expected str, bytes or os.PathLike object, not DataFrame Python
  • What Is An Array In Python? array
  • How Would You Change The Name Of a Key in a Python Dictionary Python
  • How to Pass a Javascript Variable to Python using JSON Flask
  • How To Add Values to a Python Dictionary Python

Select your language!

  • English
  • हिंदी
  • Español
  • Français
  • Italiano
  • Deutsch
  • Tkinter GUI tutorial python – how to clean excel data Python
  • How to import data into excel Python Tutorial
  • how to remove unwanted characters from your data R Programming
  • select rows with a certain value using SQL SQL
  • How to create a calculated field in Tableau data visualisation
  • What is data profiling and its benefits? data profiling
  • python classes class
  • How To Create An Empty Dictionary In Python python dictionaries

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