Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python scope

# 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.
Comment

python variable scope

a = 5

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

how does scope work in python

a = 5
b = 3

def f1():
  a = 2
  print(a)
  print(b)
  
print(a)   # Will print 5
f1()       # Will print 2 and 3
Comment

Python Scope

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". 
Comment

PREVIOUS NEXT
Code Example
Python :: python shuffle 
Python :: Get current cursor type and color Tkinter Python 
Python :: python create dictionary from csv 
Python :: how to know if the space button has been clicked in python pygame 
Python :: remove white border matplotlib 
Python :: semicolon in python 
Python :: xpath start-with python 
Python :: -- python 
Python :: get local ipv4 
Python :: how to put python code on a website 
Python :: what is a framework 
Python :: django order by foreign key count 
Python :: find all occurrences of an element in a list python 
Python :: print whole list python 
Python :: or statement python 
Python :: how to clear dictionary in python 
Python :: python disable logging on unittest 
Python :: python enum advanced 
Python :: make widget span window width tkinter 
Python :: python isinstance 
Python :: Convert column as array to column as string before saving to csv 
Python :: Python Tkinter Button Widget 
Python :: get file size python 
Python :: numpy.random.choice 
Python :: httplib python 
Python :: scroll to element selenium python 
Python :: Bar Charts bokeh 
Python :: streamlit headings;streamlit text 
Python :: remove brases from array py 
Python :: django convert object to dict 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =