“Lingkup Python” Kode Jawaban

Lingkup Python

# Variables have different levels of scope. Some include global and local scopes.

myVar = 5 # This is a global variable. It is not in any functions.

def myFunc():
  # global myVar
  
  myVar = 10 # This is a local variable

myFunc() # Runs myFunc
print(myVar) # Prints 5. The local variable in myFunc did not change the value of myVar.
# If you added 'global myVar' in myFunc, myVar would be changed to 10.
Ninja Penguin

Lingkup Python

def myfunc():
  x = 200
  print(x)

myfunc() #works
#print(x) does not work
#as a rule, you can use a variable more "outside" than you but you cannot use a variable more "inside". 
Javasper

Jawaban yang mirip dengan “Lingkup Python”

Pertanyaan yang mirip dengan “Lingkup Python”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya