top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Variable Scopes in Python

Writer's picture: Ebrima SiseEbrima Sise

In python, a variable is only accessible from inside the area it has been created in. The term `Scope` is used to describe this logic.


A variable can be in one of these types of scopes:

- Local Scope

- Global Scope

- Built-in Scope


Local Scope

In this case, the variable can only be accessible within the code block it's declared

For example, take a look at the variable `a` in the following function.



def func():
    a = 10
    print(a)

`a` is only accessible from within this function. If you try to access `a` outside this function, the program is going to throw a NameError.


Global Scope

If a variable is created in the main body of the python code, it is considered to have a global scope. This makes it accessible from anywhere in the program.


For example, the code below illustrates this logic.


x = 10

def test_func():
    print(x)

test_func()
print(x)

output: 
10
10

From the results above, it is evident that indeed variable `x` is accessible from anywhere in the program.


Built-in Scope

This is actually considered the widest scope in the python programming language. All reserved names in python have a built-in scope. Variables/names in this scope can be accessed from anywhere in the program without the need to declare them.


Maybe an example will go a long way in explaining this.



b = 99.9
b = int(b)
print(type(b))
print(b)

output:

<class 'int'>
99

In the example above, the functions int(), print() and type() all have built in scope.

If we declare variables with the same name in different scopes. The local scope will use the local variable instead of the global one.


We can use the `global` keyword before the variable name if we want the opposite of the above.


We can use the `nonlocal` keyword to accomplish something similar to the `global`.

0 comments

Recent Posts

See All

Comentarios


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page