Page 290 - Computer Science Class 11 Without Functions
P. 290
The slice [1:7:3] yields a list, comprising the elements beginning at index 1 and up to index 7 (excluding the
element at index 7), in steps of size 3. Thus, the expression colors[1:7:3] yields a list comprising the elements
colors[1] and colors[4]:
>>> colors[1:7:3]
['green', 'orange']
C T 01 1. lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
For each of the following lists, give a slice that would extract it from the list lst.
a. ['c', 'd', 'e', 'f']
b. ['a', 'b', 'c']
c. ['e', 'f', 'g']
d. [a, d]
e. [b, d]
f. Second to last element of the list
2. Suppose we have the following assignment statements:
s1 = 'I wish to score highest marks.'
s2 = ['I', 'wish', 'to', 'score', 'highest', 'marks', '.']
Which of the following statements will execute without any errors?
a. s2[0]= 'You'
b. s1[29]='!'
c. s2[6]='!'
12.1.4 Membership Operator in
The membership operator in is used to check the membership of an element in a list. It returns True if the specific
element being looked for is present in the list, otherwise, False.
Example:
>>> colors = ['red', 'green', 'blue', 'yellow', 'orange', 'white', 'black']
>>> 'yellow' in colors
True
>>> 'pink' in colors
False
12.1.5 Membership Operator not in
The membership operator not in is used to check the membership of an element in a list. It returns True if the
specific element being looked for is not present in the list, otherwise, False.
Example:
>>> 'pink' not in colors
True
12.2 List Assignment
288 Touchpad Computer Science-XI

