1 00:00:00,160 --> 00:00:03,230 Welcome to the final lesson of Python sequences. 2 00:00:03,230 --> 00:00:05,630 In this video, we're going to be discussing concatenation and 3 00:00:05,630 --> 00:00:08,440 multiplication of Python sequences. 4 00:00:08,440 --> 00:00:12,400 Concatenation is when you attach one object to the end of another, so together, 5 00:00:12,400 --> 00:00:14,520 they become one larger object. 6 00:00:14,520 --> 00:00:18,670 For mutable sequence types, like lists, this concatenation can be done in place, 7 00:00:18,670 --> 00:00:21,240 the original list can become a larger list. 8 00:00:21,240 --> 00:00:23,090 For immutable sequence types, like tuples and 9 00:00:23,090 --> 00:00:27,780 strings, concatenation always involves creating an entirely new object. 10 00:00:27,780 --> 00:00:29,810 This is not very memory efficient. 11 00:00:29,810 --> 00:00:33,140 So if you're making repeated concatenations on an immutable type, 12 00:00:33,140 --> 00:00:35,290 you might want to consider a different data structure. 13 00:00:36,300 --> 00:00:39,420 Concatenation is done by using what looks like simple addition, 14 00:00:39,420 --> 00:00:41,800 the plus sign, let's take a look. 15 00:00:41,800 --> 00:00:44,230 Here I have two lists, object1 and object2. 16 00:00:44,230 --> 00:00:47,137 To concatenate, I just add them together. 17 00:00:55,786 --> 00:00:57,550 What do you think will happen if print out object1? 18 00:00:58,550 --> 00:01:06,044 Let's find out. 19 00:01:12,310 --> 00:01:17,050 As you can see, object1 now contains all the elements of object1 and object2. 20 00:01:18,110 --> 00:01:20,790 It's important to note that it doesn't matter if the sequences you're 21 00:01:20,790 --> 00:01:23,440 concatenating have any overlapping elements. 22 00:01:23,440 --> 00:01:27,070 Concatenation simply attaches the second object to the end of the first and 23 00:01:27,070 --> 00:01:29,000 ignores the contents. 24 00:01:29,000 --> 00:01:31,940 It won't try to merge the objects, remove duplicates, or 25 00:01:31,940 --> 00:01:34,440 change the order of a sequence in any way. 26 00:01:34,440 --> 00:01:35,450 Pause the video here and 27 00:01:35,450 --> 00:01:38,790 take a moment to play with concatenation in your attached workspace. 28 00:01:38,790 --> 00:01:41,880 What happens if you change the provided list to tuples? 29 00:01:41,880 --> 00:01:43,930 What if you change them to strings? 30 00:01:43,930 --> 00:01:46,290 When you're done exploring concatenation, come back, and 31 00:01:46,290 --> 00:01:47,780 we'll wrap up with multiplication. 32 00:01:49,520 --> 00:01:52,400 How did it go, did you learn anything new? 33 00:01:52,400 --> 00:01:54,450 Now, we'll move on to multiplication. 34 00:01:54,450 --> 00:01:57,410 This is basically concatenation multiple times. 35 00:01:57,410 --> 00:01:59,770 It's also called repetition. 36 00:01:59,770 --> 00:02:01,190 When you multiply a sequence, 37 00:02:01,190 --> 00:02:05,900 you concatenate that sequence to itself the provided number of times. 38 00:02:05,900 --> 00:02:10,000 The syntax for that is the sequence name, an asterisk, which means multiply, 39 00:02:10,000 --> 00:02:14,230 and then the number of times it should be repetitively concatenated. 40 00:02:14,230 --> 00:02:16,470 For this example, we'll use a short string. 41 00:02:22,719 --> 00:02:28,239 To multiply this string five times, I'll use the code, str*5. 42 00:02:28,239 --> 00:02:34,690 Now, let's wrap this in a print statement, And save it, and run it. 43 00:02:40,929 --> 00:02:45,020 All right, it printed out python concatenated together five times, 44 00:02:45,020 --> 00:02:46,550 great work. 45 00:02:46,550 --> 00:02:49,270 Check out the teacher's notes to learn more about immutability and 46 00:02:49,270 --> 00:02:51,530 concatenation and multiplication. 47 00:02:51,530 --> 00:02:52,780 See the following text step for 48 00:02:52,780 --> 00:02:56,310 more examples of both using different sequence types. 49 00:02:56,310 --> 00:02:58,680 I'm really proud of the work you've done in this course. 50 00:02:58,680 --> 00:03:02,250 Finish up with a code challenge and a quiz and celebrate your accomplishment, and 51 00:03:02,250 --> 00:03:03,690 don't forget, keep on coding.