ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
a = [1, 2, 3, 4, 5, 6, 7, 8] print(a[::3], a[2::-1], a[-3::2])
- A. [1, 4, 7] [3, 2, 1] [6, 8]
- B. [1, 4, 7] [3, 2, 1] [6]
- C. [1, 4, 7] [2, 1] [6, 8]
- D. [1, 3, 5] [3, 2, 1] [6, 8]
Correct answer: A. [1, 4, 7] [3, 2, 1] [6, 8]
a[::3] takes indices 0, 3 and 6. a[2::-1] goes from index 2 back to index 0, which gives 3, 2, 1. a[-3::2] starts at index 5 and steps by two, giving 6 and 8.
More Python Programming questions
- What is printed? python x = [i for i in range(3)] g = (i for i in range(3)) print(x == list(g), list(g), len(x))
- What is printed? python print(sorted(['b10', 'b9', 'a100'], key=lambda s: (s[0], int(s[1:]))))
- What is printed? python print(round(3.14159, 3), round(12345, -2), round(0.5), round(1.5), round(2.5, 0))
- What is printed? python a = [3, 1, 2] b = sorted(a, reverse=True)[:2] c = sorted(a)[-2:] print(b, c, sum(b) + sum(c))
- What is printed? python word = 'Level' print(word.lower() == word.lower()[::-1], word == word[::-1], word.casefold() == 'level')
- What is printed? python d = {} for w in ['b', 'a', 'b']: d[w] = d.get(w, 0) + 1 print(d, max(d, key=d.get), sorted(d.values()…