Important Notice:

Scope & Modules

Scope & Modules

18 views 1 min read

Variable Scope

  • Local: Inside function
  • Global: Outside all functions
  • Enclosing: In nested functions
  • Built-in: Python built-in names

x = 10 # Global\ndef func():\n y = 20 # Local\n print(x, y)\n\ndef increment():\n global count\n count += 1Modules

A module is a .py file with Python code.

import math\nprint(math.pi) # 3.14159\nprint(math.sqrt(16)) # 4.0\n\nimport random\nprint(random.randint(1, 10))Common ModulesModuleUsemathMath functionsrandomRandom numbersosOS operationsdatetimeDate/timesysSystem