What does each of the following expressions evaluate to?
Suppose that L is the list
("These". ("are", "a"), ["few", "words"], "that", "we", "will", "use")
a) Len(L) b) L [3:4]+L[1:2] c) "few" in L[2:3]
d) L[2][1:] e) L[1] + L [2]
Answers
Correction:
L= ("These", ("are", "a"), ["few", "words"], "that", "we", "will", "use")
Note: L is not a list it is a tuple here.
Lists is defined with square brackets and not parenthesis. Parenthesis is used to define tuples in python.
Answer (with L as tuple) :
a) 7
b) ('that', ('are', 'a'))
c) False
d) ['words']
e) error.
Explanation:
a) len(a) returns length of the tuple.
b) Returns the
c) Checks if in tuple from index 2. Which is not the case here "few" is not on index 2 but is an element of element on index 2.
d) Returns 2nd element's elements from index 1 onwards.
e) Index 1 has a tuple and index 2 has a list and concatenation not possible in tuples and lists so this generates an error.
Answer (with L as lists) :
Note: I took L as ["These". ("are", "a"), ["few", "words"], "that", "we", "will", "use"]
These outputs are same as that in tuples (but that does not make tuples and list same, make sure not to mix em up).