Assume the following list in python.
letters=[“a”,”b”,“o”,“c”,“p”]
what would be the results of these:
(a)letters[-1]
(b)letters[2:5:2]
(c)letters[2:-5:2]
(d)letters+[“x”]
(e)letters[len(letters)-2]
Answers
Answered by
4
a.) p
b.) ['o', 'p']
c.) []
d.) ['a', 'b', 'o', 'c', 'p', 'x'] (Note: Adds x in the list)
e.) c
Answered by
4
Given below are the results for a python list letters= ["a", "b", "o", "c", "p"].
Explanation:
- (a) output- p: In a python list negative indices are used to address the list backwards. Hence index -1 refers to the last element of a list.
- (b)output- ["o", "p"] : here the output is starting at index 2 and stopping at index 5 and has a step of 2 hence it has every second element starting from 2 to 5.
- (c)output- [ ] : here negative index -5 is the fifth element backwards that is at index 1. Hence, the starting index is 2 and stopping index is 1 therefore there is no output.
- (d)output- [“a”, ”b”, “o”, “c”, “p”, "x"]: here '+' is used to concatenate one list to another list. Hence, "x" is added to the list.
- (e)output- c: the function 'len()' returns an integer value equal to the length of the list. Here length of list is 5 hence the final index passed here is 3 and letters[3] is "c".
Similar questions
Hindi,
2 months ago
India Languages,
2 months ago
Hindi,
2 months ago
Hindi,
4 months ago
Environmental Sciences,
11 months ago
Math,
11 months ago
Math,
11 months ago