Declaration in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Declaration in Python

In Python why, global a = 10 generates syntax error while global a a=10 is correct ?

16th May 2019, 7:36 PM
harshit
harshit - avatar
3 Answers
+ 3
"global" isn't used to declare a global variable, but to use a global variable in a function. To declare a global variable, just declare it in the global scope (outside of all functions and classes) a = 5 # global variable def f(): a = 7 # local variable, doesn't affect global a def g(): global a a = 7 # changes global variable
17th May 2019, 5:59 AM
Anna
Anna - avatar
+ 2
They both throw a syntax error. EDIT: You changed the original question. It throws an error because its syntax doesn't allow to have variable assignment. https://docs.python.org/3/reference/simple_stmts.html#global
16th May 2019, 7:42 PM
Diego
Diego - avatar
+ 1
White spases are not allowed,Python is dynamically typed, means that you don't have to declare what type each variable is ,The variable is always assigned with the equal sign, followed by the value of the variable.There are some reserved word s for Python and can not be used as variable name. The correct is a = 10
16th May 2019, 7:58 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar