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 218 views
1 answer 175 views
2 answers 120 views
120 views asked Jun 9, 2023 by avibootz
1 answer 157 views
1 answer 170 views
170 views asked Sep 29, 2020 by avibootz
3 answers 323 views
...