import sys

def printf(format, *args):
    sys.stdout.write(format % args)


aList = [ 30,20,10, 5,4,3 ]

# Access element by index.
print( aList[3] )

# "Backward" indexing starts at the list end with index -1.
print( aList[-1], aList[-2], aList[-3], aList[-4] )

# Accessing position outside the scope of the list
# causes a runtime error  "IndexError: list index out of range"
# un-comment the next line:
# print( aList[40] )

# -----------------------------------------------------------
#   LIST SLICING
# -----------------------------------------------------------
# List slice is a new list containing a contiguous part
# of the original list.
# In Matlab (Octave, SageMath, etc), the same concept
# is used on vectors and matrices.

aList1 = [10, 20, 30, 40, 50, 60, 70, 80 ]
aList2 = aList1[ 2:5 ]
print(aList2)

# Missing parameter in slice specification represents the
# beginning or the end position in the original List
aList1 = [10, 20, 30, 40, 50, 60, 70, 80 ]
aList2 = aList1[:5]
aList3 = aList1[2:]
aList4 = aList1[:]
print(aList1)
print(aList2)
print(aList3)
print(aList4)


# -----------------------------------------------------------
#   Example:  Print all unempty slices of a list.

print()
#     0   1   2   3   4   5   6
L = [60, 40, 50, 30, 20, 15, 13]
for i in range( 0, len(L) ):
    for j in range( 0, len(L)+1 ):  # j = 0,1,2,3,4,5,6
        if i < j:
            print( "L[", i, ":", j, "] = ", L[i:j], sep = "" )
# Note: sep = "" sets item separator to empty string
# Try yourself setting sep = ";", sep = "___", etc to see the effect.

# -----------------------------------------------------------
#   Example:  Print all unempty slices of a list
#             with more efficient choice of the indexes

print()

for i in range( 0, len(L) ):
    for j in range( i+1, len(L)+1 ):
        print( "L[", i, ":", j, "] = ", L[i:j], sep = "" )


# -----------------------------------------------------------
#   Example:  Print all slices of a given length of a given list

print()

L = [70, 60, 40, 50, 30, 20, 10]
sliceLen = 2
for i in range( 0, len(L)-sliceLen ):
    print( "L[", i, ":", i+sliceLen, "] = ", L[i:i+sliceLen], sep = "" )

# -----------------------------------------------------------
#   LIST SLICING General rules
# -----------------------------------------------------------

'''
Copied from
https://stackoverflow.com/questions/509211/understanding-slice-notation

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first
value that is not in the selected slice.
So, the difference between stop and start is the number of elements selected
(if step is 1, the default).

The other feature is that start or stop may be a negative number,
which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Similarly, step may be a negative number:

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for.
For example, if you ask for a[:-2] and a only contains one element,
you get an empty list instead of an error.
Sometimes you would prefer the error,
so you have to be aware that this may happen.

'''









