OnJava8-Examples/arrays/PythonLists.py

25 lines
770 B
Python
Raw Normal View History

2015-05-05 11:20:13 -07:00
#: arrays/PythonLists.py
2015-05-29 14:18:51 -07:00
# <20>2015 MindView LLC: see Copyright.txt
2015-05-05 11:20:13 -07:00
aList = [1, 2, 3, 4, 5]
print(type(aList)) # <type 'list'>
print(aList) # [1, 2, 3, 4, 5]
print(aList[4]) # 5 Basic list indexing
aList.append(6) # lists can be resized
aList += [7, 8] # Add a list to a list
print(aList) # [1, 2, 3, 4, 5, 6, 7, 8]
aSlice = aList[2:4]
print(aSlice) # [3, 4]
class MyList(list): # Inherit from list
# Define a method, 'this' pointer is explicit:
def getReversed(self):
reversed = self[:] # Copy list using slices
reversed.reverse() # Built-in list method
return reversed
list2 = MyList(aList) # No 'new' needed for object creation
print(type(list2)) # <class '__main__.MyList'>
print(list2.getReversed()) # [8, 7, 6, 5, 4, 3, 2, 1]
#:~