=== stingernewt is now known as damionloop === 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 4 - Instructors: pedro3005 [20:00] Logs for this session will be available at http://irclogs.ubuntu.com/2010/10/09/%23ubuntu-classroom.html following the conclusion of the session. [20:01] Hello folks [20:01] Anybody out there? [20:01] erm [20:01] my fail [20:02] ok, I'm in -chat now [20:02] Alright, let's begin [20:03] Last class we were talking about functions and list comprehension [20:03] now these are difficult subjects [20:03] I gave some examples but I realized they might've been unclear [20:04] Functions are used a lot in programming [20:04] List comprehension isn't used as much, but it still is used not rarely [20:05] The purpose of functions is simple [20:05] When we are doing one procedure over an over, we can direct it to a function to avoid code repetition [20:06] For instance, suppose we are coding a little greeting program, which talks to the user [20:07] the user gave you his name, something like "Mr. H" or "Mrs. K" [20:07] Now, suppose you have many lines which are oriented by gender [20:08] before each of these lines, you are doing: [20:08] if name.startswith("Mr."): [20:08] bla [20:08] To avoid all this code being repeated over and over, you might use... functions! [20:09] as we learned last class, you can define a function with the keyword def [20:09] All functions return a value [20:09] but some functions return Null [20:10] Functions may or may not receive parameters, which are kind of like informations about the things it's supposed to do [20:10] In our case, it makes sense that our function receives as a parameter the name of the person [20:11] So it can determine the person's gender [20:11] Enough talk, let's get dirty [20:12] >>> def gender(name): [20:13] ... if name.startswith("Mr."): [20:13] ... return "male" [20:13] ... elif name.startswith("Mrs."): [20:13] ... return "female" [20:13] ... [20:13] you can observe it returning [20:13] >>> gender("Mr. Rob") [20:13] 'male' [20:14] Now, let me share a dirty little secret with you [20:15] In this function, we're just assuming the parameter is a string [20:15] what happens if we call it with an integer, for instance? [20:15] >>> gender(2) [20:15] Traceback (most recent call last): [20:15] File "", line 1, in [20:15] File "", line 2, in gender [20:15] AttributeError: 'int' object has no attribute 'startswith' [20:16] But, worry not, my lad [20:17] You, sir, are the programmer [20:17] You know better than to pass an int into your function expecting a string [20:17] But you must be careful with user values [20:17] If the user typed a value, you should NEVER assume it is what it's meant to be [20:17] We've discussed that previously, I just wanted to stress it again [20:19] We can also have functions with no parameters [20:20] As an example, we have the print_help function [20:20] Our program could have a help message [20:20] and we want to create a function to display it [20:20] But I don't really have any parameters, I just want the damn thing to print it [20:20] >>> def print_help(): [20:20] ... print "rtfm" [20:22] You may ask: what does this function return? [20:22] I didn't tell it to return anything [20:22] Well, we can tru to assign it to a variable [20:23] try* [20:23] >>> a = print_help() [20:23] rtfm [20:23] >>> print a [20:23] None [20:23] You can note that the function returns None [20:24] TrueCryer45 asked: None as value? not Null? [20:24] Yeah, I got the name wrong on the first time [20:24] :P [20:24] It's None [20:25] matematikaadit asked: none is none [20:25] >>> None is None [20:25] True [20:25] does that answer your question? [20:26] We also talked about the scope of functions [20:26] A function has its own scope of variables [20:27] for instance, if you declare a variable inside a function, it doesn't exist outside that function [20:27] if you declare variables outside the function, you can access the variable from within the function, but you cannot change it (unless if you call it global) [20:27] let's go over that [20:28] >>> def a(): [20:28] ... n = 1 [20:28] ... [20:28] >>> a() [20:28] >>> n [20:28] Traceback (most recent call last): [20:28] File "", line 1, in [20:28] NameError: name 'n' is not defined [20:28] Here, we can clearly see the effects of this [20:28] n is not defined outside the function [20:29] >>> n = 1 [20:29] >>> def a(): [20:29] ... print n [20:29] ... [20:29] >>> a() [20:29] 1 [20:29] here, we show that you can access the variable even though it wasn't defined in that very function [20:29] >>> def a(): [20:29] ... n = 2 [20:29] ... [20:29] >>> a() [20:29] >>> n [20:29] 1 [20:30] But here, you see that you cannot change n [20:30] When you try to define a new value to n, you are actually creating a new, different n [20:30] this n exists only inside a() [20:30] when you get out of that function [20:30] there is another n [20:30] which is still 1 [20:30] it was not changed [20:31] We can use the global keyword to identify it as a global variable [20:31] >>> def a(): [20:31] ... global n [20:31] ... n = 2 [20:31] ... [20:31] >>> n = 1 [20:31] >>> a() [20:31] >>> n [20:31] 2 [20:31] Are we clear on this, everyone? [20:34] Good [20:34] Let's introduce a new subject then [20:35] Named parameters [20:35] this is useful for holding default values [20:36] for instance, suppose you have a help function [20:37] if you call that with a certain command, it displays help about the command [20:37] if you call it by itself, it displays a general help message [20:37] we can accomplish this with named parameters [20:38] >>> def p_help(command=None): [20:38] ... if command == None: [20:38] ... print "general help message" [20:38] ... else: [20:38] ... print "specific help about %s" % command [20:38] ... [20:38] >>> p_help("ls") [20:38] specific help about ls [20:38] >>> p_help() [20:38] general help message [20:39] if you don't pass anything, command gets the default value of None [20:42] >>> def p_help(command=None, detailed=None): [20:42] ... if command == None: [20:42] ... if detailed: [20:42] ... print "detailed general help message" [20:42] ... else: [20:42] ... print "general help message" [20:42] ... else: [20:42] ... if detailed: [20:42] ... print "detailed help message about %s" % command [20:42] ... else: [20:42] ... print "help message about %s" % command [20:42] ... [20:43] >>> p_help(detailed=True) [20:43] detailed general help message [20:44] So as you see in this last function, you can use named parameters to only pass a certain value to the function [20:44] Any questions? [20:45] OttoBusDriver asked: Can you call functions with named parameters with positional paramters? EG p_help('command', True) [20:45] Yes [20:45] >>> p_help("ls", True) [20:45] detailed help message about ls [20:46] OttoBusDriver asked: And can you define a function with positional and named parameters? [20:46] didn't we just do that? [20:46] def p_help(command=None, detailed=None): [20:48] Good [20:48] It seems we are all clear on this subject [20:48] Let me see if there's anything more of functions I should go through today [20:50] Oh yes [20:50] this looks hard but it is actually quite easy [20:50] Python has a shortcut for easy, one-expression functions [20:50] if you have a function that just grabs a value x and adds 2, for instance [20:50] or grabs the value x and calls some_random_function(x) [20:50] you can use lambda expressions [20:51] >>> a = lambda x: x + 2 [20:51] >>> a(40) [20:51] 42 [20:52] lambda of x is x + 2 [20:52] the syntax is a bit different from the def keyword [20:52] you must assign it to a variable, as you see [20:52] the x is actually the parameter [20:52] not the function's name [20:52] lambda expressions can take in multiple parameters [20:53] >>> a = lambda x, y: x + y [20:53] >>> a(2, 3) [20:53] 5 [20:53] >>> a = lambda x=1, y=2: x + y [20:53] >>> a() [20:53] 3 [20:53] >>> a(2) [20:53] 4 [20:53] And you have named parameters with lambdas ^ [20:54] Lambda comes from functional programming, which (obviously) bases itself heavily on functions [20:54] so it was needed to create a shorter path to small functions [20:54] thus lambda [20:55] It's not used much outside of FP (functional programming), but I wanted to go over it because FP knowledge is important to any decent programmer [20:55] Any questions? [20:57] Alright [20:57] this is it for functions [20:57] I mean, there is more to it, but I think I have explained most of it [20:58] you should play with functions, use them in your programs, get used to them [20:58] they are really important in programming [20:58] Also reference to last class where I explained functions as well [20:58] (to find out that a function can receive another function as argument) [20:59] But let us continue on exploring data structures [20:59] We've been talking about lists [20:59] they are ways of sequencing things [20:59] you can create a list for instace [20:59] instance* [21:00] numbers = [1, 2, 3, 4, 5] [21:00] And we went over various things you can do with lists [21:00] grab elements [21:00] numbers[0] being the first [21:00] put in new elements, with numbers.append(6) or numbers.insert(0, 0) [21:00] (insert 0 at the position 0) [21:01] http://docs.python.org/tutorial/datastructures.html#more-on-lists [21:01] those are all the methods you have with lists [21:01] I want to go over a new type today [21:01] it is called a dict [21:01] short for dictionary [21:02] let's think about real dicitonaries [21:02] they have a word and a definition [21:02] so they are kind of like mapping tools [21:02] they link one thing to another [21:02] in our case, a word to a definition [21:02] In python, we have that [21:03] we have dictionaries that map a string to an object [21:03] but get this [21:03] everything is an object [21:03] a fuction is an object, a list is an object, they're all objects [21:03] so a dict maps a string to pretty much anything [21:03] let's go over the syntax [21:04] imagine you have three users, Rob, Joe and Max [21:04] and they have user ids of 1, 2 and 3 respectively [21:04] let's create a dictionary which maps user to user id [21:05] users = {"Rob":1, "Joe":2, "Max":3} [21:05] Notice we use {} [21:05] We can access an element of this dict by calling: users["Rob"] [21:05] that will return 1 [21:06] because Rob is mapped to 1 [21:06] >>> users["Rob"] [21:06] 1 [21:07] Questions? [21:07] Good [21:08] In Python terms, "Rob", "Joe" and "Max" are keys of the dict users [21:08] And how do you get all the keys in users? [21:08] >>> users.keys() [21:08] ['Max', 'Rob', 'Joe'] [21:09] But hey, wait a minute! [21:09] I put in Rob _first_ [21:09] Why did I get Rob in second? [21:09] What the hell is python thinking?! [21:09] It's simple [21:10] they're random [21:10] You see, Python doesn't guarantee that your dict will be in the order you left it [21:12] The function of dicts is to relate string to anything [21:12] and that function is kept [21:12] I think python 3 will have ordered dicts [21:12] or was that 2.7? [21:12] not sure [21:12] matematikaadit asked: is it possible a key related to nothing? [21:13] well, sure [21:13] but in python we call nothing None :) [21:13] >>> a = {"a": None} [21:13] >>> a["a"] [21:14] Questions? [21:14] Good [21:16] matematikaadit asked: users.keys() is for calling the key, how about the value? [21:16] As TrueCryer45 lovely pointed out, users.values() [21:16] You can use the method .has_key() to check if a dict has a certain key [21:16] >>> users.has_key("Rob") [21:16] True [21:17] example: [21:17] >>> if users.has_key("Rob"): [21:17] ... print "Rob is here. His ID is %s" % users["Rob"] [21:17] ... [21:17] Rob is here. His ID is 1 [21:18] Here we are mapping a string ("Rob", "Joe", whatever) to an int (1, 2, 3 ..) [21:18] But who's to say we can't map strings to lists, for instance? [21:18] We can! [21:19] >>> boxes = {"box1": ["a key", "an old cd"], "box2": ["three coins", "a phone"]} [21:19] >>> boxes["box1"][0] [21:19] 'a key' [21:21] Hell, we can map strings to functions, other dicts, other dicts that have dicts within them, other dicts with dicts with dicts within.. [21:21] in short, we can map strings to practically anything [21:21] with dictionaries [21:22] What if we wanted to print each user's name and id? [21:22] We need to iterate over this dict [21:22] we would use a for loop (I hope you all remember the for loop class) [21:23] >>> for user, id in users.iteritems(): [21:23] ... print user, id [21:23] ... [21:23] Max 3 [21:23] Rob 1 [21:23] Joe 2 [21:23] now, let's analyze that [21:24] do you guys remember tuples? they we're like (1, 2) [21:24] sort of like lists [21:24] I taught that we could unpack these tuples into variables [21:24] x, y = (1, 2) [21:24] x = 1 and y = 2 [21:24] this is exactly what happens in the for loop [21:25] the function returns something like ('Max', 3) [21:25] and it's unpacked into user and in [21:25] id* [21:25] then we print these [21:25] Questions? [21:27] Ok, good [21:28] Now [21:28] Let's turn our attentions to something more tangible [21:28] Hang in there guys [21:28] we're nearly getting over the basic python [21:29] Programs usually have a functionality of reading and writing to files [21:29] so let's learn how to do that in python [21:29] All this time, you've been reading and writing to files and you didn't know it [21:30] print "bla" [21:30] this writes "bla" into the file stdout [21:30] which in our case is the terminal [21:30] when you call raw_input() you're reading from stdin [21:30] how cool is that? [21:30] When we change to other files, it's not much different [21:31] We use the open() function to open a file [21:32] We do it like this: [21:32] f = open("some_text_file") [21:32] notice that we didn't define a mode here [21:32] by default, it is in reading mode [21:32] that is equivalent to [21:33] f = open("some_text_file", "r") [21:33] now you can do things with f [21:33] for instance, let's read our file [21:33] f.read() [21:34] this returns a string with the entire file [21:34] it will not remove newlines ("\n") etc [21:34] Let me quote the python docs [21:34] To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an emp [21:34] ty string (""). [21:37] You may call f.readline() to read just one line [21:37] and you can iterate over the file [21:38] >>> for line in f: [21:38] ... print line [21:38] ... [21:38] this [21:38] is [21:38] a [21:38] file [21:39] iterating over f will give you line by line [21:39] Questions? [21:41] Good [21:41] Writing to files is just as easy [21:41] Open it in "w" mode [21:41] f = open("some_file", "w") [21:41] and then call write() [21:41] f.write("blablabla") [21:42] when you're done, don't forget [21:42] f.close() [21:42] remember that you must call write with a string [21:42] so imagine you have [21:42] answer = 42 [21:43] you can't do [21:43] f.write(answer) [21:43] because answer has type int [21:43] convert it to string [21:43] f.write(str(answer)) [21:44] There are more methods to files [21:44] these are the most used [21:44] http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects [21:44] I recommend you read this [21:46] Given this, I think now you are experienced enough to try your hands at a challenge [21:46] http://ubuntuforums.org/showthread.php?t=884394 [21:48] OH, I forgot to mention [21:48] files also havc the append mode [21:48] which is similar to write [21:48] but you begin writing where the file stop [21:48] whereas the file is cleared if you open it in write mode [21:48] f = open("bla", "w") [21:48] and f = open("bla", "a") [21:48] Is that clear? [21:50] There are 10 minutes remaining in the current session. [21:55] So just for recalling [21:55] There are 5 minutes remaining in the current session. [21:55] so far we went over variables, user input, outputting (print), types, type checking & conversion, boolean expressions, if blocks, for and while loops, functions, lists and dicts, list comprehension, files [21:56] So it'd be good if you all were sharp on these subjects [21:58] I think in the next class I will present a program, so we can study how to actually develop full stuff [21:58] But anyway [21:58] Time for us to call it a day [21:58] I hope this was of some use [22:00] Logs for this session will be available at http://irclogs.ubuntu.com/2010/10/09/%23ubuntu-classroom.html === 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 || === yofel_ is now known as yofel