#!/usr/bin/python -tt # Copyright 2010 Google Inc. # Licensed under the Apache License, Version 2.0 # http://www.apache.org/licenses/LICENSE-2.0 # Google's Python Class # http://code.google.com/edu/languages/google-python-class/ # Lecture List def main(): general() meths() def general(): colors = ['red', 'blue', 'green'] print colors[0] ## red print colors[2] ## green print len(colors) ## 3 z = [1,2,3] y = [4,5,6] x = z + y print z x = y x[0] = 7 print y[0] squares = [1, 4, 9, 16] sum = 0 for num in squares: sum += num print sum list = ['larry', 'curly', 'moe'] if 'curly' in list: print 'yay' ## print the numbers from 0 through 99 for i in range(100): print i ## Access every 3rd element in a list a = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0] i = 0 while i < len(a): print a[i] i = i + 3 def meths(): list = ['larry', 'curly', 'moe'] list.append('shemp') ## append elem at end list.insert(0, 'xxx') ## insert elem at index 0 list.extend(['yyy', 'zzz']) ## add list of elems at end print list ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz'] print list.index('curly') ## 2 list.remove('curly') ## search and remove that element print list.pop(1) ## removes and returns 'larry' print list ## ['xxx', 'moe', 'shemp', 'yyy', 'zzz'] list = ['a', 'b', 'c', 'd'] print list[1:-1] ## ['b', 'c'] list[0:2] = 'z' ## replace ['a', 'b'] with ['z'] print list ## ['z', 'c', 'd'] # Standard boilerplate to call the main() function. if __name__ == '__main__': main()
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