Skip to content

Commit

Permalink
removed redundant blank spaces from 10,11,12
Browse files Browse the repository at this point in the history
  • Loading branch information
hclivess committed Apr 17, 2017
1 parent 88fbcea commit af23ffa
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 485 deletions.
100 changes: 50 additions & 50 deletions transcripts/10-tuples/1.txt
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
0:01 Tuples play a central role in Python, we are going to look at variety of techniques 
0:05 and ways to use tuples that are particularly Pythonic, 
0:09 let's start with assignment and unpacking, and we'll start in code. 
0:01 Tuples play a central role in Python, we are going to look at variety of techniques
0:05 and ways to use tuples that are particularly Pythonic,
0:09 let's start with assignment and unpacking, and we'll start in code.
0:13 So, you probably know that tuples are defined like this, so it could be a number,
0:19 another number, a string, even be like an array. 
0:23 So if we wanted to just look at the our little creation here, we can go like this, 
0:28 we'll see that it's actually not the parentheses that have anything to do with this, 
0:34 usually, we could just as well write like this, and we get the same output, 
0:38 it's the commas, not the parentheses that make the tuple. 
0:44 In fact, we can come down here and can have a shorter one, like this, 
0:49 sometimes you want to have a tuple with just one element in it, we could write this, 
0:53 there is a tuple of length 1, let's find out. There, a tuple of length 1. 
1:00 So that tuples, they can't grow after they are created, 
1:03 they are basically immutable objects that it can be added to or moved, things like that. 
1:08 So that's not the Pythonic thing, that's just tuples. 
1:12 We get values out of them like this, if we wanted to say 
1:14 print out the word "cat" that's in the second index position, zero-based, 
1:18 so there we'd print "cat", if we didn't actually do this, there that prints "cat", 
1:28 so the first thing we want to look at is unpacking these into variables, 
1:32 let's go over here and work with the shorter version, 
1:34 so let's suppose we had the number 7 and the word "cat" and that was all, 
0:19 another number, a string, even be like an array.
0:23 So if we wanted to just look at the our little creation here, we can go like this,
0:28 we'll see that it's actually not the parentheses that have anything to do with this,
0:34 usually, we could just as well write like this, and we get the same output,
0:38 it's the commas, not the parentheses that make the tuple.
0:44 In fact, we can come down here and can have a shorter one, like this,
0:49 sometimes you want to have a tuple with just one element in it, we could write this,
0:53 there is a tuple of length 1, let's find out. There, a tuple of length 1.
1:00 So that tuples, they can't grow after they are created,
1:03 they are basically immutable objects that it can be added to or moved, things like that.
1:08 So that's not the Pythonic thing, that's just tuples.
1:12 We get values out of them like this, if we wanted to say
1:14 print out the word "cat" that's in the second index position, zero-based,
1:18 so there we'd print "cat", if we didn't actually do this, there that prints "cat",
1:28 so the first thing we want to look at is unpacking these into variables,
1:32 let's go over here and work with the shorter version,
1:34 so let's suppose we had the number 7 and the word "cat" and that was all,
1:38 if I want to have 2 variables, one for the number and one for the... let's say animal,
1:45 I could say "n for number = this" and I could say "a for animal = that", 
1:52 that would not be Pythonic, instead, what you would say is "n, a = t", 
1:57 and Python will unpack the first value into the first thing here, 
2:01 the second value into the second thing there, and so on. 
2:04 Now, if we had another one, like another number here, and we try to run this, 
2:08 you'll see it crashes because it says too many values, 
1:45 I could say "n for number = this" and I could say "a for animal = that",
1:52 that would not be Pythonic, instead, what you would say is "n, a = t",
1:57 and Python will unpack the first value into the first thing here,
2:01 the second value into the second thing there, and so on.
2:04 Now, if we had another one, like another number here, and we try to run this,
2:08 you'll see it crashes because it says too many values,
2:11 so if we are doing that we would typically say
2:13 I don't care what this value is and use an underscore to say 
2:16 "please ignore it", so let's run that. 
2:18 And PyCharm is just saying "look you assign these values 
2:22 and you never use them here", so let's do this. 
2:24 Now, maybe we should show you what numbers came out 
2:26 or what values came out of there, so we'll say like so, "n is 7", "a is cat", beautiful. 
2:32 And underscore, well we could grab it but we don't care, that's the whole point of it. 
2:37 So this concept lets us assign values in a single line 
2:40 so I could say "x, y = 1, 2", then I could print "x" and "y" and I would get 1 and 2. 
2:49 Finally, this tuple unpacking is a very important when we are talking about loops, 
2:55 remember our numerical "for...in" loop where it gives us both index and the item, 
3:00 we wrote something like this: "for index, item in enumerate", something like this, 
3:06 for enumerating over a collection, we'll get the index and the item, 
3:09 so 0 had 1, "cat" and so on, there, 0 goes to 1, 2 goes to "cat", 
3:17 well this returns a tuple and we unpack them into index and item, 
3:22 so this tuple unpacking is super important 
3:24 and we'll see more of it as we go through this chapter. 
2:13 I don't care what this value is and use an underscore to say
2:16 "please ignore it", so let's run that.
2:18 And PyCharm is just saying "look you assign these values
2:22 and you never use them here", so let's do this.
2:24 Now, maybe we should show you what numbers came out
2:26 or what values came out of there, so we'll say like so, "n is 7", "a is cat", beautiful.
2:32 And underscore, well we could grab it but we don't care, that's the whole point of it.
2:37 So this concept lets us assign values in a single line
2:40 so I could say "x, y = 1, 2", then I could print "x" and "y" and I would get 1 and 2.
2:49 Finally, this tuple unpacking is a very important when we are talking about loops,
2:55 remember our numerical "for...in" loop where it gives us both index and the item,
3:00 we wrote something like this: "for index, item in enumerate", something like this,
3:06 for enumerating over a collection, we'll get the index and the item,
3:09 so 0 had 1, "cat" and so on, there, 0 goes to 1, 2 goes to "cat",
3:17 well this returns a tuple and we unpack them into index and item,
3:22 so this tuple unpacking is super important
3:24 and we'll see more of it as we go through this chapter.
3:28 So we saw we can create a tuple, we can unpack it into a variety of values,
3:31 should we have a tuple of 4 values, we unpack it into greeting and one into enclosing, 
3:36 then we just print them out, you see the values come out 
3:31 should we have a tuple of 4 values, we unpack it into greeting and one into enclosing,
3:36 then we just print them out, you see the values come out
3:39 as if they had been pulled out individually,
3:41 we also saw that enumerate returns tuples 
3:44 and the way that we actually separate the values in a really nice clean way 
3:47 maybe didn't even notice this one has happened 
3:50 is we are actually using tuple unpacking into those two items 
3:53 as it comes back from enumerate. 
3:41 we also saw that enumerate returns tuples
3:44 and the way that we actually separate the values in a really nice clean way
3:47 maybe didn't even notice this one has happened
3:50 is we are actually using tuple unpacking into those two items
3:53 as it comes back from enumerate.
44 changes: 22 additions & 22 deletions transcripts/10-tuples/2.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
0:01 Let's see the Pythonic way to swap two values and hint: It involves tuples. 
0:01 Let's see the Pythonic way to swap two values and hint: It involves tuples.
0:05 All right, so here we have two values, "x" and "y",
0:08 you can see we'll print them out here and we'll print them out there 
0:10 and our attention is to do some sort of swap thing. 
0:13 Let's just run it really quickly. 
0:14 All right, obviously not swapped yet, let's swap them here. 
0:17 In most languages, this is sort of a 3-step process, 
0:20 you'd say something like "temp = x", "x = y", "y = temp". 
0:25 Now if we run this, you'll see they should be swapped, 
0:27 great, 7, 11, 11, 7, but this is non-Pythonic. 
0:34 And let's even teach PyCharm: "Hey, that's a word". 
0:37 OK, so if we are going to do this in a Pythonic way, we are going to use tuples, 
0:42 and it turns out you can do it in a beautiful concise one-liner 
0:45 by temporarily creating a tuple and then unpacking 
0:48 it into the same variables but in reverse, so we can say "y, x = x, y". 
0:54 Remember, the comma here creates a tuple 
0:56 and then the stuff in the left hand side will unpack that tuple 
0:59 back into the values but it unpacks "x" into "y" and unpacks "y" into "x". 
1:03 Beautiful, one line, very Pythonic, let's see if it works. 
1:07 Ta da, same thing, much cleaner. 
1:10 Want to swap two values in Python? Create a tuple 
1:13 and unpack it back into the reversed set of variables, 
1:17 so here we have "x" and "y", we say "y, x = x, y". Swapped, one line, very Pythonic. 
0:08 you can see we'll print them out here and we'll print them out there
0:10 and our attention is to do some sort of swap thing.
0:13 Let's just run it really quickly.
0:14 All right, obviously not swapped yet, let's swap them here.
0:17 In most languages, this is sort of a 3-step process,
0:20 you'd say something like "temp = x", "x = y", "y = temp".
0:25 Now if we run this, you'll see they should be swapped,
0:27 great, 7, 11, 11, 7, but this is non-Pythonic.
0:34 And let's even teach PyCharm: "Hey, that's a word".
0:37 OK, so if we are going to do this in a Pythonic way, we are going to use tuples,
0:42 and it turns out you can do it in a beautiful concise one-liner
0:45 by temporarily creating a tuple and then unpacking
0:48 it into the same variables but in reverse, so we can say "y, x = x, y".
0:54 Remember, the comma here creates a tuple
0:56 and then the stuff in the left hand side will unpack that tuple
0:59 back into the values but it unpacks "x" into "y" and unpacks "y" into "x".
1:03 Beautiful, one line, very Pythonic, let's see if it works.
1:07 Ta da, same thing, much cleaner.
1:10 Want to swap two values in Python? Create a tuple
1:13 and unpack it back into the reversed set of variables,
1:17 so here we have "x" and "y", we say "y, x = x, y". Swapped, one line, very Pythonic.
114 changes: 57 additions & 57 deletions transcripts/10-tuples/3.txt
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
0:01 Sometimes you need to return more than one value from a function. 
0:04 Let's see what the story of that is in Python. 
0:07 So over here we have a non-Pythonic way 
0:09 to return more than one value from a function, 
0:01 Sometimes you need to return more than one value from a function.
0:04 Let's see what the story of that is in Python.
0:07 So over here we have a non-Pythonic way
0:09 to return more than one value from a function,
0:11 now in Python, we don't have the concept of reference parameters,
0:16 passing a pointer by reference, 
0:19 we  just have passing the pointer, which lets us work with the values. 
0:22 Some languages you can do things like this, you can say "int & val 1, int & val 2", 
0:29 that would be like C++, and say C# you might say like this: "out" or "ref" or "in out", 
0:37 things like this, we could even, if this was pass by value, 
0:41 we could even pass a pointer and then let it change, 
0:44 there is lots of things that some languages let us do, 
0:47 Python doesn't let us do that. 
0:48 So here is one way which we can kind of do this in a kind of a hokey way 
0:52 so here we are passing in some value to work with, 
0:54 we want to compute 2 values and return both the values, 
0:57 so here we are going to pass in a list, 
0:59 and if the list is empty we are going to make a spot for 2 entries, 
1:02 otherwise, if the list is not length 2, we are going to complain 
1:05 and say "Oh this is not really what we are looking for", 
1:07 we wanted either a list with 2 elements 
1:09 or an empty list so that we can stuff the two return values into them. 
1:13 Do a quick little bit of math and back here we get the values out 
1:17 and we pull them out, this is super non-Pythonic. 
1:21 This is bad, so the question is: "Can we do better?" 
1:24 First of all, let's see if we pass in 7 
1:27 that we are going to get the right values. 49 and 18.52 
1:31 Those are right values, but the code, not so right, 
1:34 so let's take this and have a good version. 
1:37 Keep that one down here we'll make this one to be Pythonic, 
1:40 we'll just call it out_params, and we are going to do something entirely different. 
1:43 We are going to get rid of all the stuff, this link, all this junk, 
1:46 watch how much simpler this gets. 
1:49 So we'll have, let's say, return value 1, 
1:53 those are not good names in general for variables 
1:55 but maybe just to make a case of look 
1:57 these are the two values we are returning, we'll call of this. 
0:16 passing a pointer by reference,
0:19 we  just have passing the pointer, which lets us work with the values.
0:22 Some languages you can do things like this, you can say "int & val 1, int & val 2",
0:29 that would be like C++, and say C# you might say like this: "out" or "ref" or "in out",
0:37 things like this, we could even, if this was pass by value,
0:41 we could even pass a pointer and then let it change,
0:44 there is lots of things that some languages let us do,
0:47 Python doesn't let us do that.
0:48 So here is one way which we can kind of do this in a kind of a hokey way
0:52 so here we are passing in some value to work with,
0:54 we want to compute 2 values and return both the values,
0:57 so here we are going to pass in a list,
0:59 and if the list is empty we are going to make a spot for 2 entries,
1:02 otherwise, if the list is not length 2, we are going to complain
1:05 and say "Oh this is not really what we are looking for",
1:07 we wanted either a list with 2 elements
1:09 or an empty list so that we can stuff the two return values into them.
1:13 Do a quick little bit of math and back here we get the values out
1:17 and we pull them out, this is super non-Pythonic.
1:21 This is bad, so the question is: "Can we do better?"
1:24 First of all, let's see if we pass in 7
1:27 that we are going to get the right values. 49 and 18.52
1:31 Those are right values, but the code, not so right,
1:34 so let's take this and have a good version.
1:37 Keep that one down here we'll make this one to be Pythonic,
1:40 we'll just call it out_params, and we are going to do something entirely different.
1:43 We are going to get rid of all the stuff, this link, all this junk,
1:46 watch how much simpler this gets.
1:49 So we'll have, let's say, return value 1,
1:53 those are not good names in general for variables
1:55 but maybe just to make a case of look
1:57 these are the two values we are returning, we'll call of this.
2:00 So we can come down here and we can return a tuple and we can say that
2:04 just say "r1, r2", that defines a tuple, that's one thing, we'll return that. 
2:09 That's a little bit like what we were doing before with our list, 
2:12 so we could like say return a list but the thing that's cool 
2:16 is the tuple unpacking lets us get at that value really easy, 
2:20 so we can come over here and we can say we would like to call this function 
2:22 out_params with the value 7, we'd like to capture the values, remember, 
2:26 it's coming back as a tuple, so we can unpack that into individual values 
2:29 and give basically the appearance that our method is returning more than one values, 
2:34 we can say "v1, v2 = this", I can print out let's say the good version 
2:40 instead of this funky stashing stuff in the list, we just say v1, v2. 
2:44 Now it literally looks like this method returns more than one value, 
2:48 but the trick that facilitates it is of course tuples and tuple unpacking. 
2:52 Perfect, besides a little bit of spacing, it looks identical. 
2:56 There, identical, so much better. 
3:00 So we saw we can fiddle with collection types to make it sort of possible 
2:04 just say "r1, r2", that defines a tuple, that's one thing, we'll return that.
2:09 That's a little bit like what we were doing before with our list,
2:12 so we could like say return a list but the thing that's cool
2:16 is the tuple unpacking lets us get at that value really easy,
2:20 so we can come over here and we can say we would like to call this function
2:22 out_params with the value 7, we'd like to capture the values, remember,
2:26 it's coming back as a tuple, so we can unpack that into individual values
2:29 and give basically the appearance that our method is returning more than one values,
2:34 we can say "v1, v2 = this", I can print out let's say the good version
2:40 instead of this funky stashing stuff in the list, we just say v1, v2.
2:44 Now it literally looks like this method returns more than one value,
2:48 but the trick that facilitates it is of course tuples and tuple unpacking.
2:52 Perfect, besides a little bit of spacing, it looks identical.
2:56 There, identical, so much better.
3:00 So we saw we can fiddle with collection types to make it sort of possible
3:06 to return more than one value, this is really a bad idea, don't do this kind of stuff,
3:10 instead, leverage the ability to create and return as single tuple 
3:14 and then unpack them as if they were multiple values, 
3:17 so here we are calling compute values, return it to tuple, 
3:19 we are unpacking that into two variables we are calling "b2", and "b32", 
3:24 and we are printing them out. 
3:26 Wonderful. 
3:10 instead, leverage the ability to create and return as single tuple
3:14 and then unpack them as if they were multiple values,
3:17 so here we are calling compute values, return it to tuple,
3:19 we are unpacking that into two variables we are calling "b2", and "b32",
3:24 and we are printing them out.
3:26 Wonderful.
Loading

0 comments on commit af23ffa

Please sign in to comment.