Lesson 4

Press <Cmd-Space>. This brings up your search bar.


Type in the word "terminal" to have it search for the Terminal Application.


Hit <return> to start Terminal.

Refresher

We learned several terminal commands two weeks ago. To run them you just have to type them into the terminal and hit <return>

ls -- list directory contents
cd -- change directory
mkdir -- make directory
top -- display information about the processor, RAM and other computer programs running

Two weeks ago we created a folder with your name on it. Use "cd" to change directories until you get there (hint: these were placed on the desktop so you'll have to start with "cd Desktop")

FYI, in these instructions if you're supposed to type something into Terminal, it'll generally be shown as

$ cd Desktop

You don't need to type in the "$ ", but just know that it means to type things into terminal. And don't forget to hit <return> when you're done!

Press <Cmd-Space> again to bring up your Mac spotlight search, and this time type in textedit.
Hit return to start the TextEdit application.

Just to get things rolling, in TextEdit type

print "starting lesson 4!"

Before saving, remember that TextEdit initially tries to save files in rtf (rtf stands for Rich Text Format). We don't want any of that funky jazz because it confuses python, so before saving convert your file to txt format. Go to "Format" in the menu bar and click "Make Plain Text"

Now hit <Cmd-s> to save the file. Save it in your folder with the name "lesson_4.txt".

Go to terminal and run

$ python lesson_4.txt

Scope

The "scope" in a piece of code defines what variables it can utilize and which lines of code belong to each other. In most all computer programming languages, "scope" is a very important term. Specifically, in they python programming language, scope is controlled by tabs and colons within functions. Take the following bit of code for example:

def some_function(num):
     x = num
     for i in range(0, 100):
          y = i * 2
          x = x + y
     return x

z = 10

The line at the end of "def some_function(num):" defines the start of a new scope. Everything that is in that scope is tabbed in. All the code below that is tabbed in is considered "within the scope" of the function. The "z = 10" is out of the function's scope. Since scope is used to group things together, and z is not within that group, it can't use the x and y variables.

For "for loops" and "if statements" Then the "for i in range(0, 100):" defines another type of scope (notice the colon and additional tab). It doesn't control access of variables, but it does define which pieces of code are executed. For instance, the "y = i * 2" and "x = x + y" lines of code are run over and over again in the for loop, while the "return x" line is only run after. That's because the "y = i * 2" and "x = x + y" lines are within the scope of that for loop and the "return x" line is outside of the for loop's scope. Lastly, scopes can be nested, so the lines "y = i * 2" and "x = x + y" are in the scope of the for loop and inside the scope of the function.

Scope can be a bit confusing, but it starts to make a lot more sense with a bit of practice. Try out these two examples and notice the difference.

def function_01():
     x = 0
     y = 0
     for i in range(1, 10):
          y = i * 2
          x = x + y
          print x

def function_02():
     x = 0
     y = 0
     for i in range(1, 10):
          y = i * 2
     x = x + y
     print x

print "running function_01"
function_01()

print "running function_02"
function_02()

What's the difference between these two functions? How come one printed out a bunch of numbers and the other only printed out one number?

Alright, now that you have that figured out, get Phil and tell him what the differences are. Can you tell him where all the variables are, where the functions are defined and where the functions are called. Where are the various scopes in this code?

Download the next hack challenge