1 00:00:00,720 --> 00:00:02,790 Hello and welcome to yet another Worked 2 00:00:02,790 --> 00:00:06,860 Exercise from Python for Informatics Exploring Information. 3 00:00:06,860 --> 00:00:11,230 My name is Charles Severance and I am your exercise coach. 4 00:00:11,230 --> 00:00:14,681 I'm your personal trainer, your personal assistant. 5 00:00:14,681 --> 00:00:17,190 Welcome to the software development gym. 6 00:00:18,712 --> 00:00:21,206 This material, of course, is copyright Creative 7 00:00:21,206 --> 00:00:23,920 Commons as is the textbook as well. 8 00:00:23,920 --> 00:00:29,420 So just make sure when you're using these exercises that you're using them properly, 9 00:00:29,420 --> 00:00:33,170 particularly if you're taking this class and you have an 10 00:00:33,170 --> 00:00:38,340 assignment that includes this exercise, you probably should try it first. 11 00:00:38,340 --> 00:00:43,940 I mean, these exercises are there so that you learn the material and if you just 12 00:00:43,940 --> 00:00:47,100 sort of, take the crutch every time, then you 13 00:00:47,100 --> 00:00:50,150 won't learn the, to do things on your own. 14 00:00:50,150 --> 00:00:53,080 So enough of that lecturing. 15 00:00:55,270 --> 00:00:58,023 Other details you can get from pythonlearn.com. 16 00:00:59,330 --> 00:01:04,380 So today we're contemplating exercise 6.9 and our goal is 17 00:01:04,380 --> 00:01:09,710 to write some code, to parse lines that start with this, something 18 00:01:09,710 --> 00:01:15,040 blah blah blah blah, colon, and then we're going to chop off 19 00:01:15,040 --> 00:01:17,320 the second half and convert it to a floating point number. 20 00:01:18,515 --> 00:01:22,900 In a later chapter we'll do this in a loop, but for now we're 21 00:01:22,900 --> 00:01:28,360 just going to like hack one line and just see what, what it looks like, okay? 22 00:01:28,360 --> 00:01:31,560 And so, so this one's kind of a silly one but 23 00:01:33,660 --> 00:01:34,550 we'll do it, we'll do it anyway. 24 00:01:34,550 --> 00:01:43,320 So this one, let's grab this string while we're at it and start up TextWrangler. 25 00:01:43,320 --> 00:01:46,402 Oh, the last exercise is still there. 26 00:01:46,402 --> 00:01:52,608 Start TextWrangler and start the Terminal program. 27 00:01:54,608 --> 00:01:58,430 In my Terminal program, I'll go to my desktop, 28 00:01:58,430 --> 00:02:03,826 I'll go to Python for informatics folder, 29 00:02:04,826 --> 00:02:11,364 and I'll paste that string in and I'm just going to call 30 00:02:11,364 --> 00:02:15,480 my string x for now and put this in a string. 31 00:02:15,480 --> 00:02:18,040 Later, you'll be reading this from a file but 32 00:02:18,040 --> 00:02:20,640 this exercise is not to read anything from a file. 33 00:02:20,640 --> 00:02:23,140 This exercise is just to parse that string. 34 00:02:24,390 --> 00:02:26,790 So it's going to be pretty simple. 35 00:02:26,790 --> 00:02:29,720 I will print x just for yucks. 36 00:02:30,720 --> 00:02:33,220 We could make it so it would 37 00:02:33,420 --> 00:02:34,450 prompt for this string in a 38 00:02:34,450 --> 00:02:38,650 loop or something like that but we'll, we'll leave that for now. 39 00:02:38,650 --> 00:02:41,820 We're just going to, our job is just hacking and slashing. 40 00:02:41,820 --> 00:02:44,260 So the first thing we're going to do is 41 00:02:44,260 --> 00:02:47,640 we're going to use the find function in the string library. 42 00:02:47,640 --> 00:02:53,111 We've got to make a variable called position and we're going to say x dot find colon. 43 00:02:53,111 --> 00:02:59,797 It's like, where is that colon at? Print position. 44 00:02:59,797 --> 00:03:08,760 And we're going to save this guy as spamchop.py 45 00:03:08,760 --> 00:03:13,800 And, of course, the colors change and we do an ls and here's spamchop.py 46 00:03:13,800 --> 00:03:20,847 So it's python spamchop.py, and again there's no input here. 47 00:03:20,847 --> 00:03:24,650 Try that again, spamchop.py. 48 00:03:24,650 --> 00:03:26,007 So there we go. 49 00:03:26,007 --> 00:03:32,430 That basically says that the position of this letter is in position 18. 50 00:03:32,430 --> 00:03:35,670 So the next thing we're going to do is we're going 51 00:03:35,670 --> 00:03:39,700 to just sort of say, okay, let's chop off the first part. 52 00:03:39,700 --> 00:03:46,030 print x and we're going to use string slicing, p-o-s colon, 53 00:03:46,030 --> 00:03:49,920 which says start at position and move to the end. 54 00:03:49,920 --> 00:03:54,990 So I'll save that one and I'll run it and that looks pretty close. 55 00:03:54,990 --> 00:04:00,170 We got rid of most of it but we still don't, we still have the colon. 56 00:04:00,170 --> 00:04:03,780 So we gotta chop off one after the position of the colon. 57 00:04:03,780 --> 00:04:06,050 We actually are not interested in the colon. 58 00:04:06,050 --> 00:04:10,860 So we just add one here and then we go, spamchop. 59 00:04:10,860 --> 00:04:13,690 Now, we're going to see if the float cares about this. 60 00:04:13,690 --> 00:04:18,864 I don't think so but we'll find out. num equals float 61 00:04:19,864 --> 00:04:24,700 and I'm just going to say x of position plus 1 62 00:04:24,700 --> 00:04:30,020 because I can give it an expression here, to the end, and let's see what happens. 63 00:04:30,020 --> 00:04:30,960 print num. 64 00:04:32,360 --> 00:04:33,683 So what do you think is going to happen? 65 00:04:33,683 --> 00:04:35,051 Think it's going to work? 66 00:04:35,051 --> 00:04:37,576 Think it's going to blow up? 67 00:04:37,576 --> 00:04:39,941 Think these spaces at the front are going to freak it out? 68 00:04:39,941 --> 00:04:42,132 Let's find out. 69 00:04:42,132 --> 00:04:42,530 No. 70 00:04:42,530 --> 00:04:45,020 The spaces don't freak it out. 71 00:04:45,020 --> 00:04:51,290 So we now have a number and if I did, if I did a print of type, 72 00:04:51,290 --> 00:04:55,620 which is to be sort of like obsessively compulsive, 73 00:04:55,620 --> 00:05:00,470 you couldd say num comma, type of num, and so 74 00:05:00,470 --> 00:05:05,172 proven to me, and I can, I can comment these guys out. 75 00:05:05,172 --> 00:05:13,180 We don't need those guys anymore. 76 00:05:13,180 --> 00:05:16,480 And that is a floating point number and it's all parsed and it's cool. 77 00:05:16,480 --> 00:05:18,520 I mean it's pretty simple, right? 78 00:05:18,520 --> 00:05:20,510 You go find where this thing is, advance 79 00:05:20,510 --> 00:05:24,160 one character beyond it, and then slice it out. 80 00:05:24,160 --> 00:05:25,670 Simple enough. 81 00:05:25,670 --> 00:05:26,430 Thanks for your time.