# Lists squares_list = [0,1,4,9,16,25] squares_list #Output: [0, 1, 4, 9, 16, 25] squares_list[0] #Output: 0 squares_list[2:4] #Output: [4, 9] - Dave asks: Why this output? ## Answer: It slices. Example: squares_list[startAt:endBefore:skip] ## Extended range notation tutorial - : http://stackoverflow.com/questions/9027862/what-does-listxy-do squares_list[0:4:25] #Output [0] squares_list[0:25:2] #Output [0, 4, 16] squares_list[-2] #Output: 16 - Returns the 2nd to last item in list. squares_list[2::4] #Output: [4] # Ranges x = range(100) # Python’s range() Function Explained: http://pythoncentral.io/pythons-range-function-explained/ # The `range' function creates a list of numbers, starting at `start', with a step of `step', up to, but not including `end'. # Source: https://www.python.org/dev/peps/pep-0204/ x[::2] #Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98] x[::3] #Output: [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99] x[10:40:6] #Output: [10, 16, 22, 28, 34] range(10) #Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Strings # TUTORIAL: Google for Education > Python > Python Strings: https://developers.google.com/edu/python/strings hw = 'Hello World!' print hw #Output: Hello World! print "hw[1:5]: ", hw[1:5] #Output: hw[1:5]: ello
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question