0 votes
in Python by
Mention what are the rules for local and global variables in Python?

2 Answers

0 votes
by

Local variables: If a variable is assigned a new value anywhere within the function's body, it's assumed to be local.

Global variables: Those variables that are only referenced inside a function are implicitly global.

0 votes
by

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

Requiring global for assigned variables provides a bar against unintended side-effects.

Related questions

0 votes
asked May 17, 2020 in Python by SakshiSharma
0 votes
asked Jun 15, 2023 in Sql by Robin
...