a = 'this is a global variable' def yourFunction(arg): #you need to declare a global variable, otherwise an error global a return a.split(' ')
#it is best not to use global variables in a function #(pass it as an argument) a = 'This is global a' def yourFunction(): global a return a[0:2]
global variable variable = 'whatever'
global var1 var1 = 'whatever'
a = 0 def testFor(): global a if(a == 0): #run code
global n n = 'whatever'
x = 5 def foo(): global x = 10 print("local x:", x) foo() print("global x:", x)
global x x=1
a = input('Hello: ') if a == 'world': global b b = input('Here') print b #Prints the input for be as a result
# Python program showing no need to # use global keyword for accessing # a global value # global variable a = 15 b = 10 # function to perform addition def add(): c = a + b print(c) # calling a function add()