1. Local Variables
Local variable wo hota hai jo sirf function ke andar accessible hota hai.
def greet():
message = "Hello from function!" # local variable
print(message)
greet()
# print(message) # Error: message is not defined outside function
2. Global Variables
Global variable program ke kisi bhi part me accessible hota hai.
message = "Hello Global!" # global variable
def greet():
print(message)
greet()
print(message)
Output:
Hello Global!
Hello Global!
3. Modifying Global Variables
Function ke andar global variable modify karne ke liye global keyword use hota hai.
count = 0 # global variable
def increment():
global count
count += 1
print("Inside function:", count)
increment()
print("Outside function:", count)
Output:
Inside function: 1
Outside function: 1
4. Local vs Global Example
x = 10 # global
def test():
x = 5 # local
print("Inside function:", x)
test()
print("Outside function:", x)
Output:
Inside function: 5
Outside function: 10
Next Step
Ab hum seekhenge Classes & Objects, jisme Python me Object-Oriented Programming ka introduction milega.