AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Python Scope


Python Tutorial » Python Scope

A variable is only available from inside the region it is created called scope.

Local Scope
A variable created inside a function can only be used inside that function.
Example:
def my_func():
  y = 2021
  print(y)

myfunc()


Output: 2021


Function Inside Function


Variable y is not available outside the function, but it is available for any function inside the function

Example:
def my_func_name():
  y = 5422
  def my_inner_func():
    print(y)
  my_inner_func()

my_func_name()

Output:


Global Keyword

The "global' (without quotes) keyword makes the variable global.
In the next program, three different variables "y" are defined in separate namespaces and accessed accordingly.

Example:

def my_outer_function():
     global y
     y = 220

    def my_inner_function():
      global y
      y = 230
      print('y =', y)

    my_inner_function()
    print('y =', y)

y = 210
my_outer_function()
print('y =', y)

Output:
y = 230
y = 230
y = 230




Python scope : variables, error initializing python engine, could not detect python version, rules, function, of variable in for loop, in future, if, for loop
Python Scope - python

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.