List Slicing
nums[start:end]
nums[start:end:step]
List Slicing
- Numbers can be Omitted
- Numbers can be Negative
def main():
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(nums)
# simple slices
print(nums[3:7])
print(nums[5:])
print(nums[:5])
print(nums[::2])
print(nums[1::2])
# negative numbers
print(nums[-1])
print(nums[-7:-3])
print(nums[-3:])
print(nums[:-3])
print(nums[::-1])
main()
List from Range
nums = range(10)
print(nums[3:7])
range(3, 7)
List from Range
nums = list(range(10))
print(nums[3:7])
[3, 4, 5, 6]