ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
s = list(range(10)) print(s[8:2:-3], s[::-4], s[-3:])
- A. [8, 5] [9, 5, 1] [8, 9]
- B. [8, 5] [9, 5, 1] [7, 8, 9]
- C. [8, 5, 2] [9, 5, 1] [7, 8, 9]
- D. [8, 5] [0, 4, 8] [7, 8, 9]
Correct answer: B. [8, 5] [9, 5, 1] [7, 8, 9]
s[8:2:-3] visits indices 8 and 5, and stops before index 2. s[::-4] starts at 9 and steps back by 4 (9, 5, 1). s[-3:] is the last three items.
More Python Programming questions
- What is printed? python a = [1, 2, 3, 4, 5] a[1:3] = [9] 4 print(a)
- What is printed? python print(repr('abc'[5:]), repr('abc'[-10:2]), [1, 2][3:0:-1])
- What is printed? python s = 'banana' print(s.find('an', 2), s.rfind('an'), s.find('x'), s.index('na', 3))
- What is printed? python print('aaaa'.count('aa'), 'aaaa'.replace('aa', 'b'), len('aaaaa'.split('aa')))
- What is printed? python x = 3.14159 print(f"{x:08.3f}|{x:>8.2e}|{42:#x}|{'ab'!r:>6}")
- What is printed? python print(f"{1234567.891:,.2f}", f"{255:b}", f"{-5:+d}")