← Python Programming
Collections and functions · 15 MIN

Scope, modules and imports

Scope, modules and imports

A variable created inside a function is local to that function unless declared otherwise. Modules are Python files that can be imported. Keep executable startup behavior inside a main function and use the __name__ guard.

Try it yourself

Change the example, run it, and explain the result in your own words. Then create one variation of your own.

Key idea

Use the example as a starting point, not something to memorize. Practise, inspect the result and make small improvements.

Code example

import math

def main():
    print(math.sqrt(81))

if __name__ == "__main__":
    main()