Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.
In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.
Concatenation, in the context of programming, is the operation of joining two strings together. The term"concatenation" literally means to merge two things together.
In Python, there are a few ways to concatenate – or combine - strings. The new string that is created is referred to as a string object. Obviously, this is because everything in Python is an object – which is why Python is an objected-oriented language.
In order to merge two strings into a single object, you may use the “+” operator. When writing code, that would look like this:
str1 = “Hello”
str2 = “World”
str1 + str2
The final line in this code is the concatenation, and when the interpreter executes it a new string will be created.
One thing to note is that Python cannot concatenate a string and integer. These are considered two separate types of objects. So, if you want to merge the two, you will need to convert the integer to a string.
We are accustomed to using the * symbol to represent multiplication, but when the operand on the left side of the * is a list, it becomes the repetition operator. The repetition operator makes multiple copies of a list and joins them all together. Lists can be created using the repetition operator, *. For example,
numbers = [0] * 5
print numbers
This will give the output:
[0, 0, 0, 0, 0]
[0] is a list with one element, 0. The repetition operator makes 5 copies of this list and joins them all together into a single list. Another example using multiple elements in the list.
numbers = [0, 1, 2] * 3
print numbers
This will give the output:
[0, 1, 2, 0, 1, 2, 0, 1, 2]
Note that Python creates shallow copies of the lists in this. So changing objects at one place will change them at all places they are repeated. If you don't want this behaviour, don't use repetition operator to create lists.
The membership operators in Python are used to test whether a value is found within a sequence. For example, you can use the membership operators to test for the presence of a substring in a string. Two membership operators exist in Python:
Here are a couple of examples:
>>> 'Hello' in 'Hello world!'
True
>>>
>>> 'Hello' in 'Hello world!'
True
>>> 'house' in 'Hello world!'
False
>>> 'hello' in 'Hello world!'
False // note that Python is case sensitive
>>> 'orld' in 'Hello world!'
True
>>> 'Hello' not in 'Hello world!'
False
>>> 'car' not in 'Hello world!'
True
>>> 'house' not in 'Hello world!'
True
In simple terms, index() method finds the given element in a list and returns its position.
However, if the same element is present more than once, index() method returns its smallest/first position.
Note: Index in Python starts from 0 not 1.
The syntax of index() method for list is:
list.index(element)
The slice object is used to slice a given sequence (string, bytes, tuple, list or range) or any object which supports sequence protocol (implements __getitem__() and __len__() method).
Slice object represents the indices specified by range(start, stop, step).
The syntax of slice() are:
slice(stop)
slice(start, stop, step)
slice() mainly takes three parameters which have the same meaning in both constructs:
If a single parameter is passed, start and step are set to None.
slice() returns a slice object used to slice a sequence in the given indices.