code logs -> 2007 -> Sat, 08 Dec 2007< code.20071207.log - code.20071209.log >
--- Log opened Sat Dec 08 00:00:38 2007
00:08 You're now known as TheWatcher[T-2]
00:13 You're now known as TheWatcher[zZzZ]
02:03 ilovefire [~santros_v@209.82.191.ns-11321] has joined #code
02:04
< Vornicus>
Okay. Step 1: download and install Python.
02:04
< ilovefire>
I've got Python 2.5 allready.
02:04 C_tiger_laptop [~c_wyz@96.232.24.ns-12742] has joined #code
02:04
< Vornicus>
Okay, it's already installed?
02:04
< ilovefire>
Yes.
02:04
< Vornicus>
Okay, set yourself up a directory, call it, say, python-stuff or something.
02:06
< ilovefire>
Okay, at the risk of sounding exceedingly stupid (because I've never had to do this before), how do I do that/ Or is it just, err, a folder or?
02:06
< Vornicus>
folder, directory, same thing
02:06
< ilovefire>
Okay.
02:06
<@ToxicFrog>
Directory is the right name~
02:06
< Vornicus>
But most APIs call them directories.
02:06
< Vornicus>
So, directory.
02:07
< ilovefire>
Okays.
02:07
< ilovefire>
Done.
02:07
< Vornicus>
Now, your Python install came with an editor/IDEoid, named IDLE. Open that.
02:08
< ilovefire>
Done.
02:08
< Vornicus>
Okay, now there should be a File menu, should be a thingthat says "New" in there. use it.
02:08
< ilovefire>
New Window?
02:08
< Vornicus>
I think that's it.
02:09
< Vornicus>
If it doesn't give you a blank window, it's wrong.
02:09
< ilovefire>
It gives me a blank window.
02:09
< Vornicus>
Okay.
02:09
< Vornicus>
The traditional first program in any language is, generally, Hello World.
02:09
< Vornicus>
Wherein you print "Hello, World!" or something similarly silly.
02:09
< ilovefire>
Isn't that just, err... I think I read it in an XKCD comic.
02:10
< ilovefire>
I remember it being very simple.
02:10
< Vornicus>
I'm not going to tell you how to do this. You're going to have to guess. :)
02:10
< C_tiger_laptop>
Somewhere, there's a very funny probably-not-true explanation for that.
02:10
< Vornicus>
hint: yeah, it was in an xkcd comic. A recent one, too.
02:11
<@ToxicFrog>
I prefer "goodbye, cruel world!"
02:11
<@ToxicFrog>
As typically the program exits immediately thereafter.
02:11
< Vornicus>
I prefer to get it to spit out the first thousand primes, but i'm crazy.
02:11
<@ToxicFrog>
Well, you aren't a noob.
02:12
< Vornicus>
And I /am/ a math nerd. So.
02:12
< C_tiger_laptop>
wait, is there a good algorithmic way to calculate primes?
02:12
< Vornicus>
So, tell me when you think you've figured it out.
02:13
< C_tiger_laptop>
I thought the easiest way is to make a list and cross out every third, every fifth, etc...
02:13
< ilovefire>
Print "Hello, World!"?
02:13
< Vornicus>
ilf: close.
02:13
<@ToxicFrog>
C_tiger_laptop: that's the Sieve of Erastosthenes
02:13
< ilovefire>
Hmm.
02:14
< C_tiger_laptop>
But is there one that doesn't use an array and prints them out in order?
02:14
<@ToxicFrog>
And it does generate them in order, but it's memory-heavy
02:14
< Vornicus>
Save that in your directory with the name hello_world.py, and then hunt down the "run" command in one of the menus.
02:14
< Vornicus>
C: usually I use trial division.
02:14
< C_tiger_laptop>
Ah.
02:15
< Vornicus>
well, seiving.
02:15
<@ToxicFrog>
Well, relatively speaking; you can do it in one bit per cell, so the first 8K primes require 1K memory.
02:15
< Vornicus>
ilf: this should give you a, uh, Syntax Error.
02:15
< Vornicus>
this is a sign that you've done something wrong.
02:16
< ilovefire>
Okay.
02:16
< Vornicus>
Now, here's the key thing here: Python, like most languages, is case-sensitive; the capitalization of things is important. Keywords in Python are all lower case. 'print' is a keyword.
02:16
< ilovefire>
ah.
02:16
< ilovefire>
so it's print "Hello, World!"
02:16
< Vornicus>
Yes.
02:17
< C_tiger_laptop>
Yeah, I'll just stick to doing a Fibonocci sequence for my helloworld. I don't usually know how to manage data structures when I start.
02:18
< Vornicus>
Change that, save it, run it, see if it works.
02:18
< C_tiger_laptop>
This brings us to the very first thing you learn in programming: Trial and error is your best friend.
02:19
< C_tiger_laptop>
And there is a lot of error. And there is nothing wrong with that.
02:19
< ilovefire>
Vorn: Well, um, it opens a dos thing for about half a second before ti closes.
02:19
<@McMartin>
In IDLE on Windows, F5 is "reset other window and run program there"
02:19
< Vornicus>
heh. Are you using the run command in IDLE, or opening it using Explorer?
02:19
< Vornicus>
You should be using the former.
02:20
< Vornicus>
But yeah, use f5. It's more convenient.
02:20
<@ToxicFrog>
...print in python is a keyword? o.O
02:21
< Vornicus>
TF: yeah, it's rather a wart. I believe Py3k is removing it.
02:21
<@McMartin>
If it weren't, you'd have to put the string in parens.
02:21
< Vornicus>
(and replacing it with a proper function)
02:21
< ilovefire>
okay, now it works. Prints out Hello, World! in nice blue letters.
02:21
< Vornicus>
(of the same name)
02:21
< Vornicus>
Okay, you're doing pretty good.
02:21
< C_tiger_laptop>
Woot!
02:22
< ilovefire>
If only because I have some prior experiance with doing this, and an xkcd comic to help...
02:24
< ilovefire>
So, what's next?
02:24
< Vornicus>
Okay, let's see. Next, I guess, should be arithmetic. Which is mostly pretty obvious, but can have some really scary bits in.
02:24
<@ToxicFrog>
Write an air traffic control system ??
02:24
< Vornicus>
Go over to the interactive prompt - the >>>
02:25
< ilovefire>
... Err?
02:25
< Vornicus>
Got a window with a thing that goes >>> in it?
02:25
< ilovefire>
Ah.
02:25
< ilovefire>
Done.
02:25
< Vornicus>
Go over there, we're going to type in that for a while, because we're going to be doing some 1-line stuff.
02:26
< ilovefire>
Allright.
02:27
< Vornicus>
Try some things out - addition, subtraction, negative numbers, multiplication. Avoid division for now, because it does odd things.
02:27
< C_tiger_laptop>
Or, try division.
02:27
< C_tiger_laptop>
Just to see what it does.
02:28
< ilovefire>
...
02:28
< C_tiger_laptop>
Just don't freak when it does odd things.
02:28
< ilovefire>
Huh, powers cause odd things to.
02:28
< Vornicus>
That's because it's not actually powers.
02:28
< Vornicus>
Using ^?
02:28
< C_tiger_laptop>
^ isn't powers.
02:28
< ilovefire>
Ah.
02:29
<@McMartin>
(l33t x0rxx0r1n6 sk1llz!)
02:29
< C_tiger_laptop>
McM, I can't actually read that.
02:29 * ilovefire can... is that a bad sign?
02:30
<@ToxicFrog>
And yeah, ^ is power in some languages, bitwise xor in others, logical xor in still others
02:30
< ilovefire>
... Wow, this sounds complex.
02:30
<@McMartin>
You're unlikely to need bit operations for a long time.
02:30
<@ToxicFrog>
So leave it alone for now.
02:30
< Vornicus>
The ones I want you to know right now are + - * / . Note that / does flooring division when used entirely on integers; if the result would be between two integers, the answer is the lower of those two.
02:30 * ilovefire nods
02:31
<@McMartin>
% is also worth knowing, which is remainder.
02:31
< C_tiger_laptop>
But also try doing math (especially division) with decimals.
02:31
< Vornicus>
(-3/5 is -1; -3/-5 is 0; 3/-5 is -1)
02:31
<@ToxicFrog>
Basically, integer division rounds down
02:31 * ilovefire nods.
02:32
< C_tiger_laptop>
But -3/5.0 = -0.6
02:33
< Vornicus>
Okay, now that you've familiarized yourself with those four.
02:33
<@ToxicFrog>
Because that's not integer division.
02:33
< C_tiger_laptop>
Right.
02:33
< Vornicus>
Exponentiation is **
02:33
< C_tiger_laptop>
(what you would call powers)
02:33
< ilovefire>
allright...
02:34
< ilovefire>
(Because I'm mathematically, err, below the curve.)
02:34
< Vornicus>
This comes from Fortran, originally, and it's used because ^ is already used for something else.
02:34 * ilovefire nods
02:34
< Vornicus>
And, finally, %.
02:35
< Vornicus>
% is the remainder operator; it's the difference between a and (a/b)*b
02:36
< ilovefire>
... This may need explaining to me, since, well, I haven't used reaminders in... four to five years, because my teachers since then have basically all... err, sucked.
02:37
< Vornicus>
Try 8/5
02:37
< Vornicus>
(you should get 1)
02:38
< ilovefire>
allright.
02:38
< ilovefire>
yeah, got 1
02:38
< Vornicus>
Now try (8/5)*5
02:39
< Vornicus>
Which looks like it should be 8, but won't be.
02:40
< Vornicus>
What's the result for that?
02:40
< ilovefire>
5.
02:40
< Vornicus>
Okay, now try 8 % 5
02:41
< ilovefire>
I get 3.
02:41
< Vornicus>
Okay, and what's the difference between 8 and (8/5)*5?
02:42
<@ToxicFrog>
To look at it another way: 8/5 is how many times 5 goes evenly into 8 (ie, 1). 8%5 is how much you have left over afterwards (3).
02:42
< ilovefire>
Mathematically, umm, well, you have to do whats in parenthesis first, and five dosen't go into eight evenly so...
02:43
< C_tiger_laptop>
Don't think about this when you deal with negative numbers because your brain will not like it.
02:43
< Vornicus>
ilf: yep.
02:43
<@ToxicFrog>
ilovefire: mathematically, 8/5*5 == (8*5)/5 == 8*(5/5) == 8
02:44
<@ToxicFrog>
Simple properties of multiplication and division
02:44
< Vornicus>
I think we've done enough math for a while, though, except to say this: divmod(a, b) gives you a/b /and/ a%b at once.
02:44
< ilovefire>
C: My brain allready dosen't like it. I'm a terrible person at math.
02:44
< ilovefire>
But I'm not letting that stop me here. I hope.
02:45
< Vornicus>
Fortunately there's more about decision making than about arithmetic in programming.
02:45
< C_tiger_laptop>
No matter, we press on! What you have just learned: python does math. With parentheses. Yes division may be a little funny, but that's OK.
02:45 gnolam [lenin@Nightstar-10613.8.5.253.static.se.wasadata.net] has quit [Quit: Z?]
02:46
< Vornicus>
Next thing we're going to do, is make ourselves a function.
02:46
< ilovefire>
allright...
02:47
< Vornicus>
...actually no, we'll do variables first, because that's kinda necessary.
02:48
< Vornicus>
You can assign anything you can make to a variable. Like this: a = 8
02:48
< C_tiger_laptop>
Except don't do it with keywords.
02:48
< Vornicus>
though, variables can have really long names: this_is_the_variable_name_that_never_ends_it_just_goes_on_and_on_my_friend
02:49
< ilovefire>
allright.
02:49
< Vornicus>
You can use capital letters, lowercase letters, numbers, and underscores in names.
02:49
< ilovefire>
Okay.
02:49
< Vornicus>
You can't start a name with a digit.
02:49
< Vornicus>
And you can't step on keywords with names - you can't name something print.
02:50
< C_tiger_laptop>
While it may seem efficient to always choose a short variable name (i, a, x, y) don't. It's easy to forget what a is. If you call it numberOfApples, then when you come back to use the number, you'll know what it means.
02:50
< ilovefire>
Right.
02:51
< Vornicus>
So, assign some numbers to variables, and try adding them. try assigning the results of expressions to variables. It all works just fine.
02:53
< ilovefire>
allright, so say foo = 9, bar = 6. I can then, say, go foobar = foo + bar and type in foobar, and it prints 15.
02:53
< ilovefire>
I thikn I'm getting it.
02:53
< Vornicus>
yep.
02:54
< Vornicus>
That's all you need to know, really. You can assign to variables you've already assigned to - try foobar = foo * bar, and then foobar again.
02:54
< ilovefire>
that gives me 54.
02:55
< Vornicus>
As it should.
02:55
<@ToxicFrog>
Hence the name: "variable", a name for a value that can vary
02:55
< C_tiger_laptop>
Now try foobar = "Hey I can be a string too!"
02:55
<@ToxicFrog>
We haven't done strings yet
02:56
< C_tiger_laptop>
Oh.
02:56
< C_tiger_laptop>
Ok, ignore that.
02:56
<@ToxicFrog>
(well, except implicitly, in hello world)
02:56
< Vornicus>
well you can do it anyway, but.
02:56
< C_tiger_laptop>
I thought we covered strings in Hello World but I am wrong.
02:57
< Vornicus>
Now that we've covered variables, we can talk about functions properly.
02:58
< Vornicus>
So far everything we've actually done is /used/ things that do stuff, we haven't /made/ anything that does stuff.
02:58
< ilovefire>
Okay...
02:58
< Vornicus>
So let's make the simplest possible function I can think of: add.
02:59
< Vornicus>
We need a new window to do this; technically you can do it in the interactive window, but that can be a pain.
02:59
< ilovefire>
allright.
02:59
< ilovefire>
new window opened.
02:59
< Vornicus>
Okay, first line: def add(a,b):
03:00
< Vornicus>
We just said "we're DEFining a function named 'add', and it takes parameters a and b"
03:00
< C_tiger_laptop>
Parameters is a fancy word for input.
03:00
< ilovefire>
allriht.
03:01
< Vornicus>
okay, now on the second line, put /four spaces/, and then return a + b
03:02
< ilovefire>
umm... even if it's allready indented? Okay.
03:02
< Vornicus>
well, if it's already indented, don't bother.
03:02
< ilovefire>
okay.
03:02 * Vornicus didn't think it would do that for you, but evidently it does!
03:02
< ilovefire>
okay.
03:02
< C_tiger_laptop>
Usually in properly configured editors, you can also insert four spaces with the tab button.
03:03
< Vornicus>
Save that in your directory, call it "add.py"
03:03
< ilovefire>
okay.
03:03
< ilovefire>
done.
03:03
< Vornicus>
press f5, then go back over to the interactive window.
03:04
< ilovefire>
okay...
03:04
< Vornicus>
type add(6,9)
03:05
< ilovefire>
Gets me fifteen. So I just made a function that, given two variables for imput, adds them together, yes?
03:05
< Vornicus>
yep.
03:05
< Vornicus>
Now, you can assign the result of a function to a variable.
03:06
< ilovefire>
okay...
03:06
< Vornicus>
you can also use variables as inputs (arguments) to a function, and you can even use the results of functions as arguments to a function: add(foo,add(3,4))
03:06
< ilovefire>
allright.
03:07
< C_tiger_laptop>
Provided that you have foo defined earlier.
03:07
< Vornicus>
Note, however, that variables that work /in/ the function don't work /out of/ the function - try saying just a right now.
03:08
< C_tiger_laptop>
Which means we should backtrack one second, here.
03:08
< Vornicus>
?
03:09
< C_tiger_laptop>
When you run the program, what happens is that python looks at where you typed your def: statement and compares that to what you typed in your call statement.
03:09
< C_tiger_laptop>
So the call is when you type add(9,6). The declaration is add(a,b)
03:10
< Vornicus>
ah, yes.
03:10
< C_tiger_laptop>
so a = 9 and b = 6
03:10
< ilovefire>
right...
03:10
< C_tiger_laptop>
and then it goes to the next line, adds the two and spits out the answer, clearing out a and b.
03:10
< Vornicus>
But these are /local/ variables - they don't exist outside the context of that function.
03:10
< ilovefire>
Right.
03:13 Vornicus is now known as Darius
03:13
< Darius>
Now, one other thing about functions, before we go any further.
03:14
< Darius>
The result of a function is the result of the stuff in the return statement - that is to say, the result of the line that starts with 'return'.
03:14
< ilovefire>
okay...
03:14
< Darius>
everything after a return statement is 'dead code', it never gets run.
03:14
< Darius>
return, like def and print, is a keyword; you can't use it for anything else.
03:15
< ilovefire>
okay.
03:15
< C_tiger_laptop>
This brings us to tip #2: Reading code. Everyone has a special way of reading code that is taught to them and it helps us think through what's going on in the code.
03:16
< C_tiger_laptop>
Going back to your add.py:
03:17
< ilovefire>
allright.
03:17
< C_tiger_laptop>
def: add(a,b) "Define a new function called add that takes parameters a and b"
03:17
< ilovefire>
I read it as 'define a function called add that uses variables a and b.'
03:17
< ilovefire>
should I change anything there or does that work?
03:17
< C_tiger_laptop>
return a + b "go back to the place where the function is called, carrying the result of a+b."
03:17
< C_tiger_laptop>
That works great.
03:18
< ilovefire>
and I read return a + b as 'cause the above function to add the imputted variables'. but then, that's because that's what I know it does, and have no idea of the underlying theory or anything, so...
03:19
< C_tiger_laptop>
It's better to read one line at a time.
03:19
< C_tiger_laptop>
don't reference previous lines or following lines.
03:19 * ilovefire nods. "Will do, C."
03:19
<@ToxicFrog>
ilovefire: "uses variables a and b" is misleading, as if you have variables with those names outside the function, it won't use those
03:20
<@ToxicFrog>
It uses its own internal versions, which start with whatever values were passed into the function when it was called.
03:20
< C_tiger_laptop>
for example. If you had a third line that was a*b, you'd read it as "multiply a by b".
03:20
< ilovefire>
Blargh. I'll have to do a minor paradigm shift I suppose.
03:20
<@ToxicFrog>
> a = 1
03:20
<@ToxicFrog>
> def add(a,b):
03:20
<@ToxicFrog>
>> return a+b
03:20
<@ToxicFrog>
> a
03:20
<@ToxicFrog>
=> 1
03:20
<@ToxicFrog>
> add(4,5)
03:20
<@ToxicFrog>
=> 9
03:21
<@ToxicFrog>
> a
03:21
<@ToxicFrog>
=> 1
03:21
< Darius>
(there are ways around this. I won't talk about them for a long time.)
03:21
< C_tiger_laptop>
Right, this was Vorn's earlier point about variables being locked up inside functions.
03:22
<@ToxicFrog>
Also, a function can use variables that are not parameters
03:22
<@ToxicFrog>
Eg, you can have a function that "takes no parameters, but uses variables foo and bar"
03:22
< Darius>
I won't talk about that either, yet.
03:22
< C_tiger_laptop>
Anyhow, my point is if you had a third line in add.py that was, say, a*b when you read through, you'd realize that it never hits that line because line 2 tells you to go back.
03:22
< ilovefire>
Okay...
03:23
< C_tiger_laptop>
Try it.
03:23
< Darius>
heck, put "print a*b" instead of just "a*b"
03:23
< C_tiger_laptop>
(TF, Vorn, sorry for interjecting, but reading code comes naturally to us, but I remember it being one of the hardest things to figure out when I started.)
03:24
< C_tiger_laptop>
Erm, maybe not hardest but one of the most "AHA!"
03:24
< Darius>
(is okay)
03:24
<@ToxicFrog>
(I don't recall ever being taught how to read code; it came as a natural consequence of learning the language)
03:24
< C_tiger_laptop>
I wasn't taught to read it perse, but I was taught to follow it line by line.
03:25
< ilovefire>
C: gives me an error that a is not defined.
03:25
< Darius>
ilf: is the line indented along with the return statement?
03:25
< Darius>
If not, make it so.
03:27
< C_tiger_laptop>
So you should have: def: add(a,b) >> return a+b >> print a*b
03:27
< Darius>
(the IDE is smart enough to know that anything after a return statement in a function is dead code, so will unindent after one)
03:27
< C_tiger_laptop>
where >> is an indented new line.
03:27
< ilovefire>
allright.
03:28
< C_tiger_laptop>
Now save, f5, go back to the >>> interface and run add(9,6)
03:28
< ilovefire>
now it just does the add, no printing of a*b
03:28
< C_tiger_laptop>
Right.
03:28
< C_tiger_laptop>
Now read the code.
03:30
< C_tiger_laptop>
Define the function add which takes two numbers in and assigns them to variables a and b. Go back to the calling function carrying a+b. Print a*b.
03:30
< ilovefire>
Line 1: "Define a function called add that uses parameters a and b." Line 2: "go back to the place where the function was called, bringing the result of a+b." Line 3: "show ont he screen a*b."
03:30
< C_tiger_laptop>
Right.
03:30
< C_tiger_laptop>
And you see if you follow that line by line, you'll never get to line 3.
03:30
< C_tiger_laptop>
Because line 2 tells you to hightail it back.
03:31 * ilovefire nods
03:31
< C_tiger_laptop>
Now what do you think will happen if you switch the two lines?
03:31
< ilovefire>
Umm, not entirely sure. Let's try it and find out?
03:32
< C_tiger_laptop>
so swap lines 2 and 3.
03:32
< C_tiger_laptop>
Well, try to anticipate what will happen before you run it.
03:32
< C_tiger_laptop>
Read the code. It'll tell you.
03:32 * ilovefire nods.
03:33
< ilovefire>
This time, it prints out what you get when you multiply a and b, then what you get when you add.
03:33
< C_tiger_laptop>
Ok, give it a shot.
03:33 * ilovefire gives it a shot, runs it--yeap, that's what happens.
03:33
< C_tiger_laptop>
Sweet!
03:34
< C_tiger_laptop>
Ok, back to your regularly scheduled lesson plan.
03:34
< Darius>
Okay.
03:36
< Darius>
Now, obviously if all you can do in functions is math, that's kinda silly. It doesn't get you anywhere.
03:36 * ilovefire nods...
03:36
<@ToxicFrog>
Well, it's useful for using python as a desk calculator.
03:37
< C_tiger_laptop>
(although you have also learned that you can print to the screen in functions too._
03:37
< Darius>
So what we need... is some decision-making abilities.
03:37
<@ToxicFrog>
Dun dun duuuuun
03:37
< C_tiger_laptop>
dun dun duuuuuuuuun!
03:37 * ilovefire powerchord
03:38
< C_tiger_laptop>
This is what makes programming so powerful.
03:38
< Darius>
Go back to add.py, put a blank line, then make a function called "isitzero" that takes one parameter, call it, uh, hell, Idunno, it.
03:38
<@ToxicFrog>
One of the things.
03:38
<@ToxicFrog>
...although, I suppose repetition is just an application of decision making,
03:38
< C_tiger_laptop>
I mean flow control.
03:39
< ilovefire>
so, umm, def isitzero (a):?
03:39
< Darius>
Yep.
03:40
< Darius>
now put, in that next line: if a == 0:
03:40
< ilovefire>
okay.
03:40
< Darius>
and then in the line after that, put return "Yes, it's zero!"
03:40
< ilovefire>
okay.
03:41
< Darius>
Okay, save that, head back to the interactive prompt.
03:41
< ilovefire>
okay.
03:42
< Darius>
now try isitzero(0), and isitzero(5)
03:43
< ilovefire>
The first gets a print of Yes, it's zero! and the second gets nothing.
03:43
< Darius>
try print isitzero(5)
03:43
< ilovefire>
gets me None
03:44
< Darius>
So, that one there, it skipped over the line with the return in it, because it wasn't 0.
03:45 * ilovefire nods
03:45
< Darius>
We can even put stuff after, too.
03:45
< ilovefire>
okay...
03:46
< Darius>
after the return we showed already, make it so you're indented the same amount as the if, and then put return "no, it's not zero!"
03:47
< ilovefire>
and then running isitzero(5) nets me a "No, it's not zero!"
03:47
< C_tiger_laptop>
Now read the code, and see if you can figure out why it's doing what it's doing from the code.
03:48
< ilovefire>
Because it dosen't do the first return unless the if statement is true, because it's tree'd off of the if statement?
03:49
< Darius>
yep.
03:49
< C_tiger_laptop>
Right, so here's how to read it: if a is equal to 0, do the bit in the indent. Otherwise skip ahead.
03:49 * ilovefire nods
03:50
< C_tiger_laptop>
remember "is equal to" is ==
03:50
< C_tiger_laptop>
not just one =
03:50 * ilovefire nods
03:50
< Darius>
now, the thing about this is, you don't have to return from within an if statement, and if you don't, you don't necessarily want to do absolutely everything after it.
03:51
< C_tiger_laptop>
Now may be a good time to introduce the classic division statement.
03:53
< C_tiger_laptop>
Actually, not.
03:53
< ilovefire>
Okay... err... okay.
03:53
< C_tiger_laptop>
ignore me for the moment.
03:54
< C_tiger_laptop>
Try this instead, since vorn seems to have walked off temporarily.
03:54
< Darius>
(sorry, in the loo)
03:54
< C_tiger_laptop>
if a == 0:
03:54
< C_tiger_laptop>
>> print "Zero, baby!"
03:55
< C_tiger_laptop>
else:
03:55
< C_tiger_laptop>
>> print "Nope, not zero here!"
03:55
< C_tiger_laptop>
return.
03:55
< Darius>
and then, after all that, indented like if, print "all done!"
03:56
< C_tiger_laptop>
erm, ok, put a print "all done" before the return.
03:56
< Darius>
no need for the return. If it falls off the end of the function without returning anything, it returns none.
03:56
< C_tiger_laptop>
Ok.
03:56
< C_tiger_laptop>
no need for the return, then.
03:56
< C_tiger_laptop>
All of this should be indented four spaces.
03:56
< C_tiger_laptop>
and >> is my symbol for indent further.
03:57 * ilovefire nods, does it, it works how I expected it to.
03:57
< Darius>
Okay.
03:57
< Darius>
Now, let's rename the function to isitzeroorone
03:58
< C_tiger_laptop>
wee! elif
03:58
< ilovefire>
okay.
03:58
< Darius>
Above the else, put: elif a == 1
03:58
< Darius>
er, with a colon.
03:58
< C_tiger_laptop>
elif a == 1:
03:59
< Darius>
and then after that, indented, put: print "it's one!"
04:00
< ilovefire>
okay.
04:00
< Darius>
try it out, with 0, 1, and 5.
04:00
< ilovefire>
Got the results I expected from those, yes...
04:01
< Darius>
All right then.
04:01
< Darius>
See what elif does?
04:01
< ilovefire>
er... it works as an alternate if statement? Err.
04:02
< C_tiger_laptop>
Programmers of computer languages are super lazy, they can't type "else, if" instead they shorten.
04:02
< ilovefire>
ah.
04:02
<@ToxicFrog>
...hardly.
04:02
<@ToxicFrog>
elif is easier to write a parser for.
04:02
<@ToxicFrog>
Eliminates ambiguity by introducing a new keyword.
04:03
< C_tiger_laptop>
Yes, that too.
04:03
< C_tiger_laptop>
But they could have used elseif
04:04 Chalcedon [~Chalcedon@Nightstar-9233.ue.woosh.co.nz] has joined #code
04:04 mode/#code [+o Chalcedon] by ChanServ
04:04
< Darius>
It works like an alternate if statement, yes. If it fails the if, it tries the elif (well, if there's multiple elifs, it tries the first one, then the second one, yadda, until it runs out or gets one that works), and then if that fails, it goes to the else.
04:04 * ilovefire nods
04:05
< Darius>
Really simple. Note that if: yadda and then if: again works differently - it tries each if independently.
04:06
< ilovefire>
allright.
04:06
< C_tiger_laptop>
Compare the difference between the following two:
04:07
< C_tiger_laptop>
if a<5: //>> print "A is small" // elif a<1: //>> print "A is really small"
04:07
< C_tiger_laptop>
where // is a new line >> is an indent.
04:07
< C_tiger_laptop>
compare that to.
04:08
< C_tiger_laptop>
if a<5: //>> print "A is small" // if a<1: //>> print "A is really small"
04:08
< C_tiger_laptop>
erm, reverse that.
04:08
< C_tiger_laptop>
that won't work.
04:08
< C_tiger_laptop>
One more time:
04:08
< Darius>
pff.
04:08
< C_tiger_laptop>
sorry.
04:09
< Darius>
ilf: meet programming. Even the people who are really good at it screw it up regularly.
04:09
< ilovefire>
Heh.
04:09
< C_tiger_laptop>
if a<1: //>> print "A is really small" // elif a <5 "print A is small"
04:09
< C_tiger_laptop>
compare to
04:09
< C_tiger_laptop>
crap.
04:09
< C_tiger_laptop>
I'm getting tired.
04:09
< C_tiger_laptop>
Add the colon and indents as you see fit.
04:09
< Darius>
(with the appropriate fixings, you should know how to do this by now!)
04:10
< C_tiger_laptop>
and the same thing but using if instead of elif.
04:10
< C_tiger_laptop>
It's hard to code using // and >> when you're not used to them.
04:10
< C_tiger_laptop>
I come from perl and C where whitespace didn't matter, unlike python where it's very important.
04:11
< ilovefire>
well, in the first one, it would only print A is small if a is greater than one, but less than five.
04:11
< Darius>
(greater than or equal to)
04:11
< ilovefire>
Whereas in the second, it would print out A is small even if it also pritns out A is really small.
04:11
< ilovefire>
((right, sorry.))
04:11
< C_tiger_laptop>
Bingo.
04:12
< Darius>
Okay. One more thing, and then we give you an Exercise!
04:12
< C_tiger_laptop>
This is a very important difference.
04:12
< Darius>
two more things, actually, one about print, and one about division.
04:13
< Darius>
The first thing is about print.
04:13
< ilovefire>
okay.
04:13
< Darius>
if you give print a list of things separated by commas, it will print those things separated by spaces. thus, print "foo", "bar" prints "foo bar"
04:14
< ilovefire>
allright.
04:14
< Darius>
Really simple.
04:14
< Darius>
There's lots more cool stuff you can do with strings but I don't feel like getting into them yet.
04:14
< Darius>
The second thing is about division. Try 1/0
04:14
< C_tiger_laptop>
bar =5 // print "foo", bar will give you "foo 5"
04:16
< ilovefire>
gives me a zero division error, vorn.
04:16
< Darius>
All right.
04:17
< Darius>
Here's your Exercise: make a function that divides two numbers and prints (for instance) "the result is 5", but if you would get a zero division error, instead print "I can't divide those!"
04:20
< C_tiger_laptop>
While you code, what is the best way to transmit code in IRC?
04:20
< Darius>
Pastebin.
04:20
< C_tiger_laptop>
Because IRC deletes leading spaces, making indents a pain to check.
04:20
< Darius>
look in the topic.
04:21
< ilovefire>
... what the fuck?
04:21
< ilovefire>
python just crashed on me.
04:21
< Darius>
That's not very good.
04:21
< Darius>
reopen it, I guess.
04:22
< ilovefire>
but not everything did, just my new window and the interactive window.
04:22
< ilovefire>
add.py is stil open.
04:22
< Darius>
This is another lesson: Save Your Work. you never know when something's gonna crash.
04:22
< Darius>
Fortunately in this case you didn't lose much.
04:23
<@ToxicFrog>
...IRC does not delete leading spaces.
04:23
<@ToxicFrog>
leading spaces wheee
04:23
< Darius>
this should be about centered
04:23
< Darius>
okay, I missed long.
04:23
<@ToxicFrog>
this is mostly centered on my screen
04:23
< C_tiger_laptop>
Weird, I'm totally messed up.
04:23
< Darius>
but really, pastebin is your friend.
04:24
<@ToxicFrog>
But Vorn is correct; pastebin is your tiny buffer-filled god.
04:24
< C_tiger_laptop>
It must be some other protocol that deletes leading spaces.
04:24
< C_tiger_laptop>
Ok, pastebin what you have, we'll take a look at it.
04:24
< Darius>
it's just your client.
04:24
< C_tiger_laptop>
Ah.
04:25
< C_tiger_laptop>
Damn my client and stuff.
04:25
< ilovefire>
I lost everything I had. getting it back, though. as soon as I retype it.
04:25
< C_tiger_laptop>
ok
04:27
< ilovefire>
Okay, working...
04:27
< ilovefire>
(what's wierd, though, it crashed as I was saving it.)
04:28
< C_tiger_laptop>
Hmm... it didn't paste
04:28
< Darius>
what do you mean it didn't paste?
04:29
< ilovefire>
Hmm, is the syntax for 'does not equal' !==?
04:29
< Darius>
!=
04:29
< Darius>
there's also < > <= and >=
04:29
< C_tiger_laptop>
I typed my little blurb into the window, clicked paste and it didn't do anything.
04:30
< C_tiger_laptop>
Well it gave me a new window where my code was prettified.
04:30
< C_tiger_laptop>
but nothing in channel.
04:30 mode/#code [+ooo C_tiger_laptop Darius ilovefire] by Darius
04:30
<@ilovefire>
sweet, I thik I've got it.
04:30
<@Darius>
http://pastie.caboo.se/125901 <--- it should give you a url with a number in it.
04:30
<@C_tiger_laptop>
http://pastie.caboo.se/125899
04:30
<@C_tiger_laptop>
Oh.
04:30 * ilovefire goes to pastie it.
04:30
<@C_tiger_laptop>
I'm supposed to copy it over.
04:30
<@C_tiger_laptop>
Ah.
04:31
<@ilovefire>
http://pastie.caboo.se/125903
04:31
<@Darius>
Okay. What if I wanted it to say "the result is 5" instead of just "5"?
04:32
<@Darius>
(once you've figured that bit out, I have another hting I need to point out, but will take a bit of explaining.)
04:34
<@C_tiger_laptop>
Interestingly, it doesn't seem to think that elif is a keyword.
04:35
<@C_tiger_laptop>
Also I realize I forgot colons. This new language thing is going to take some getting used to.
04:36
<@Darius>
rafb.net/paste is better, I think
04:37
<@ilovefire>
is there any way to seperate a variable in a print string from the rest of the string and have it work?
04:37
<@Darius>
We showed you one.
04:37
<@C_tiger_laptop>
Read up if you need a refresher.
04:37 * ilovefire blarghs.
04:38
<@ilovefire>
Oh, no wonder, it's 30 to midnight. That's why I'm confused generally. right, refresher time.
04:38
<@Darius>
look for "the first thing is about print"
04:39
<@ilovefire>
http://pastie.caboo.se/125904
04:39
<@ilovefire>
I actually said "wait... Aha!" alound when I realized it.
04:39 Pi [~sysop@Nightstar-24414.hsd1.wa.comcast.net] has quit [Ping Timeout]
04:40
<@Darius>
you can even put the a/b expression in directly. But.
04:40
<@ilovefire>
But...?
04:42
<@Darius>
No need.
04:43
<@Darius>
There's something important I want to show you though.
04:43
<@ilovefire>
okay.
04:46
<@Darius>
See how your if condition and your elif condition are exact opposites of each other? How if you succeed at one, you definitely fail at the other?
04:46
<@ilovefire>
right...
04:47
<@Darius>
And you can never succeed at both, and you can never fail at both?
04:47
<@Darius>
What this means is that you don't really need to say elif there; you can use elf.
04:47
<@Darius>
er
04:47
<@Darius>
else.
04:48
<@ilovefire>
okay.
04:49
<@ilovefire>
(Idly, I've gotta go in about five minutes.))
04:50
<@Darius>
(okay. I'm about done for now anyway.)
04:51
<@C_tiger_laptop>
I'm trying to think of another exercise that would be fun.
04:51
<@C_tiger_laptop>
But it's hard without learning how to loop.
04:51
<@Darius>
all mine require either a lot more background in Python, or a lot more mathematical knowledge.
04:53 ilovefire [~santros_v@209.82.191.ns-11321] has quit [Quit: Shut up ya wee CROTCH-GNOME, afore I take me hammer an' wail on me jimmies!]
04:54
<@C_tiger_laptop>
I was going to suggest something like write code to generate the fibonacci sequence or generate the first X primes by test division.
05:04
<@Darius>
The first one needs loops; the second, loops and lists.
05:04
<@C_tiger_laptop>
Test division doesn't need lists if you just print as you go.
05:05
<@C_tiger_laptop>
And you don't seive.
05:05
<@C_tiger_laptop>
sieve.
05:05
<@Darius>
true.
05:13 Chalcedon [~Chalcedon@Nightstar-9233.ue.woosh.co.nz] has quit [Quit: Gone]
05:14 C_tiger_laptop [~c_wyz@96.232.24.ns-12742] has quit [Ping Timeout]
05:27 Darius is now known as Vornicus-Latens
05:43 Forj [~Forj@203.211.125.ns-20845] has quit [Quit: Gone]
06:02 Forj [~Forj@Nightstar-9233.ue.woosh.co.nz] has joined #code
06:02 mode/#code [+o Forj] by ChanServ
07:02 Thaqui [~Thaqui@Nightstar-262.dialup.xtra.co.nz] has joined #code
07:02 mode/#code [+o Thaqui] by ChanServ
09:46 You're now known as TheWatcher
10:30 Forj [~Forj@Nightstar-9233.ue.woosh.co.nz] has quit [Quit: Gone]
11:32 Doctor_Nick [~nick@68.42.57.ns-4111] has quit [Ping Timeout]
11:54 Thaqui [~Thaqui@Nightstar-262.dialup.xtra.co.nz] has quit [Quit: This computer has gone to sleep]
13:01 * AnnoDomini is making headway in the JavaBeans territory. The IDE is actually accepting them as drag and drop components. Not displaying them properly, though.
13:05
<@AnnoDomini>
I'll work on it later. As it is, my work is worth a passing grade. :D
13:16 gnolam [lenin@Nightstar-10613.8.5.253.static.se.wasadata.net] has joined #Code
13:16 mode/#code [+o gnolam] by ChanServ
14:43 MyCatVerbs [~mycatverb@Nightstar-13709.lurkingfox.co.uk] has quit [Ping Timeout]
14:47 MyCatVer1s [~mycatverb@Nightstar-13709.lurkingfox.co.uk] has joined #code
15:28 Vornicus-Latens [~vorn@ServicesOp.Nightstar.Net] has quit [Ping Timeout]
15:30 Vornotron [~vorn@76.233.4.ns-3619] has joined #code
16:49 ilovefire [~santros_v@209.82.191.ns-11321] has joined #code
16:51 C_tiger_laptop [~c_wyz@96.232.24.ns-12742] has joined #code
17:11 You're now known as TheWatcher[afk]
17:28 ilovefire is now known as ilf_nothereforawhile
17:30 ilf_nothereforawhile [~santros_v@209.82.191.ns-11321] has quit [Quit: Shut up ya wee CROTCH-GNOME, afore I take me hammer an' wail on me jimmies!]
18:12 Chalain [~chalain@Admin.Nightstar.Net] has quit [Quit: leaving]
18:19 You're now known as TheWatcher
18:53 AnnoDomini [AnnoDomini@Nightstar-29453.neoplus.adsl.tpnet.pl] has quit [Ping Timeout]
18:54 C_tiger_laptop [~c_wyz@96.232.24.ns-12742] has quit [Ping Timeout]
18:57 AnnoDomini [~farkoff@Nightstar-29222.neoplus.adsl.tpnet.pl] has joined #Code
18:57 mode/#code [+o AnnoDomini] by ChanServ
19:14 C_tiger_laptop [~c_wyz@Nightstar-5378.nycmny.east.verizon.net] has joined #code
19:29 C_tiger_laptop [~c_wyz@Nightstar-5378.nycmny.east.verizon.net] has quit [Ping Timeout]
20:24 Chalcedon [~Chalcedon@Nightstar-9802.ue.woosh.co.nz] has joined #code
20:24 mode/#code [+o Chalcedon] by ChanServ
20:32 You're now known as TheWatcher[afk]
20:50 ilovefire [~santros_v@209.82.191.ns-11321] has joined #code
20:57
< Vornotron>
ah, ilf
20:58
<@AnnoDomini>
ilovefire? HERE?!
20:58
<@AnnoDomini>
HERESY!
20:59
< Vornotron>
Are you ready for another lesson?
21:00
< ilovefire>
Vorn: I think so.
21:02
< Vornotron>
Okay.
21:02
< Vornotron>
The first thing I want to cover, is comments.
21:03
< Vornotron>
If, anywhere on a line outside of a literal string (that is to say, something in "" or ''), there is a #, that # and everything after it on that line is ignored.
21:03
< Vornotron>
IDLE will denote this by coloring all that stuff green (I think green, anyway)
21:03
< ilovefire>
red.
21:04
< ilovefire>
But I can see the use for this--putting in notes about what code does and the like?
21:04
< Vornotron>
Indeed.
21:04
< Vornotron>
In general: all functions should have comments describing what they do.
21:04
< Vornotron>
But not necessarily how they do it.
21:05 * ilovefire nods.
21:05
< ilovefire>
(also I'm making a .txt quick referance sheet. I felt it a smart thing to do.)
21:07
< Vornotron>
( a good idea)
21:07
< Vornotron>
All right. That's really all I've got about comments.
21:07 Vornotron is now known as Vornicus
21:08
< ilovefire>
Allright.
21:09
< Vornicus>
Well, okay, one other thing: there exist programs, like Doxygen, which use your comments to generate full-on documentation. You should learn how to talk to some of these, eventually.
21:14
< ilovefire>
Allright.
21:15
< Vornicus>
Anyway, on to the Next Important Thing, which is Lists and Tuples.
21:15
< EvilDarkLord>
Python also supports comments in another format for functions and classes. def a(): \ """Documentation here""" \ Code here.
21:15
< Vornicus>
Yeah, but that involves actually talking about strings, which I don't want to do yet.
21:15
< EvilDarkLord>
Oh, right. Sorry, carry on.
21:17
< Vornicus>
So, what if you have a whole lot of data.
21:17
< Vornicus>
like, a barkingly huge amount.
21:17
< Vornicus>
And you don't want to give it all names.
21:18
< ilovefire>
Umm... okay, just out of the blue guessing: make a list or something?
21:18
< Vornicus>
You make a list.
21:18
< Vornicus>
It looks kinda like this: [2, 3, 5, 7]
21:19 Chalcedon [~Chalcedon@Nightstar-9802.ue.woosh.co.nz] has quit [Ping Timeout]
21:19
< ilovefire>
Okay.
21:19
< Vornicus>
Except that you use whatever data you want in there.
21:19
< Vornicus>
And the [ and the ] don't have to be on the same line.
21:19
< ilovefire>
allright.
21:21
< Vornicus>
Now, you can talk about individual things in a list: primes = [2, 3, 5, 7] ... primes[2] -> 5
21:22
< Vornicus>
the thing[number] notation is called indexing; you put a number in, it looks up the value at that "index", and the result is that number.
21:23
< Vornicus>
You can of course do math and make decisions with stuff you get via indexing, and you can even do math inside the indexing brackets.
21:24 * ilovefire nods...
21:24
< Vornicus>
Now, mostly I've talked about lists, but everything I've done so far also applies to tuples.
21:25
< Vornicus>
but a tuple, instead of using [2, 3, 5, 7], uses (2, 3, 5, 7)
21:26
< ilovefire>
Okay...
21:26
< Vornicus>
You can of course create tuples and lists with only one, or no, elements: [2] is a one-element list, [] is a no-element list, (2,) is a one-element tuple, and (,) is a no-element tuple.
21:27 * ilovefire nods.
21:28
< Vornicus>
But, here's the main difference between tuples and lists: you can /change/ lists. You can't change tuples once they've been created.
21:29
< ilovefire>
Okay, so wouldn't that mean lists are in general better, or am I missing something important?
21:29
<@ToxicFrog>
...I thought the main difference was that tuples could have mixed types, and lists can't?
21:30
< Vornicus>
Well, lists are in general better; but there are some things you can't do with lists, namely you can't use them as keys in dictionaries - this gets pretty important later, right now we're not talking about dictionaries.
21:30
< Vornicus>
TF: you can mix types in both.
21:30
< ilovefire>
Okay.
21:30
<@ToxicFrog>
Aah. I am confusing Python with something else, then.
21:32
< Vornicus>
Anyway, the first thing you can do with lists, is assign things to indexes - primes[3] = 8 works.
21:32 * ilovefire nods...
21:32
< Vornicus>
(you don't really want to do that to your nice list of primes, but)
21:34
< Vornicus>
so, try this: a = [1,2,3,4]; a[2] = 5; print a
21:38
< ilovefire>
gets me [1,2,3,4].
21:38
< ilovefire>
err, 1,2,5,4
21:38
< Vornicus>
Good.
21:39
< Vornicus>
Now, you can do a couple other things, too - you can index using negative numbers: try a[-1], a[-2], a[-3]
21:41
< Vornicus>
(this works on tuples)
21:41
< ilovefire>
those give me 4, 5, and 2 respectively.
21:41
< Vornicus>
see how it works from the end of the list?
21:43
< ilovefire>
sort of, yes. -1 gives me the rigthmost part of the list, -2 the next to rightmost, and soon.
21:43
< ilovefire>
*so on
21:44
< Vornicus>
Right.
21:44
< Vornicus>
Now, here's two things you can do with only lists:
21:44
< Vornicus>
a.append(10)
21:44
< Vornicus>
(then print a)
21:46
< ilovefire>
adds 10 to the end of the list.
21:47
< Vornicus>
okay, now try this: print a.pop()
21:49
< Vornicus>
(and then print a again)
21:51
< ilovefire>
it removed the ten.
21:53
< ilovefire>
So I guess it pops the last thing in a list off?
21:54
< ilovefire>
(oh, a note--one of my friends is coming over soon with his 360, so...)
21:54
< ilovefire>
I won't be here for much longer, so yeah, what else do I need to learn today?
21:55
< Vornicus>
It pops the last thing in a list off... and returns it.
21:55 * ilovefire nods
21:55
< Vornicus>
One last thing you need.
21:55
< Vornicus>
try this:
21:55
< Vornicus>
for k in a:
21:55
< Vornicus>
print k
21:56
< ilovefire>
printed, in different lines, 1, 2, 4, and 5.
21:59
< Vornicus>
That's what for does: it steps through the list you give it, doing its block on each element.
22:01 * ilovefire nods.
22:01
< ilovefire>
... huh, I jsut found out my grandfather knows fortran, cobol, and basic.
22:03
< Vornicus>
impressive.
22:04 * ilovefire nods
22:07 Chalcedon [~Chalcedon@Nightstar-3198.ue.woosh.co.nz] has joined #code
22:07 mode/#code [+o Chalcedon] by ChanServ
22:17 ilovefire is now known as ilf_afk
23:29 Chalcedon [~Chalcedon@Nightstar-3198.ue.woosh.co.nz] has quit [Ping Timeout]
23:51 Chalcedon [~Chalcedon@Nightstar-11255.ue.woosh.co.nz] has joined #code
23:51 mode/#code [+o Chalcedon] by ChanServ
23:59 You're now known as TheWatcher
--- Log closed Sun Dec 09 00:00:44 2007
code logs -> 2007 -> Sat, 08 Dec 2007< code.20071207.log - code.20071209.log >