Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python edit global variable in function

globalvar = "flower"

def editglobalvar():
	global globalvar # accesses the "globalvar" variable, so when we change it
    # it won't assign the new value to a local variable,
    # not changing the value of the global variable
    
    globalvar = "good" # assigning new value
    
print(globalvar) # outputs "flower"
# if we didnt use the "global" keyword in the function, it would print out 
# "flower"
    
Comment

how to set global variable in python function

#A global variable can be accessed from the hole program.

global var = "Text"
Comment

Python Modifying Global Variable From Inside the Function

c = 1 # global variable
    
def add():
    c = c + 2 # increment c by 2
    print(c)

add()
Comment

PREVIOUS NEXT
Code Example
Python :: python plot groupby 
Python :: how to find in which directory my python code is running 
Python :: how to cerate a bar chart seaborn 
Python :: pandas average every n rows 
Python :: python subprocess exception handling 
Python :: check type of django messages 
Python :: python face recognition 
Python :: get the name of a current script in python 
Python :: how to know the python pip module version 
Python :: jinja2 template import html with as 
Python :: python create a pinging sound 
Python :: throw error in python 
Python :: webscrapping with python 
Python :: import django value 
Python :: python sort array by value 
Python :: tqdm range python 
Python :: midpoint 
Python :: convert np shape (a,) to (a,1) 
Python :: embed image in html from python 
Python :: how to concat on the basis of particular columns in pandas 
Python :: pygame get keypress code 
Python :: python 3.7 download for windows 7 32-bit 
Python :: Command errored out with exit status 1: 
Python :: python selenium click element 
Python :: getters and setters in python 
Python :: soap 1.2 request python 
Python :: python filter timestamp 
Python :: python loop back to start 
Python :: How to combine train and Test dataset in python 
Python :: discord bot slash 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =