Skip to content

Instantly share code, notes, and snippets.

@garybradski
Created September 9, 2017 00:48
Show Gist options
  • Save garybradski/539209d0332ecff747891e0677736419 to your computer and use it in GitHub Desktop.
Save garybradski/539209d0332ecff747891e0677736419 to your computer and use it in GitHub Desktop.
python_indexing_slices.py
my_tuple = ('p','r','o','g','r','a','m','i','z')
print("og forward:")
print("og: ",my_tuple[2:4])
print("og: ",my_tuple[-7:4])
print("og: ",my_tuple[-7:-5])
print("og: ",my_tuple[2:-5])
print("og backwards (go):")
print("go: ",my_tuple[3:1:-1])
print("go: ",my_tuple[-6:1:-1])
print("go: ",my_tuple[-6:-8:-1])
print("go: ",my_tuple[3:-8:-1])
print("orm foward:")
print("orm: ",my_tuple[2:7:2])
print("orm: ",my_tuple[-7:7:2])
print("orm: ",my_tuple[-7:-2:2])
print("orm: ",my_tuple[2:-2:2])
print("orm back (mro):")
print("mro: ",my_tuple[6:1:-2])
print("mro: ",my_tuple[6:-8:-2])
print("mro: ",my_tuple[-3:1:-2])
print("mro: ",my_tuple[-3:-8:-2])
print("===================")
@garybradski
Copy link
Author

my_tuple = ('p','r','o','g','r','a','m','i','z')

print("og forward:")
og forward:
print("og: ",my_tuple[2:4])
('og: ', ('o', 'g'))
print("og: ",my_tuple[-7:4])
('og: ', ('o', 'g'))
print("og: ",my_tuple[-7:-5])
('og: ', ('o', 'g'))
print("og: ",my_tuple[2:-5])
('og: ', ('o', 'g'))

print("og backwards (go):")
og backwards (go):
print("go: ",my_tuple[3:1:-1])
('go: ', ('g', 'o'))
print("go: ",my_tuple[-6:1:-1])
('go: ', ('g', 'o'))
print("go: ",my_tuple[-6:-8:-1])
('go: ', ('g', 'o'))
print("go: ",my_tuple[3:-8:-1])
('go: ', ('g', 'o'))

print("orm foward:")
orm foward:
print("orm: ",my_tuple[2:7:2])
('orm: ', ('o', 'r', 'm'))
print("orm: ",my_tuple[-7:7:2])
('orm: ', ('o', 'r', 'm'))
print("orm: ",my_tuple[-7:-2:2])
('orm: ', ('o', 'r', 'm'))
print("orm: ",my_tuple[2:-2:2])
('orm: ', ('o', 'r', 'm'))

print("orm back (mro):")
orm back (mro):
print("mro: ",my_tuple[6:1:-2])
('mro: ', ('m', 'r', 'o'))
print("mro: ",my_tuple[6:-8:-2])
('mro: ', ('m', 'r', 'o'))
print("mro: ",my_tuple[-3:1:-2])
('mro: ', ('m', 'r', 'o'))
print("mro: ",my_tuple[-3:-8:-2])
('mro: ', ('m', 'r', 'o'))
print("===================")
===================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment