Python Scope | |||
![]() ![]() ![]() | |||
A variable is only available from inside the region it is created. This is called scope. Local Scope A variable created inside a function belongs to the local scope of that function, and can only be used inside that function.
Function Inside Function As explained in the example above, the variable
Global Scope A variable created in the main body of the Python code is a global variable and belongs to the global scope. Global variables are available from within any scope, global and local.
Naming Variables If you operate with the same variable name inside and outside of a function, Python will treat them as two separate variables, one available in the global scope (outside the function) and one available in the local scope (inside the function):
Global Keyword If you need to create a global variable, but are stuck in the local scope, you can use the The
Also, use the
Tags: Python Python Scope ![]() ![]() | |||
| |||
| |||
|
SIMILAR POSTSPython PolymorphismPython ModulesPython IteratorsPython InheritancePython Classes/ObjectsPython ArraysPython LambdaPython FunctionsPython While Loops/For LoopsPython Conditions and If statementsPython DictionariesPython SetsPython TuplesPython Comparison OperatorsPython Arithmetic OperatorsPrinting string n timesString concatenation by join()Python ListsPython String OperationsPython Data TypesPython VariablesPython CommentsPython SyntaxPython Getting StartedPython IntroductionWhat is Python?Code Chronicles - A Caffeine-Fueled Journey into Data Software EngineeringMachine Learning Types and Programming LanguagesPython Tutorials for AP Computer Science Principles, Data Projects and High School InternshipMastering Excel Data Manipulation with Python |