Lesson 3
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 3!"
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_3.txt".
Go to terminal and run
$ python lesson_3.txt
So far we've covered "variables", "functions", and "flow control". Today we're going to introduce "operators" as well as a new type of variable called a "list".
Operators
Operators are just like functions, in fact they ARE functions, but they look very different when you use them. Consider this code:
a = 1
b = 2
c = a + 3
All of the "=" and "+" in that code are operators. The "=" is a fairly special operator called the "assignment operator". There are many operators. Lets try the "less than" and "greater than" operators. They look like "<" and ">"
Try running this code:
x = 10
y = 12
z = x > y
print z
So the "greater than" operator checked to see if x was greater than y.
Now try running this code, but replace the question marks with the values to make it print out "What a radical dude with a radical 'tude!"
x = ?
y = ?
z = ?
print "What a ",
if x > 34:
print "stanky ",
else:
print "radical ",
print "dude with a ",
if y < 12:
print "radical "
else:
print "ugly "
w = x + y
if z > w:
print "'tude!"
else:
print "cat :("
When you finish, show Phil!
List
A list holds a sequence of variables in order. You create a list by using square brackets like "[" and "]". You also access the variables in a list using brackets.
Try running this bit of code:
x = ["Phil", "is", "the", "best", "teacher"]
print x
This should pretty much just print back what you typed in. The brackets create the list and assign the list to the variable x.
The neat thing about lists is that you can access just one element at a type. Write the code below into your editor, but before running it, try to guess what will be printed to the screen:
x = ["Phil", "is", "the", "best", "teacher"]
print x[0]
print x[1]
x[3] = "meanest"
print x
As you can see, once a list is assigned to a variable, you can access it's elements by using the bracket operator and saying which position in the list to access.
Now, go get Phil because you're ready for the hack challenge!