/srv/irclogs.ubuntu.com/2010/10/02/#ubuntu-classroom.txt

=== James is now known as Guest29919
=== SergioMeneses_ is now known as SergioMeneses
=== Mrokii_ is now known as Mrokii
=== ZachK_ is now known as zkriesse
=== Eric is now known as Guest36399
=== zumbi_ is now known as zumbi
=== Mohan_chml is now known as mohi1
=== kdrx-[-_-]\ is now known as kuadrosx
=== ChanServ changed the topic of #ubuntu-classroom to: Welcome to the Ubuntu Classroom - https://wiki.ubuntu.com/Classroom || Support in #ubuntu || Upcoming Schedule: http://is.gd/8rtIi || Questions in #ubuntu-classroom-chat || Event: Beginners Team Dev Academy - Current Session: Introduction to Python: Part 3 - Instructors: pedro3005
pedro3005Why, hello everyone18:01
pedro3005er.. is anybody out there?18:01
pedro3005*tumbleweed rolls by*18:01
pedro3005Alright, let's go on18:03
* pedro3005 checks logs18:03
pedro3005Well, last time we were talking about lists18:03
pedro3005a quick refresher to you guys18:03
pedro3005numbers = [1, 2, 3, 4, 5]18:04
pedro3005so we have various methods within lists to help us deal with them, as was explained18:04
pedro3005http://docs.python.org/tutorial/datastructures.html#more-on-lists  you can see them here if you forgot18:04
pedro3005(i know you did, don't lie)18:05
pedro3005Well, anyway, today I would like to talk a bit more on list slicing18:05
pedro3005We can get a specific element or number of elements from lists using that18:05
pedro3005take the numbers list for example18:05
pedro3005numbers[0] is 118:05
pedro3005we always start counting at 018:06
pedro3005numbers[1] is 2 and so forth18:06
pedro3005we can use the colon to indicate more than 1 element18:06
pedro3005say I want everything from the beginning to the 3rd element?18:06
pedro3005numbers[0:3] will yield that18:07
pedro3005a list with [1, 2, 3]18:07
pedro3005I can use -1 to go back18:08
pedro3005numbers[-1] is the first element of the list counting from right to left18:08
pedro3005and I can also define the step with an extra colon18:09
pedro3005for instance18:09
pedro3005numbers[0:5:2] returns [1, 3, 5]18:09
pedro3005because it grabs every other number from position 0 to element 518:09
pedro3005All of this list slicing also works with strings18:10
pedro3005>>> "abcd"[0]18:10
pedro3005'a'18:10
ClassBotMohan_chml asked: Is there any difference in using for loop and slicing OR it is just code efficiency?18:11
pedro3005it makes code smaller and neater18:11
pedro3005and easier to understand too18:11
pedro3005For dealing with strings, we have this method called split()18:12
pedro3005it splits a string (returning a list of strings) by a certain character18:12
pedro3005for instance, space18:12
pedro3005"a b c d".split(" ") will return ['a', 'b', 'c', 'd']18:13
pedro3005split() is the same as split(" ")18:13
pedro3005you can also split by comma, for instance18:13
pedro3005"a,b,c,d".split(",") will do the same18:13
pedro3005the inverse of the split function is join18:14
ClassBotOtto56 asked: Does split accept multiple seperator characters or does it then split on teh sequence / sub-string?18:15
pedro3005No, your example would split by ":;"18:15
pedro3005<Otto56> eg split(";:")18:15
pedro3005err, I mean ";:"18:15
pedro3005You can use regular expressions for what you want18:16
pedro3005(but I don't know regexp :P)18:16
pedro3005or you can call subsequent split methods18:16
pedro3005wait.. no you can't18:16
pedro3005never mind that18:16
pedro3005As I was saying, the join function18:16
pedro3005you call it with a string and a list and it will join each element of this list with a string between them18:17
pedro3005let me exemplify18:18
pedro3005", ".join(['a', 'b', 'c', 'd'])18:18
pedro3005that's pretty much it about join18:19
pedro3005any questions?18:19
pedro3005there are a whole bunch of string methods, and it's not my goal to go over each of them18:20
pedro3005many are self-explanatory18:20
pedro3005you have for instance str.startswith() and str.endswith()18:20
pedro3005check the python docs for all18:20
pedro3005http://docs.python.org/library/stdtypes.html#string-methods18:20
pedro3005I would like to introduce a new concept here18:20
pedro3005before we go on to more data structures, like dicts etc. it's important to learn functions18:21
pedro3005All this time, we've been using various functions18:22
pedro3005raw_input(), split(), they're all functions18:22
pedro3005and we call functions sometimes with parameters, like split(" ")18:22
pedro3005that's calling split with " " as argument18:22
pedro3005We can define our own functions18:23
pedro3005This is useful for a procedure that you're doing various times in your code18:23
pedro3005to avoid writing the same thing over and over, you define a function18:23
pedro3005for instance, let's define a function that prints a help message18:23
pedro3005def print_help():18:24
pedro3005    print "You're on your own"18:24
pedro3005now that that's done, when you call print_help() it'll display that message18:24
pedro3005you can add more print commands to it18:25
pedro3005We also learned about boolean expressions, and we can make functions that answer boolean questions18:25
pedro3005for instance, let's create a function that checks if a number is negative18:26
pedro3005def is_negative(number):18:26
pedro3005    if number < 0:18:26
pedro3005        return True18:26
pedro3005    return False18:26
pedro3005let's analyze this18:27
pedro3005the function is_negative accepts a parameter, called number18:27
pedro3005if this number is smaller than 0, it returns True18:27
pedro3005the thing is, when a function returns a value, it immediately stops executing and goes back to the main code18:28
pedro3005so it wouldn't hit return False18:28
pedro3005but if the if block was not run, that is, number > 0, it would just read return False18:28
pedro3005;)18:28
pedro3005you could otherwise rewrite that function as18:28
pedro3005def is_negative(number):18:28
pedro3005    if number < 0:18:28
pedro3005        return True18:28
pedro3005    else:18:28
pedro3005        return False18:28
pedro3005but it's one line too big :)18:28
pedro3005we can write functions that accept more than one parameters18:29
pedro3005for instance, let's make a function that multiplies two values18:29
pedro3005def mul(x, y):18:30
pedro3005    return x * y18:30
pedro3005but what if I wanted a function to double a number?18:30
pedro3005I could try writing it as this (it is _WRONG_)18:30
pedro3005def double(x):18:30
pedro3005    x = 2 * x18:31
pedro3005why is this wrong?18:31
pedro3005because the variable we're dealing with inside the function is a copy of the variable you called the function with18:32
pedro3005if we only alter the copy, it won't alter the original variable18:35
ClassBotOtto56 asked: Is it possible to pass values in by reference?18:35
pedro3005like in C? no18:35
pedro3005we can make global variables18:35
pedro3005can I brb for some minutes? family calls18:35
pedro3005sorry everyone18:36
pedro3005back18:44
pedro3005For this problem, we have two solutions18:44
pedro3005if we're dealing with a specific variable, we can make it global18:45
pedro3005a = 218:45
pedro3005def double_a():18:45
pedro3005    global a18:45
pedro3005    a = a * 218:45
pedro3005Or, we can return the new value and set the old variable equal to that18:46
pedro3005def double(x):18:46
pedro3005    return 2 * x18:46
pedro3005a = 418:46
pedro3005a = double(a)18:46
pedro3005that would yield 818:46
pedro3005a function can return and accept any object18:47
pedro3005and everything is an object18:47
pedro3005you can for instance define a function that completes a sentence with the period, for instance18:48
pedro3005err, too many for instance's18:49
pedro3005but let's show that18:49
pedro3005def complete_sentence(sentence):18:49
pedro3005    if sentence.endswith("."):18:49
pedro3005        return sentence18:49
pedro3005return sentence + "."18:50
pedro3005you could modify that function to check that the first letter is a capital letter etc18:50
pedro3005(left as an exercise for the reader :P)18:50
pedro3005a function can even accept another function as argument (do I hear some functional programming?)18:51
pedro3005for instance, imagine we have functions that return some numeric value. e.x. minus_one(x), minus_two(x) ad infinitum. (what they do is fairly obvious, and so is the implementation)18:52
pedro3005we want to make a generic function which calls it a number is negative and record the number of steps (for some freakish reason. my examples suck. sorry)18:53
pedro3005give me a minute to write that18:53
=== harrisonk_away is now known as harrisonk
pedro3005>>> def call(f, x):18:55
pedro3005...     if x < 0:18:55
pedro3005...             return 018:55
pedro3005...     steps = 018:55
pedro3005...     while x:18:55
pedro3005...             x = f(x)18:55
pedro3005...             steps += 118:55
pedro3005...     return steps18:55
pedro3005...18:55
pedro3005let's analyze that step by step18:55
pedro3005(day by day...)18:55
pedro3005sorry, let's continue18:55
pedro3005Well, if x is already smaller than 0 (negative), it took 0 steps, right?18:55
pedro3005so we return that18:55
pedro3005we continue the function (if it's positive) by initializing the number of steps at 018:56
pedro3005while x, that is, while x > 018:56
pedro3005hmmm18:56
pedro3005maybe I should change that to while x >= 018:56
pedro3005yes18:56
pedro3005>>> def call(f, x):18:57
pedro3005...     if x < 0:18:57
pedro3005...             return 018:57
pedro3005...     steps = 018:57
pedro3005...     while x >= 0:18:57
pedro3005...             x = f(x)18:57
pedro3005...             steps += 118:57
pedro3005...     return steps18:57
pedro3005...18:57
pedro3005that is correct now18:58
pedro3005Same thing18:58
pedro3005while x is bigger or equal to 0, that is, non-negative18:58
pedro3005we run these steps18:58
pedro3005first, we set x to be f(x), that is, we call the function upon x18:58
pedro3005we raise the step count by 1 and continue iterating18:58
pedro3005when it is done, we return steps18:58
pedro3005so, consider we had the function minus_one() and the value 418:59
pedro3005call(minus_one, 4)18:59
pedro3005returns 5, as expected18:59
pedro3005we type minus_one like that, without parenthesis ()18:59
pedro3005because we're not calling the function, we're passing it as argument18:59
pedro3005python has the built-in function map() but we can rewrite it for fun19:00
pedro3005one min19:00
pedro3005>>> def new_map(f, values):19:03
pedro3005...     final = []19:03
pedro3005...     for x in values:19:03
pedro3005...             final.append(f(x))19:03
pedro3005...     return final19:03
pedro3005...19:03
pedro3005so, what does this do?19:04
pedro3005it accepts a list and calls the function f on each element of the list, returning a new list with the returned values19:04
pedro3005>>> new_map(minus_one, [1, 2, 3])19:04
pedro3005[0, 1, 2]19:04
pedro3005(again, python already has the function map())19:04
ClassBotOtto56 asked: What is the python terminology for this? I understand it as a function pointer or delegate19:05
pedro3005hm.. I'm not aware of a specific terminology19:05
pedro3005it's a function19:05
pedro3005it's not a pointer to a function, it IS a function19:05
pedro3005forget pointers :P19:05
pedro3005(how I hate them)19:06
pedro3005anyway19:06
pedro3005We could write the map() function more easily using list comprehension, but I didn't go over that19:06
pedro3005any questions? does everyone understand the functions?19:07
=== harrisonk is now known as harrsonk_away
pedro3005So let's go back to lists with list comprehension19:17
pedro3005this is useful, I promise19:17
pedro3005list comprehension is a method of constructing a new list by some iteration19:18
pedro3005it's useful for instance to unpack strings19:18
pedro3005let's say I want to get a string "abcd" and turn it into a list with each character, i.e. ['a', 'b', 'c', 'd']19:19
pedro3005newlist = [x for x in "abcd"]19:19
pedro3005but I can do things with this first x19:20
pedro3005let's say I want to ensure that all characters are lower-case19:20
pedro3005we have the function lower()19:20
pedro3005newlist = [x.lower() for x in "AbcD"]19:20
pedro3005or, imagine this19:21
pedro3005we have a list of lists19:21
pedro3005[[1, 2], [3, 4], [5, 6]]19:21
pedro3005we want a simple list with all these values multiplied, as in, 1 * 2, 3 * 4, 5 * 619:22
pedro3005we can do that with:19:23
pedro3005newlist = [x * y for x, y in oldlist]19:23
pedro3005(given oldlist = [[1, 2], [3, 4], [5, 6]])19:23
pedro3005we're unpacking each element of the list (another list)19:24
pedro3005let me exemplify a simpler case19:24
pedro3005imagine we have a = [1, 2]19:24
pedro3005we can do19:24
pedro3005x, y = a19:24
pedro3005we are unpacking a19:24
pedro3005x = 1, y = 219:24
pedro3005so, as I mentioned, we can rewrite the map function using list comprehensions19:25
pedro3005def new_map(f, x):19:25
pedro3005    return [f(x) for val in x]19:26
pedro3005errrrr19:26
pedro3005def new_map(f, values):19:26
pedro3005    return [f(x) for x in values]19:26
pedro3005we can use this to improvise multiple-argument maps19:27
pedro3005for instance, the built-in function max() returns the max value of the list it is called with19:27
pedro3005hmm, no , forget that19:28
pedro3005hehe19:28
pedro3005someone give me a function with two arguments :P19:28
pedro3005ohh19:29
pedro3005we have the cmp() function. it accepts two integers19:29
pedro3005it returns negative if x < y, zero if x == y, and positive if x > y (given cmp(x, y))19:29
pedro3005we can map that19:29
pedro3005a = [[1, 2], [3, 3], [4, 3]]19:30
pedro3005vals = [cmp(x, y) for x, y in a]19:30
pedro3005>>> vals19:30
pedro3005[-1, 0, 1]19:30
pedro3005questions?19:31
pedro3005we can add boolean expressions to list comprehensions19:32
pedro3005say I want all positive values from a  list19:32
pedro3005a = [1, 2, -5, 0, 4]19:32
pedro3005newlist = [x for x in a if x > 0]19:33
pedro3005we can use all sorts of expressions there19:34
pedro3005for instance, we can call int() but only if it's a digit19:34
pedro3005>>> a = ['a', "2", "c", "4"]19:35
pedro3005>>> [int(x) for x in a if x.isdigit()]19:35
pedro3005[2, 4]19:35
pedro3005we can use this to strip out of a string all upper-case characters for instance19:35
pedro3005message = "HELLO there MY friend"19:36
pedro3005>>> [x for x in message.split() if not x.isupper()]19:36
pedro3005['there', 'friend']19:36
pedro3005this one is a bit more complex so I'll go over it19:37
pedro3005I wanted to separate message by each word it had, so I called message.split(). if you recall, that defaults to split(" "), that is, grab by each space19:37
pedro3005so we're effectively getting each word19:37
pedro3005if not x.isupper()  would be equal to saying  if x.isupper() == False19:38
pedro3005but shorter and in my opinion clearer19:38
pedro3005because it's closer to natural language19:39
pedro3005Questions?19:39
pedro3005Alright19:42
pedro3005Now, as I promised last class, I will solve a couple problems19:42
pedro3005first I must introduce a simple built-in function, range()19:42
pedro3005it does arithmetic progressions19:42
pedro3005>>> range(10)19:43
pedro3005[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]19:43
pedro3005range(10) means that stop = 10. it stops at 10 without including 10.  And it defaults to starting at 0, but you can specify19:43
pedro3005>>> range(1, 10)19:43
pedro3005[1, 2, 3, 4, 5, 6, 7, 8, 9]19:43
pedro3005now we started at 119:43
pedro3005we can also specify the step19:44
pedro3005>>> range(1, 10, 2)19:44
pedro3005[1, 3, 5, 7, 9]19:44
pedro3005picking every other number19:44
pedro3005the step may be negative to count descending19:44
pedro3005>>> range(10, 0, -1)19:44
pedro3005[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]19:44
pedro3005What is this useful for, you ask19:45
pedro3005well, many things19:45
pedro3005let's take for instance this problem19:45
pedro3005Add all the natural numbers below one thousand that are multiples of 3 or 5.19:45
pedro3005well, we'll be working with the range of 1 - 1000 (we can skip 0, since it's not a multiple)19:46
pedro3005let's ask ourselves, how can we translate "multiple of 3 or 5" to python?19:46
pedro3005well, if x is a multiple of 3, when we divide x by 3 we get 0 rest19:47
pedro3005right?19:47
pedro3005so we use the % operator19:47
pedro3005meaning that if x is a multiple of 3, x % 3 == 019:47
pedro3005now it's easy!19:47
pedro3005first, we get a variable to store the sum19:47
pedro3005value = 019:47
pedro3005for num in range(1, 1000):19:48
pedro3005    if num % 3 == 0 or num % 5 == 0:19:48
pedro3005        value += num19:48
pedro3005after the loop is done, value will hold the sum of all the natural numbers below one thousand that are multiples of 3 or 519:49
pedro3005Questions?19:50
pedro3005The old classic FizzBuzz (let's see if I have enough time)19:58
pedro3005we must loop from 1 to 100, print Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz if both19:59
pedro3005for num in range(1, 101): # gotta account for 10019:59
pedro3005    if num % 3 == 0:20:00
pedro3005err, wait20:00
pedro3005answer = ""20:00
=== ChanServ changed the topic of #ubuntu-classroom to: Welcome to the Ubuntu Classroom - https://wiki.ubuntu.com/Classroom || Support in #ubuntu || Upcoming Schedule: http://is.gd/8rtIi || Questions in #ubuntu-classroom-chat ||
pedro3005lol damn20:00
pedro3005ok20:01
pedro3005FizzBuzz is left as homework!20:01
pedro3005:P20:01
pedro3005I already gave a hint20:01
pedro3005;)20:01
=== pedro_ is now known as pedro3005
nUboon2AgeFor those interested, i started a home page for pedro3005's Intro to Python class:  https://wiki.ubuntu.com/BeginnersTeam/FocusGroups/Development/Academy/IntroToPython20:41
nUboon2Agepedro3005: question: in the above class you referred several times to f(), and you passed it in as an argument as: somefunc(f,x), but it was never defined.  is that a function defined by python or one that you would define (but you just didn't in these examples)?22:02
pedro3005nUboon2Age, double(x): return x + x22:02
pedro3005is x defined anywhere else in the code?22:02
nUboon2Agesorry i'm still not understanding pedro300522:04
pedro3005nUboon2Age,22:05
pedro3005>>> def double(x):22:05
pedro3005...     return x + x22:05
pedro3005...22:05
pedro3005>>> double(2)22:05
pedro3005422:05
pedro3005x is not defined elsewhere, just in that function22:06
pedro3005the variable x exists only in the double function, when it is called. x is a copy of whatever value was passed to it22:06
nUboon2Ageokay22:06
nUboon2Agemakes sense so far22:06
pedro3005similarly, we can do this with functions22:06
pedro3005for instance, a function that calls f on x (for example purposes)22:07
pedro3005>>> def call(f, x):22:08
pedro3005...     f(x)22:08
pedro3005...22:08
pedro3005>>> def p(x):22:08
pedro3005...     print x22:08
pedro3005...22:08
pedro3005>>> call(p, "Hello world!")22:08
pedro3005Hello world!22:08
nUboon2Ageoh, okay, i think i'm starting to get it.22:09
pedro3005nUboon2Age, just start to think of things as objects22:10
pedro3005a function is also an object22:10
pedro3005so it can be passed around22:10
nUboon2Agei'll go back and look at the prior examples to see if i understand them now.22:11
pedro3005nUboon2Age, are you good with math?22:11
nUboon2Agepedro3005: not great, just okay22:12
nUboon2Agei think this is the line i was confused by:  x = f(x)22:12
nUboon2Agei guess the f(x) was not explicitly defined in the example22:13
pedro3005no, by that line we're calling the function that was passed to us22:14
pedro3005we don't know what the function does22:14
pedro3005we just expect it to return something22:14
pedro3005and we set x to be that22:14
pedro3005think of it like this22:14
pedro3005on that example, we're dealing with numeric functions22:14
pedro3005so they're supposed to take a number like 4 and return something like 322:15
pedro3005so imagine we have x = 422:15
pedro3005we want to call the function which does whatever and then set x to be that new value22:15
pedro3005x = f(x)22:15
pedro3005repeat this process until we get a negative value22:15
nUboon2Ageokay i got that.22:16
nUboon2Ageanother thing that i was confused by was "return [f(x) for val in x]"  -- the 'for val in x' part isn't clear to me pedro300522:17
pedro3005nUboon2Age, it was a mistake by me22:18
nUboon2Ageoh, what was it supposed to say?22:18
pedro3005return [f(x) for x in values]22:18
pedro3005nUboon2Age, it kind of makes sense if you read it out loud. [f of x for (each) x in values]22:20
nUboon2Ageokay, could you break that down for me please pedro3005?22:20
nUboon2Ageso what would an example be pedro3005?22:20
pedro3005grab each x inside values and add to the new list we're creating the return value of f(x)22:21
pedro3005hm, for instance22:21
pedro3005[x for x in [1, 2, 3]]22:21
pedro3005that is simply [1, 2, 3]22:21
pedro3005for each x in the old list, it puts x in the new list22:21
pedro3005but imagine this22:21
pedro3005[str(x) for x in [1, 2, 3]]22:21
pedro3005that is ['1', '2', '3']22:22
pedro3005for x in the old list, it puts string of x in the new list22:22
pedro3005for each*22:22
pedro3005similarly, we can do22:22
pedro3005[x for x in [-1, 0, 1] if x > 0]22:22
nUboon2Agedoes x mean each element in the array?22:22
pedro3005for each x in the old list, put x in the new list if x is bigger than 022:23
pedro3005nUboon2Age, yes, we're iterating through it22:23
nUboon2Ageand x could be any label, not just x?22:24
nUboon2Age[goomba for goomba in [-1,0,1]if goomba > 0] -- pedro3005?22:25
pedro3005nUboon2Age, absolutely22:25
nUboon2Agei'm closer but not quite there pedro300522:25
pedro3005nUboon2Age, do you understand for loops?22:26
nUboon2Ageyes22:26
nUboon2Agethe thing that is confusing me i think is22:26
nUboon2Agethe first x22:26
pedro3005>>> a = [1, 2, 3, 4, 5]22:26
pedro3005>>> new_a = []22:26
pedro3005>>> for number in a:22:26
pedro3005...     new_a.append(number)22:26
pedro3005...22:26
pedro3005>>> new_a22:26
pedro3005[1, 2, 3, 4, 5]22:27
pedro3005that is the same thing as saying new_a = [x for x in a]22:27
nUboon2Ageso i understand ... for x in[blah], but why the first x?22:27
pedro3005nUboon2Age, well, again, read it out loud. for x in [blah]. imagine you're the interpreter. Okay, we've got that; for x in [blah]. but what do I do with it?22:28
nUboon2Ageoh, i'm starting to get it22:28
pedro3005x for x in [blah]  -- okay, just return x.  [f(x) for x in [blah]] -- okay, call f() with x and return that22:28
nUboon2Agepedro3005: are you saying that x for x in [blah] would just return x?22:31
pedro3005nUboon2Age, well, it would grab each x in blah and return x to the new list22:32
nUboon2Agepedro3005: so    z for x in [blah] would grab each x in blah and return z to the new list ?22:33
pedro3005there are like, two dudes, one building the new list and the other dude tells the first one what to put in. you're that dude. so you read x for x, pick up 4 and just tell him: "4!". but if you read f(x), you call f with x, and get, for instance, 3. So you tell him "3!"22:33
pedro3005nUboon2Age, no, that would be an error22:33
nUboon2Age?22:33
pedro3005that wouldn't work22:33
nUboon2Agedo they have to be the same variable pedro3005?22:34
pedro3005nUboon2Age, yes. read it: for each x in [blah], put z in the new list. that doesn't make sense, does it?22:34
pedro3005list comprehension is a way of constructing a new list with a certain set of instructions22:35
nUboon2Agepedro3005: unfortunately it makes sense to me ;-)22:35
pedro3005nUboon2Age, well, be the interpreter: WTF is z?22:35
nUboon2Age;-)22:35
pedro3005if you try that,  it would raise a WTFError22:35
nUboon2Agei'd have to define it elsewhere i guess i was assuming22:36
pedro3005well, actually a NameError22:36
pedro3005NameError: name 'z' is not defined22:36
nUboon2Age:-)22:36
pedro3005nUboon2Age, the variable inside the list comprehension is temporary22:36
pedro3005like uhm..22:36
nUboon2Agehmmm... okay.22:36
pedro3005imagine your mom tells you this: grab each thing in this box. if this thing is a spoon, put it in the kitchen. if it's a dead animal throw it away22:37
nUboon2Ageyou gave me a good laugh there pedro300522:37
pedro3005what is 'thing'?22:38
nUboon2Ageokay so how would you write that in python?22:38
pedro3005well, you're iterating over each item of the box and calling that 'thing'22:38
nUboon2Agething for thing in box[spoon,dead animal]?22:39
pedro3005to_the_kitchen = [thing for thing in box if thing == spoon]22:39
pedro3005remember, you're building a new list22:39
pedro3005okay, I have a better example22:40
pedro3005the box is full of apples and oranges22:40
nUboon2Ageto_the_kitchen = [thing for thing in box[spoon,dead animal] if thing == spoon]   pedro3005?22:40
pedro3005your dad tells you: take two boxes, label them apples and the other oranges. grab each fruit in the old box. if it's an apple, put it in the apples box, and put it in the oranges box otherwise22:40
pedro3005apples = [fruit for fruit in box if fruit == apple]  (this is an example, the variables box and apple are NOT defined, and thus would FAIL)22:41
pedro3005oranges = [fruit for fruit in box if fruit == orange]22:41
nUboon2Ageokay, and if you were to complete the example to that it would not fail what would that look like?22:42
nUboon2Ages/to/so22:43
pedro3005>>> apple = "apple"22:43
pedro3005>>> orange = "orange"22:43
pedro3005>>> box = [apple, orange, orange, apple, orange, orange, apple, apple, apple, orange]22:43
pedro3005>>> apples = [fruit for fruit in box if fruit == apple]22:43
pedro3005>>> oranges = [fruit for fruit in box if fruit == orange]22:43
pedro3005>>> apples22:43
pedro3005['apple', 'apple', 'apple', 'apple', 'apple']22:43
pedro3005>>> oranges22:43
pedro3005['orange', 'orange', 'orange', 'orange', 'orange']22:43
nUboon2Ageokay i get that.  i'22:44
nUboon2Agei'll save that example22:44
nUboon2Agemuchas gracias pedro300522:44
pedro3005nUboon2Age, :) now do homework :P22:44
nUboon2Agepedro3005: first i have to get through the rest of the examples. ;-)22:45
pedro3005nUboon2Age, and I will try to come up with better examples next time22:46
nUboon2Agepedro3005: math examples sometimes elude me, so for me this example made more sense.22:48
pedro3005nUboon2Age, yeah, sorry, I didn't think of those not good with abstract math22:49
nUboon2Agepedro3005: is the word 'global' predefined in python?22:50
pedro3005nUboon2Age, yes, it is a keyword22:52
pedro3005nUboon2Age, don't worry too much about global though22:54
pedro3005it's not very common to use22:55
pedro3005normally you'll just pass variables back and forth22:55
nUboon2Agein the above examples i didn't really understand what the map() is about pedro300522:56
pedro3005nUboon2Age, ah, a little relic from functional programming22:57
pedro3005here's what it does22:57
pedro3005it receives two parameters, a function and a list of values22:57
pedro3005it returns a list with the returned value by calling this function with each value22:57
=== yofel_ is now known as yofel
pedro3005like, if you call map(x, [a, b, c]) it returns [x(a), x(b), x(c)]22:58
pedro3005let me exemplify22:59
pedro3005suppose you have a list ['1', '2', '3']23:00
pedro3005call that list snums23:00
pedro3005map(int, snums)23:00
pedro3005this calls int() with each element of snums23:00
pedro3005and returns a list substituting the value for the returned value of the function23:00
pedro3005in this case [1, 2, 3]23:00
nUboon2Ageokay, that kinda makes sense.  i'll have to ponder it for a little while pedro300523:01
nUboon2Ageokay i'm getting that now pedro300523:06
pedro3005nUboon2Age, these are not trivial concepts, so it's normal to take a while to comprehend23:07
nUboon2Ageone thing that i'm kinda surprised by is the seemingly large number of standard predefined functions.  i'm used to languages where there are not very many pedro300523:19
pedro3005nUboon2Age, well, there are mere programming languages, and there's python23:20
pedro3005python is not a language, it's a lifestyle!23:20
pedro3005:P23:20
pedro3005python's standard library is ginormous23:21
pedro3005So big my class won't go over a tiny fraction of it23:22
pedro3005besides the built-in functions (the ones you can access without importing anything), which already are many, there are loads and loads of modules23:23
pedro3005think of anything you can do with a computer23:24
pedro3005python has a standard module for that23:24
pedro3005nUboon2Age, in conclusion, python rocks, love it23:37
nUboon2Agepedro3005: i'm happy to be learning python and it seems really fun, so thank you for your instruction. ;-)23:40
pedro3005nUboon2Age, is it your first language?23:41
nUboon2Agepedro3005: i've been away from programming for a little while, but23:47
nUboon2Agebasic was my first language, followed much latter by pascal, C, turbo pascal with objects, C++, all in school, and then i taught myself Java23:49
nUboon2Ageoh, and i had a little assembly too.23:49
pedro3005ah, cool23:50
nUboon2Agebut i'm really rusty, so this is brushing my cobwebs off.  ;-)23:50
pedro3005nUboon2Age, if you knew Java, classes shouldn't be too much of a problem23:51
nUboon2Agea little shell programming (and a fair bit of DOS batch files) too23:51
nUboon2Agepedro3005: yes, classes shouldn't be too bad.23:51
pedro3005nUboon2Age, python classes can inherit from various other ones ;)23:52
nUboon2Agei haven't gone as far as i'd like into OOP, so there may be some things that i'll need to wrestle with a bit.23:52
nUboon2Agegood point pedro300523:53
pedro3005nUboon2Age, I think the biggest problem with python is how slow it is23:54
nUboon2Agepedro3005: yes that's an issue for every interpreted language.  but i'm pleasantly surprised how reasonable the speed is of python.23:58
nUboon2Ageoh, i forgot to mention i taught myself some perl too.23:58
pedro3005nUboon2Age, oh, I'm a language whore too23:59
pedro3005I go out learning a lot of different languages :P23:59

Generated by irclog2html.py 2.7 by Marius Gedminas - find it at mg.pov.lt!