Unpacking, a Practical Example3:16 with Ashley Boucher
See unpacking in action with a practical example.
Now that you've seen the basics of how unpacking works, 0:00 let's look at a practical example. 0:03 Let's say that in the simple app you're writing you wanna ask your users to 0:05 enter their full name. 0:08 Entering their full name together is quicker for your users, but for 0:10 your purposes you'd rather have their first names and last names separated. 0:12 With unpacking and multiple assignment you can capture the first name and 0:17 last name entered by your user into their own variables 0:20 in just one line of code, let's take a look. 0:23 First we'll ask for the name. 0:26 The \n at the end of the input line simply adds a new line to keep things neat in 0:39 the terminal. 0:43 Okay, so when this program is run, the user will be prompted to enter their name. 0:45 Whatever the user enters is then saved into the full name variable, 0:49 this will be a string value. 0:53 Strings can be split apart at different delimiters using the string method, split. 0:55 Split is nothing more than a function that has one parameter, the delimiter. 1:00 So when we call the split method on a string, 1:04 we pass it whatever the delimiter should be used to split that string. 1:06 The method then returns a list. 1:09 Each element in the list is a piece of the broken up string. 1:11 So let’s split the full name variable. 1:15 We can safely assume that our users will enter their full names with the space 1:17 between each word. 1:20 That space will be our delimiter. 1:22 I’ll call the split method on the input string, and pass our argument, 1:24 a space character. 1:28 Before we move forward, let's print the full name variable and run the program so 1:32 we can see the result of the split method. 1:36 Okay, it's prompted me to enter my full name. 1:51 Yes, Ashley Boucher has been split into a list with two elements. 1:58 The first element is the string 'Ashley', 2:02 and the second element is the string 'Boucher'. 2:04 And lists, like all Python sequences, can be unpacked. 2:07 So instead of assigning the input string to a variable called full name, 2:11 let's take advantage of unpacking and multiple assignment to get the two 2:15 elements of the list into their own variables. 2:18 This change means that the first element of the list, 2:22 Ashley, will be assigned to the variable first. 2:27 And the second element of the list, 2:33 Boucher, will be saved to the variable last. 2:35 Let's print these out, save, and run. 2:38 Clear this down here. 2:48 Awesome we did it. 2:56 In one line of code we took a string, split that string into a list, and 2:57 then unpacked that list into two variables, first and last. 3:02 Now we're free to use these variables separately. 3:06 This could be handy when saving your user to a database, when printing a personal 3:08 message to your user, or when alphabetizing a list of users. 3:12
You need to sign up for Treehouse in order to download course files.Sign up