Python basics: functions and scope - klinke.studio
browse sections
Browse Notes
audio
computers
design
electrical-engineering
literature
math
ux-design

Python basics: functions and scope

Python basics: functions and scope

Functions help structure logic and localize variables. Variables exist inside their defining scope unless returned or declared global.

import math

global_variable = 1

def say_hello(name=None):
    if name:
        print(f"Hello, {name}!")
    else:
        print("Hello, World!")

def main():
    num_students = 10
    for i in range(num_students):
        say_hello(f"Student {i}")

    say_hello()

if __name__ == "__main__":
    main()