0
Q:

global var in python

a = 'this is a global variable'
def yourFunction(arg):
    #you need to declare a global variable, otherwise an error
    global a
    return a.split(' ')
    
5
#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]
5
global variable
variable = 'whatever'
10
global var1
var1 = 'whatever'
21
a = 0

def testFor():
  global a 
  if(a == 0):
    	#run code
3
global n
n = 'whatever'
7
x = 5

def foo():
    global x = 10
    print("local x:", x)


foo()
print("global x:", x)
0
a = input('Hello: ')
if a == 'world':
  global b
  b = input('Here')
print b
#Prints the input for be as a result
0

New to Communities?

Join the community