An example of conditional execution.

Save the bank.py version of this code. The Jupyter (*.ipynb) notebook will not be adequate in demonstrating this feature.

This program imports our other file financial.py. Due to the conditional execution written in that file using a simple condition main() function is not executed when the file is imported below, using import financial.

We use conditional execution in this file as good practice, even if we never intend on bank.py itself being imported by another program.

In [ ]:
import financial

def main():
    # Account balances for a variety of people
    accounts = [4545, 24, 3256, 223, 789]

    # 7% interest
    rate = 0.07

    #for account in accounts:
    print(accounts)
    
    for i in range(len(accounts)):
        new_bal = financial.add_interest(accounts[i], rate)
        accounts[i] = new_bal
        
    print(accounts)

if __name__ == '__main__':    
    main()