Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Scope in Python

In Python, Scope determines where a variable is accessible within a program. In simple words, scope tells us where a variable can be used.

CIIT Tip: Think of a variable as something kept inside a room. If the thing is inside the room, it is available in that room. Similarly, Python scope determines where a variable is accessible.

1. What is Scope?

Scope means the variable's visibility and accessibility.

In Python, beginners should mainly understand two important scopes:

  • Local Scope
  • Global Scope

2. Local Scope

A variable created inside a function is generally a local variable. It can be accessed inside the function.

def greet():

    message = "Hello CIIT"

    print(message)


greet()
Output:
Hello CIIT

Here, message is created inside the function, so its scope is limited to the function.

3. Local Variable Outside Function

Trying to access a local variable directly outside the function will result in an error.

def greet():

    message = "Hello CIIT"

    print(message)


greet()

print(message)
Important: message is a local variable of the function. Therefore, it is not directly accessible outside the function.

4. Global Scope

A variable created outside a function is a global variable.

course = "Python Full Stack"


def show_course():

    print(course)


show_course()

print(course)
Output:
Python Full Stack
Python Full Stack

A global variable can also be accessed inside a function.

5. Local Scope vs Global Scope

Local Variable Global Variable
Created inside a function Created outside a function
Accessible inside the function Can be accessible in multiple parts of the program
Limited scope Wider scope

6. Same Variable Name in Different Scopes

The same variable name can exist in local and global scopes.

course = "Python"


def show_course():

    course = "Java"

    print("Inside:", course)


show_course()

print("Outside:", course)
Output:
Inside: Java
Outside: Python

The local course is used inside the function, while the global course is used outside the function.

7. global Keyword

If you want to modify a global variable inside a function, you can use the global keyword.

count = 10


def update_count():

    global count

    count = 20


update_count()

print(count)
Output:
20

8. Why global is Needed?

When an assignment is made inside a function, Python normally treats that variable as a local variable.

count = 10


def update_count():

    count = 20

    print("Inside:", count)


update_count()

print("Outside:", count)
Output:
Inside: 20
Outside: 10

Here, a new local variable was created inside the function. The global variable was not changed.

9. Global Variable Example 🤓❤️

institute = "CIIT Training Institute 🤓👀...!"


def display_institute():

    print(institute)


display_institute()
Output:
CIIT Training Institute 🤓👀...!

10. Function Parameters and Scope

Function parameters also behave like local variables inside the function.

def greet(name):

    print("Hello", name)


greet("Amit")
Output
Hello Amit

Here, name is a parameter and is available in the function's local scope.

11. Nested Function Scope

In Python, one function can be defined inside another function.

def outer():

    message = "Hello from Outer"

    def inner():

        print(message)

    inner()


outer()
Output:
Hello from Outer

The inner function can access the outer function's variable.

12. Enclosing Scope

In nested functions, the outer function's scope is called the enclosing scope.

def outer():

    name = "CIIT"

    def inner():

        print(name)

    inner()


outer()
Output:
CIIT

13. LEGB Rule

When searching for a variable, Python follows the LEGB Rule.

Letter Scope
L Local
E Enclosing
G Global
B Built-in
Search Order:

Local → Enclosing → Global → Built-in

14. Built-in Scope

Python's built-in functions and objects are available in the built-in scope.

numbers = [10, 20, 30]

print(len(numbers))

print(max(numbers))

print(sum(numbers))
Output:
3
30
60

15. CIIT Example 👀🤓

Suppose the institute name is global in a CIIT application, and the student name is local inside the function.

institute = "CIIT Training Institute ❤️😍...!"


def student_details(name):

    course = "Python Full Stack"

    print("Institute:", institute)
    print("Student:", name)
    print("Course:", course)


student_details("Rahul")
Output:
Institute: CIIT Training Institute ❤️😍...!
Student: Rahul
Course: Python Full Stack

16. Scope Best Practices

  • Keep variables limited to the scope where they are required.
  • Avoid unnecessary global variables.
  • Use parameters for function inputs.
  • Use return to get a result from a function.
  • Use meaningful variable names.

17. Common Mistakes

def student():

    name = "Amit"


student()

print(name)

Here, name is a local variable. Therefore, it cannot be accessed outside the function.

Remember: A local variable generally has scope inside the function, whereas a global variable is defined outside the function.

18. Interview Points

  • What is scope in Python?
  • What is the difference between local and global variables?
  • When is the global keyword used?
  • What is the LEGB rule?
  • What is the scope of a function parameter?
  • Can an inner function access a variable from the outer function?

CIIT Learning Point

Understanding scope makes it clear where Python variables can be created, accessed, and modified. Local, Enclosing, Global, and Built-in scopes understanding through the LEGB Rule is important for Python programming.

CIIT Practice Tasks

  1. Local Variable:
    Create a function in which the student's name is a local variable and print it.
  2. Global Variable:
    institute = "CIIT Training Institute" Create a global variable and display it inside the function.
  3. Same Variable Name:
    Use the same variable name in global and local scopes and display both values.
  4. global Keyword:
    Create a global counter variable and inside the function update its value using the global keyword.
  5. Nested Function:
    Create a student name in the outer function and display it from the inner function.
  6. LEGB Practice:
    Create an example of local, enclosing, and global variables and identify which value Python searches for first.

Summary :

In Python, scope determines a variable's visibility and accessibility. A variable created inside a function generally has local scope, while a variable created outside a function has global scope. The enclosing scope is also important in nested functions. Python searches for variables in the LEGB order: Local, Enclosing, Global, and Built-in.