I believe in pomodoros. This class happens to be exactly 3 pomodoros. Let's use 1 to talk about the overview of the course, and set our expectations.
Computers are dumb. Sorry. Making them think creatively is hard. BUT they are extrodinary at doing oodles of simple things extremely fast. SO your goal is to convert some interesting problem into something which a computer can do.
A programming language is a compromise between us and the computer. The programming langueg reads a little bit like English, but the commands are structured and the grammar is specific. That way a computer can try its best to make sense of our request.
Our goal is to bridge gap from that (machine code) to this (humans operating the machine):
In the lab yesterday you learned how to access Python3 in several different ways. So go to cloud9 and start up a
workspace. Find the bash tab (call this the terminal) and type ipython3
.
iPython is yet another flavor of python which has some great features for a learning student.
Mini-Task 1:
mystring = "Andy is sooo cool."
mystring.
ti
then TAB?
(you should see mystring.title?
) then hit ENTER?
with ()
(you should see mystring.title()
) hit ENTERSo what did you learn? Well, a few things:
"Andy is sooo cool."
) into
a variable (mystring
).What I hope you take from this task: the ability to explore what might be possible. When you get home tonight try to learn a new method with this trick.
SYNTAX LESSON: a single equals sign (=
) is called the assignment
operator. When the program executes a command like: variable = expression
it will FIRST
try to evaluate/execute the expression (the right hand side) and then SAVE that value into the variable.
Mini-Task 2: store the number 23 into a variable named numBananas
Mini-Task 3: print the value to the screen with the command print(numBananas)
Mini-Task 4: Pick the coolest sounding method that can be executed on numBananas
my
doing the tab completion trick from above.
In order to cement our understanding of assignment and variables let's look at a ton of examples. First, be aware that we can do plain old math in python.
Try to evaluate the following math expressions using Python:
1 + 2 + 3 + 4 + 5
1*2*3*4*5
5**3
(BONUS: what does the **
operator do?)Mini-Task 5: What do you think are the variables in the following sequence of commands? What is the value of each variable after all is said and done? Run these lines in Python and print the results to confirm.
Questions?
Mini-Task 6: What is the value of x
at the end of the following two lines?:
x = 10
x = 10 + 3
In this case we have assigned a value to x
twice! "variables" must "vary", know what I'm saying?
Mini-Task 7: CHALLENGE: What is the value of x
at the end of the following two lines?:
x = 10
x = x + 3
So remember that the assignment operator evaluates the right hand side first. So after line 1 we have
x
equal to 10. After line 2 we have x
equal to 10 + 3
.
Mini-Task 8: CHALLENGE: What is the value of x
at the end of the following three lines?:
x = 10
x = x + 3
x = x * 3
LIFE HACK: Things make more sense when you have a goal. This is because you can imagine yourself at the end of your project and walk backwards. This will help you see where an idea fits.
So let's give you a goal: Generate a random number from 1 to 6 (roll a die).
You currently don't have any idea how to do this.
Our earlier trick of hitting tab (in iPython) won't work because you don't really know where to start.
USE GOOGLE!
Mini-Task 9: Google search a way to generate a random number from 1-6. Then do it. Then reflect on the steps. (You will find that stackoverflow usually has the most useful code snippets.)
1 help(what_you_care_about_here)
. Even in regular python you can call
help(something)
to see some helpful info. Try this with help(list)
.
2 print(your_thing_here.__doc__)
. This is a weird one, but you might want it
someday. Try the following two lines:
3 The OFFICIAL PYTHON DOCUMENTATION! They have tutorials, offical references, examples, FAQs, install guides, HOWTOs for specific topics, and navigable indices. Good stuff there.
4 Our book (or other books). It has a detailed index, elaborate examples, practice exercises, and is made for beginners.
Mini-Task Z: save the string "I am Zorg, ruler of planet Fun-Fun."
into a variable.
Use a method to make the string all lowercase. Print it to the screen. Then make the string all uppercase. Print
it to the screen.
On Monday when you come in to class I will give you a TOKEN and you will email in:
If I haven't timed this correctly then we can continue by exploring %
, //
,
/
, len
, type
, abs
, max
, etc.