About 665,000 results
Open links in new tab
  1. Global and Local Variables in Python - GeeksforGeeks

    Jul 25, 2024 · Local variables in Python are those which are initialized inside a function and belong only to that particular function. It cannot be accessed anywhere outside the function. Let’s see how to create a local variable.

  2. Python Variable Scope (With Examples) - Programiz

    In Python, we can declare variables in three different scopes: local scope, global, and nonlocal scope. A variable scope specifies the region where we can access a variable. For example, sum = 5 + 4. Here, the sum variable is created inside the function, so …

  3. Global and Local Variables in Python with examples

    Local Variable in Python. If the value of the variable can be achieved by a program in which it is defined, that means its scope is limited to a program, called a local variable. Example: Creating a Local Variable in Python. def calculation(): a = 10 a = a*10 print(a) calculation() we defined a variable inside the function, so “a” is local ...

  4. Python: Global and Local Variables with examples, advantages …

    Jun 6, 2024 · Local variables, on the other hand, are declared within a function and can only be accessed and used within that function. They are created when the function starts executing and are destroyed when...

  5. Python Variable Scope with Local & Non-local Examples

    May 11, 2020 · You will learn about the four different scopes with the help of examples: local, enclosing, global, and built-in. These scopes together form the basis for the LEGB rule used by the Python interpreter when working with variables.

  6. Python Scope Rules: Local and Global Variables (With Examples)

    A local scope is a temporary workspace where variables and objects created within a function exist. These variables can only be accessed within the function and are destroyed when the function completes execution. Here is an example that illustrates the concept of …

  7. Python Global and Local Variables (With Examples) - Datamentor

    Local Variables in Python. In Python, a variable declared inside the body of a function or in the local scope is known as a local variable. Suppose we have the following function: def add_numbers(n1, n2): result = n1 + n2. Let's try to print the result variable from outside the function. def add_numbers(n1, n2):

  8. Global and local variables in Python - Analytics Vidhya

    Jan 23, 2024 · There are several differences between global and local variables in Python, including their definition and declaration, access and visibility, lifetime and memory allocation, and modifiability and immutability.

  9. Python’s Global, Local, and Nonlocal Variables - pyseek.com

    Mar 19, 2025 · Local Variables: Defined inside a function and accessible only within that function. Global Variables: Defined outside any function and accessible throughout the script. Nonlocal Variables: Used inside nested functions to modify variables from the enclosing function.

  10. Variable Scope in Python - TutorialsTeacher.com

    Such a variable is called a local variable. Formal argument identifiers also behave as local variables. The following example will underline this point. An attempt to print a local variable outside its scope will raise the NameError exception. name = 'Steve' print('Hello ', name)

  11. Some results have been removed
Refresh