0

I have 3 years of PHP experience and currently trying to catch up with Python on sololearn. The first question in Intermediate Python that checks my Python knowledge goes like this:

n = [2, 4, 6, 8]
res = 1
for x in n[1:3]:
  res *= x

I am confused why the result is 24, my logic is that n[1:3] means n[1] * n[2] * n[3] so 4 * 6 * 8.

I searched on google about for loops in Python but i only find stuff about the range() function, couldn't find an explanation for the : operator

aWWW
  • 31
  • 2

1 Answers1

1

It does n[1] * n[2] = 4 *6 = 24. Read more about list slicing: https://railsware.com/blog/python-for-machine-learning-indexing-and-slicing-for-lists-tuples-strings-and-other-sequential-types/

Seiteros
  • 191
  • 1
  • 2
  • 6