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

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 :: argparse print help if no arguments 
Python :: pyinstaller pymssql 
Python :: Your WhiteNoise configuration is incompatible with WhiteNoise v4.0 
Python :: anagrams string python 
Python :: Python how to use __lt__ 
Python :: map a list to another list python 
Python :: how to sort a list in python 
Python :: making ckeditor django responsive 
Python :: how to check if a variable holds a class reference in python 
Python :: df index drop duplicates 
Python :: edit models in django admin 
Python :: python how to drop columns from dataframe 
Python :: django email change sender name 
Python :: wap in python to check a number is odd or even 
Python :: pandas remove whitespace 
Python :: getting multiple of 5 python 
Python :: Math Module pow() Function in python 
Python :: merge keep left index 
Python :: obtain items in directory and subdirectories 
Python :: python gaussian filter 
Python :: python binary 
Python :: Adding new column to existing DataFrame in Pandas using concat method 
Python :: Python Split list into chunks using for loop 
Python :: structure ternaire python 
Python :: change column names pandas 
Python :: how to get more than one longest word in a list python 
Python :: phyton 2.7 convert timedelta to string 
Python :: cropping image google colab 
Python :: requests sessions 
Python :: codechef solution 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =