Scopes in Python

What is scope in Python ? How to use Scope in Python ? Let's learn everything you need to know about Scope in Python with TipsMake !

Scopes in Python Picture 1

A variable is only available within the area it is created in. That is scope in Python. Scope or range in Python has two types:

  1. Local Scope
  2. Global Scope

Local Scope

A variable created inside a function belongs to that function's local scope in Python. And it can only be used inside that function.

For example:

A variable created inside a function is available within that function:

def myfunc(): x = 300 print(x) myfunc()

Functions within functions

As explained in the above example, the variable xis not available outside this function, but it is available to any function inside that function. For example:

Local variables can be accessed from a function inside this function:

def myfunc(): x = 300 def myinnerfunc(): print(x) myinnerfunc() myfunc()

Global Scope

A variable created in the main body of Python code is a global variable and belongs to the global scope.

Global variables are available in any scope, both global and local.

For example:

A variable created outside a function is local and can be used by anyone:

x = 300 def myfunc(): print(x) myfunc() print(x)

Name the variable

If you operate on the same variable name inside and outside of a function, Python will treat them as two separate variables. One is available in the global scope (outside of the function), and one is available in the local scope (inside the function).

For example: The function below will 'print' local scope x , then this code will print global scope x :

x = 300 def myfunc(): x = 200 print(x) myfunc() print(x)

Global keyword

If you need to create a local variable, but are 'stuck' in the local scope, you can use the global- global keyword. Keywords globalcreate global variables.

For example: If you use the keyword global, the variable belongs to the global scope:

def myfunc(): global x x = 300 myfunc() print(x)

Also, use the keyword globalif you want to make changes to a global variable inside a function.

Example: To change the value of a global variable inside a function, refer to the variable using the keyword global:

x = 300 def myfunc(): global x x = 200 myfunc() print(x)

Above are the basic things you need to know about scope in Python . Hope this article helps you better understand scope when programming in Python .

5 ★ | 1 Vote

May be interested

  • How to set up Python to program on WSLHow to set up Python to program on WSL
    get started with cross-platform python programming by setting up python on the windows subsystem for linux. here's how to set up python for wsl programming.
  • Multiple choice quiz about Python - Part 4Multiple choice quiz about Python - Part 4
    continue python's thematic quiz, part 4 goes back to the topic core data type - standard data types in python. let's try with quantrimang to try the 10 questions below.
  • How to use Closure in PythonHow to use Closure in Python
    in this article, tipsmake.com will work with you to learn about closure in python, how to define a closure and why you should use it. let's go find the answer!
  • Functions in PythonFunctions in Python
    what is python function? how is the syntax, components, and function types in python? how to create functions in python? these questions will be answered in the python lesson below.
  • How to use GPT-3 with PythonHow to use GPT-3 with Python
    feel free to use cool gpt-3 technology with your own python scripts using openai's handy api. here are details on how to use gpt-3 with python.
  • Multiple choice quiz about Python - Part 7Multiple choice quiz about Python - Part 7
    following the previous test set, part 7 continues with the topic built-in functions in python. let's try with quantrimang to try the 10 questions below.
  • The next () function in PythonThe next () function in Python
    the next () function in python returns the next element in the iterator. you can add a default value to return if iterable is already the last element.
  • Multiple choice quiz about Python - Part 10Multiple choice quiz about Python - Part 10
    following the previous test, part 10 returned with the python function. let's try with quantrimang to try the 10 questions below.
  • Why should you learn Python programming language?Why should you learn Python programming language?
    python is a multi-purpose programming language created in the late 1980s and named after monty python drama group. let's tipsmake.com find out 3 reasons you should learn python programming language in this article!
  • Package in PythonPackage in Python
    we will learn how to divide code into efficient, clear modules, using python packages. plus, the way to import and use your own package, or the package you download from somewhere into the python program.