Created
January 9, 2017 01:43
-
-
Save akhalsa/52c2bdcb5750bf849d571495b8614b67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nums = range(5) # range is a built-in function that creates a list of integers | |
print nums # Prints "[0, 1, 2, 3, 4]" | |
print nums[2:4] # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]" | |
print nums[2:] # Get a slice from index 2 to the end; prints "[2, 3, 4]" | |
print nums[:2] # Get a slice from the start to index 2 (exclusive); prints "[0, 1]" | |
print nums[:] # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]" | |
print nums[:-1] # Slice indices can be negative; prints ["0, 1, 2, 3]" | |
nums[2:4] = [8, 9] # Assign a new sublist to a slice | |
print nums # Prints "[0, 1, 8, 9, 4]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment