-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
Scope refers to the visibility or lifetime of a variable — where in the code you can access it. Python has different types of scopes depending on where the variable is declared.
A variable created inside a function is in the local scope and can only be used inside that function.
def greet():
name = "Alice"
print("Hello", name)
greet()
# print(name) # This will cause an error
A variable created outside of any function is in the global scope and can be used anywhere in the code (except inside a function unless declared global).
city = "Delhi"
def show():
print(city)
show()
To use or modify a global variable inside a function, you must declare it using the global
keyword.
count = 0
def update():
global count
count = 5
update()
print(count) # Output: 5
Variables in the outer function can be accessed in the inner function using the nonlocal
keyword.
def outer():
x = "outer"
def inner():
nonlocal x
x = "inner"
print("Inner:", x)
inner()
print("Outer:", x)
outer()
Python has some built-in names like len()
, range()
, print()
, etc. These are available globally.
print(len("scope")) # Output: 5
Python follows the LEGB rule to decide the order of scope resolution:
L - Local
E - Enclosing
G - Global
B - Built-in
Q1. Write a Python program to create a function with a local variable and print it inside the function.
Q2. Write a Python program to declare a global variable and access it inside a function using the variable name directly.
Q3. Write a Python program to modify a global variable inside a function using the global
keyword.
Q4. Write a Python program to try accessing a local variable outside its function and observe the error.
Q5. Write a Python program to create nested functions and use nonlocal
to modify a variable from the outer function.
Q6. Write a Python program to use the same variable name in global and local scope and print both.
Q7. Write a Python program to override a built-in name like sum
and observe the effect.
Q8. Write a Python program to build a calculator that updates a global result
variable after each operation.
Q9. Write a Python program to use the same global variable in two different functions and modify its value.
Q10. Write a Python program to create a counter using a nested function that updates a nonlocal
variable.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms