Don't like this style? Click here to change it! blue.css

Daily Challenge 6:

I chose to have two types of dictionaries, "union" is the main object which produces offspring, and a "person" might contain their parents union under the key "ChildOfUnion". It is a very biological tree, so step-parents and half-siblings don't exist.

Class 7: Scope, Modules, Math

OK, so at this point we've played with variables, functions, lists, dictionaries, and loops. Now we'll talk about a concept that is more subtle, which is the lifespan and reach of your variables and functions. A variable's scope is all the parts of a program which have access to that variable.

Local Variables

Sometimes we need a variable inside of a function. This is then called a local variable:

Prediction Task 1: What will happen in the following snippet?

So how does Python decide which value of wife to use?

  1. First it checks the "local" namespace
  2. Next it checks any "enclosing" functions
  3. Next it checks the "global" space (the level of your code)
  4. Finally it checks the "Built-in" names

Global Namespace

The top level of your code is called the "global" namespace. You can look up variables from here if nothing closer has been found.

Prediction Task 3: What happens in the following snippet?

Attempt Task 4: Try to change the global variable wife inside of fantasy land and have the change persist. What goes wrong?

To have a change from inside of a function actually head back into the global namespace you can tell Python that you want to using: global variablename inside of your function.

Learn by Doing 5: Add the global declaration to the fantasy_land function so that you can change wife to "Angela Merkel" (if you don't know who that is, you should go look her up). Prove to yourself that the change lasted.

What about inputs to functions?

Prediction 6: Predict the outcome of the following snippet, then run to check.

Enclosing functions

If you've got a function within a function things are kinda messy:

Explain Task 7: Turn to your neighbor, and explain why the output to that snippet worked that way.

Modules

So we've been doing things like import random since day 1. Now we have the vocabulary. This imports a module called "random" which is its own namespace.

One of the modules that you need to really know is the math module.

Explore Task 8: Use one of our tricks to see the methods inside of the math module. Pick one that sounds familiar and read the documentation.

Here is a quick usage for growing an account using continuous interest:

Adaptation Task 9: Adapt that snippet to give you the amount of cash that $100,000 would grow to at a return rate of .08 for 25 years.

Modeling Task 10: Use this idea to decide how much cash you should add to your retirement account every year if your want to retire with $2million in 10 years. Suppose you have nothing now and get a rate of return of .15. Try 20 years. Try a rate of .05 (the "risk-free" rate).

Quick aside: composition

What happens here:

Math Task The golden ratio satisfies \( \phi = 1 + \frac{1}{\phi} \). Write a function golden(x) which takes a reciprocal and adds one. Compose golden with itself 20 times starting at a value of 1. How close did you get to the real value of \(\phi\)?

Optional: Turtles

If there is enough time, then try to make a christmas tree using turtles. Here is a start:

Daily Challenge 7:

Assume an inflation rate of .03 per year. Write a function which takes in your average rate of return, current cash value, your annual cost of living, and the number of years until retirement. Calculate the amount you should contribute every year for the given number of years to live off of your interest at an inflation adjusted cost of living once that number of years is passed. You've just created an endowment!