So far we've created sequences like lists, strings and tuples,
just by declaring literals,
just like on line one,
how we declared this list through.
Another way to create them is through concatenation and repetition.
Concatenate takes two sequences and adds them together to form one long sequence.
So, here on line three,
we say print out the value of this sequence with two items,
one and two, concatenated with this other sequence with two other items, three and four.
Now, the result of this overall expression is
going to be a new list that has all of those items kind of squished together.
So, it's going to be a new list with items one,
two, three, four. So, let's run.
We can see that our new list just has items one,
two, three and four.
So, that's concatenation.
We can also take an existing list like fruit and
concatenate it with a list that we create on the file like right here.
So, here we have a list of strings in fruit.
So, we have apple, orange, banana, and cherry.
In here, we take that list and concatenate it
with another list with numbers six, seven, eight, nine.
When we do that, then we're going to get apple,
orange, banana, cherry, six, seven, eight, nine.
Let's run to be sure.
You can see that these two lists were concatenated.
In addition to concatenation,
we can also do repetition,
or repeating a list any number of times.
Whereas concatenation uses the addition or plus operator,
repetition uses the star or multiplication operator.
So, if we have this list that has one item,
that item being the integers here,
and we say repeat this item four times by saying,
the list and then multiply it by four,
then we get a new list that repeats the contents of this list four times.
So we should get a list that has zero four times.
You see that's exactly what we get.
If this list had more than one item in it, so,
let's suppose this list had zero and one as items,
then both of those items get repeated four times as well.
So, you can see our new list has zero,
one, zero, one, zero, one.
Just like before, we can actually mix up different types.
So, we can say something like, print out fruit,
plus the list one and multiply that four times.
So, that's going to take apple, orange, banana,
cherry, and then add one to that list.
So, apple, orange, banana, cherry, one.
It's going to repeat that four times.
So you see apple, orange, banana, cherry,
one, apple, orange, banana,
cherry, one, and so on.
So, one thing to note about concatenation and repetition is that they follow
the exact same order of operations as addition, multiplication, and subtraction.
So here, we put parentheses to say that we want to
do the concatenation before we do the repetition.
If we had actually left out the parentheses,
then we would get the list fruit plus the list one repeated four times.
So, we would get an entirely different result.
So, let's go through this in code lens.
So, suppose that we create that same list that we created last time,
so we have fruit as a list, apple, orange, banana,
cherry, If I stepped forward,
and you'll see that fruit here gets assigned to a list with item zero,
item one, two, three.
Then, if I create a new list of numbers,
so those numbers being six and seven,
then we create a new list in the global frame.
So, we say numlist is assigned to a list with six and seven in it.
Now, if I create another new list,
and this time I'll just call it new list,
and say that it's the concatenation of fruit and numlist,
what you'll see is that we create a third list,
and that third list contains some elements from fruit and some elements from numlist.
So, we're not affecting either fruit or numlist in anyway.
We're just creating a new list that actually
has both of their elements concatenated together.
If I create a fourth list of zeros, to say,
zero repeated four times,
then you can see that that's a fourth item in our global frame.
So, let's answer some questions about concatenation and repetition.
So, this question asks,
what's printed by the following statements?
So, we create a-list, a-list with three items,
one, three and five,
and b-list with another three items, two,
four and six, and we print out the value of a-list plus b-list.
What do we get? So, it can be tempting to say, one, two,
three, four, five, six,
because that's what's natural to us.
But, because a-list is one, two, three,
all of those items come before the items of b-list,
which are two, four and six.
So, the result that we get from a-list plus b-list is one,
three, five, and then two, four, six.
So, the order is the elements of a-list in order,
and then the elements of b-list in order.
So, that means that the answer is going to be C. Let's do another one.
So, in this question, we create a list,
a-list and set it to a list with three items one,
three and five, and then we print out the value of that repeated three times.
So, what we should get is this repeated three times.
So, one, three, five, one, three,
five, one, three, five,
a list that has nine items.
That's going to be answer
C. That's all for concatenation and repetition. See you next time.