How to declare and use global variables in Python

1 Answer

0 votes
s = "python 2"

def update_s_local():
  s = "update_s_local() python 3"
  
def update_s_global():
  global s
  s = "update_s_global() python 3"


update_s_local()
print(s)

update_s_global()
print(s)

  
  
  
  
  
'''
run:
  
python 2
update_s_global() python 3
  
'''

 



answered Apr 15, 2021 by avibootz
edited Apr 15, 2021 by avibootz

Related questions

1 answer 221 views
1 answer 182 views
2 answers 126 views
126 views asked Jun 9, 2023 by avibootz
1 answer 160 views
1 answer 177 views
177 views asked Sep 29, 2020 by avibootz
3 answers 333 views
...