C. Starr
0
Q:

Python scope

a = 5
b = 3

def f1():
  a = 2
  print(a)
  print(b)
  
print(a)   # Will print 5
f1()       # Will print 2 and 3
2
# Local Scope
# A variable created inside a function belongs to the local 
# scope of that function, and can only be used inside that function

#A variable created inside a function is available inside that function:
def myfunc():
  x = 300
  print(x)
myfunc()

# A variable created outside of a function is global and can be used by anyone:
x = 300
def myfunc():
  print(x)
myfunc()
print(x)
1
a = 5

def f1():
  a = 2
  print(a)
  
print(a)   # Will print 5
f1()       # Will print 2
1

New to Communities?

Join the community