Python Tutorial_ Sequential Data Types
Python Tutorial_ Sequential Data Types
Home Python 2 Tutorial Python 3 Tutorial Advanced Topics Numerical Programming Machine Learning Tkinter Tutorial Contact
Slicing works with three arguments as well. If the third argument is for example 3, only every third element of the list, string or tuple from the range of the first two arguments will be taken.
s[begin], s[begin + 1 * step], ... s[begin + i * step] for all (begin + i * step) < end.
In the following example we define a string and we print every third character of this string:
The following string, which looks like letter salad, contains two sentences. One of them contains covert advertising of my Python courses in Canada:
Try to figure it out using slicing with the step argument. The solution is: You have to set step to 2
>>> s
'TPoyrtohnotno ciosu rtshees lianr gTeosrto nCtiot yb yi nB oCdaennasdeao'
>>> s[::2]
'Toronto is the largest City in Canada'
>>> s[1::2]
'Python courses in Toronto by Bodenseo'
>>>
You may be interested in how we created the string. You have to understand list comprehension to understand the following:
Length
The length of a sequence, i.e. a list, a string or a tuple, can be determined with the function len(). For strings it counts the number of characters and for lists or tuples the number of
elements are counted, whereas a sublist counts as 1 element.
Concatenation of Sequences
Combining two sequences like strings or lists is as easy as adding two numbers. Even the operator sign is the same.
The following example shows how to concatenate two strings into one:
The augmented assignment "+=" which is well known for arithmetic assignments work for sequences as well.
s += t
s = s + t
But it is only syntactically the same. The implementation is different: In the first case the left side has to be evaluated only once. Augment assignments may be applied for mutable objects as an optimization.
It's easy to check, if an item is contained in a sequence. We can use the "in" or the "not in" operator for this purpose.
The following example shows how this operator can be applied:
Repetitions
So far we had a "+" operator for sequences. There is a "*" operator available as well. Of course there is no "multiplication" between two sequences possible. "*" is defined for a sequence and an integer, i.e. s * n or
n * s.
It's a kind of abbreviation for an n-times concatenation, i.e.
str * 4
is the same as
Further examples:
>>> 3 * "xyz-"
'xyz-xyz-xyz-'
>>> "xyz-" * 3
'xyz-xyz-xyz-'
>>> 3 * ["a","b","c"]
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
In our previous examples we applied the repetition operator on strings and flat lists. We can apply it to nested lists as well:
>>> x = ["a","b","c"]
>>> y = [x] * 4
>>> y
[['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']]
>>> y[0][0] = "p"
>>> y
[['p', 'b', 'c'], ['p', 'b', 'c'], ['p', 'b', 'c'], ['p', 'b', 'c']]
>>>
This result is quite astonishing for beginners of Python programming. We have assigned a new value to the first element of the first sublist of y, i.e. y[0][0] and we have
"automatically" changed the first elements of all the sublists in y, i.e. y[1][0], y[2][0], y[3][0]
The reason is that the repetition operator "* 4" creates 4 references to the list x: and so it's clear that every element of y is changed, if we apply a new value to y[0][0].
© 2011 - 2018, Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein