Last login: Sun Jan 5 11:39:54 on ttys002 odd% ipython Leopard libedit detected. Python 2.7.5 (default, Aug 25 2013, 00:04:04) Type "copyright", "credits" or "license" for more information. IPython 0.10 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: len("abcdef") Out[1]: 6 In [2]: len([1,2,3,4]) Out[2]: 4 In [3]: len(set([1,3,5])) Out[3]: 3 In [4]: a=[1,2,4,6,7] In [5]: tuple(a) Out[5]: (1, 2, 4, 6, 7) In [6]: str(a) Out[6]: '[1, 2, 4, 6, 7]' In [7]: list(str(a)) Out[7]: ['[', '1', ',', ' ', '2', ',', ' ', '4', ',', ' ', '6', ',', ' ', '7', ']'] In [8]: set("alphabet soup") Out[8]: set([' ', 'a', 'b', 'e', 'h', 'l', 'o', 'p', 's', 't', 'u']) In [9]: range(5) Out[9]: [0, 1, 2, 3, 4] In [10]: range(2,5) Out[10]: [2, 3, 4] In [11]: range(2,20,2) Out[11]: [2, 4, 6, 8, 10, 12, 14, 16, 18] In [12]: xrange(2,20,2) Out[12]: xrange(2, 20, 2) In [13]: xrange(100000000) KeyboardInterrupt In [13]: raw_input() hi there Out[13]: 'hi there ' In [14]: a=raw_input() this is a test In [15]: a Out[15]: 'this is a test' In [16]: a=raw_input() 10 In [17]: a Out[17]: '10' In [18]: input() 10 Out[18]: 10 In [19]: 10 Out[19]: 10 In [20]: input() 10+10*100 Out[20]: 1010 In [21]: max([1,3,2,5,9,7]) Out[21]: 9 In [22]: min([1,3,2,5,9,7]) Out[22]: 1 In [23]: max("alphabet") Out[23]: 't' In [24]: a=(1,3,7,6,5,2) In [25]: a Out[25]: (1, 3, 7, 6, 5, 2) In [26]: b=list(a) In [27]: b Out[27]: [1, 3, 7, 6, 5, 2] In [28]: b.sort() In [29]: b Out[29]: [1, 2, 3, 5, 6, 7] In [30]: a.sort() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /Users/stevel/ in () AttributeError: 'tuple' object has no attribute 'sort' In [31]: sorted(a) Out[31]: [1, 2, 3, 5, 6, 7] In [32]: reversed(sorted(a)) Out[32]: In [33]: list(reversed(sorted(a))) Out[33]: [7, 6, 5, 3, 2, 1] In [34]: print "this is a test" -------> print("this is a test") this is a test In [35]: print "this is a test",1,3,5,a -------> print("this is a test",1,3,5,a) ('this is a test', 1, 3, 5, (1, 3, 7, 6, 5, 2)) In [36]: print "this is a test",1,3,5,a -------> print("this is a test",1,3,5,a) ('this is a test', 1, 3, 5, (1, 3, 7, 6, 5, 2)) In [37]: ^D Do you really want to exit ([y]/n)? odd% python Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print "this is a test",1,3,5 this is a test 1 3 5 >>> a="this is a test" >>> a.upper() 'THIS IS A TEST' >>> a.lower() 'this is a test' >>> a.title() 'This Is A Test' >>> a="this is a test 1 2 3" >>> a.title() 'This Is A Test 1 2 3' >>> a 'this is a test 1 2 3' >>> a.count("t") 3 >>> a.count("is") 2 >>> a.find("is") 2 >>> a[2] 'i' >>> a[2:3] 'i' >>> a[2:4] 'is' >>> a.rfind("is") 5 >>> a.replace("is","was") 'thwas was a test 1 2 3' >>> a.replace("is","") 'th a test 1 2 3' >>> a=str([1,3,5,7,9]) >>> a '[1, 3, 5, 7, 9]' >>> a.split(",") ['[1', ' 3', ' 5', ' 7', ' 9]'] >>> a[1:-1].split(",") ['1', ' 3', ' 5', ' 7', ' 9'] >>> a="""this is ... a multiline ... test ... ... """ >>> a 'this is\na multiline\n test\n\n' >>> print a this is a multiline test >>> a.split() ['this', 'is', 'a', 'multiline', 'test'] >>> a.split(" ") ['this', 'is\na', 'multiline\n', '', '', '', 'test\n\n'] >>> a.split() ['this', 'is', 'a', 'multiline', 'test'] >>> b=a[1:-1].split(",") >>> b ['his is\na multiline\n test\n'] >>> a=str([1,3,5,7,9]) >>> b ['his is\na multiline\n test\n'] >>> b=a[1:-1].split(",") >>> b ['1', ' 3', ' 5', ' 7', ' 9'] >>> print b ['1', ' 3', ' 5', ' 7', ' 9'] >>> print ",".join(b) 1, 3, 5, 7, 9 >>> print ".".join(b) 1. 3. 5. 7. 9 >>> "is" in "this is a test" True >>> "if" in "this is a test" False >>> "this is a test".find("xxxx") -1 >>> a=range(10) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> a.append("bbb") >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'bbb'] >>> a=range(5) >>> b=range(8) >>> a [0, 1, 2, 3, 4] >>> b [0, 1, 2, 3, 4, 5, 6, 7] >>> a+b [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7] >>> a.extend(b) >>> a [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7] >>> b [0, 1, 2, 3, 4, 5, 6, 7] >>> a [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7] >>> del a[7] >>> a [0, 1, 2, 3, 4, 0, 1, 3, 4, 5, 6, 7] >>> a.remove(1) >>> a [0, 2, 3, 4, 0, 1, 3, 4, 5, 6, 7] >>> a.remove(1) >>> a [0, 2, 3, 4, 0, 3, 4, 5, 6, 7] >>> a.remove(1) Traceback (most recent call last): File "", line 1, in ValueError: list.remove(x): x not in list >>> a [0, 2, 3, 4, 0, 3, 4, 5, 6, 7] >>> a.index[6] Traceback (most recent call last): File "", line 1, in TypeError: 'builtin_function_or_method' object has no attribute '__getitem__' >>> a.index(6) 8 >>> del a[a.index(6)] KeyboardInterrupt >>> a.remove(6) KeyboardInterrupt >>> a={1:2,2:3,"a":"x"} >>> a {'a': 'x', 1: 2, 2: 3} >>> a[1] 2 >>> a.keys() ['a', 1, 2] >>> a.values() ['x', 2, 3] >>> a.items() [('a', 'x'), (1, 2), (2, 3)] >>> a.items()[2] (2, 3) >>> a.items()[2][0] 2 >>> a.items()[2][1] 3 >>> a.has_key(5) False >>> a.has_key(1) True >>> 1 in a.keys() True >>> a["x"]=10 >>> a {'a': 'x', 1: 2, 2: 3, 'x': 10} >>> a[4]=0 >>> a[4]=a[4]+1 >>> a[4]=a[4]+1 >>> a[4]=a[4]+1 >>> a[4] 3 >>> a[4]=a[4]+1 >>> a[4]=a[4]+1 >>> a[4] 5 >>> a.set_default(5,0) Traceback (most recent call last): File "", line 1, in AttributeError: 'dict' object has no attribute 'set_default' >>> a.get_default(5,0) Traceback (most recent call last): File "", line 1, in AttributeError: 'dict' object has no attribute 'get_default' >>> a.setdefault(5,0) 0 >>> a {'a': 'x', 1: 2, 2: 3, 4: 5, 5: 0, 'x': 10} >>> a[5]=a[5]+1 >>> a {'a': 'x', 1: 2, 2: 3, 4: 5, 5: 1, 'x': 10} >>> a[5] 1 >>> a.setdefault(5,0) 1 >>> a=set((1,3,5,7,9)) >>> b=set((1,4,6,7,10)) >>> a set([1, 3, 9, 5, 7]) >>> b set([1, 10, 4, 6, 7]) >>> a.union(b) set([1, 3, 4, 5, 6, 7, 9, 10]) >>> b.union(a) set([1, 3, 4, 5, 6, 7, 9, 10]) >>> a.intersection(b) set([1, 7]) >>> a.difference(b) set([9, 3, 5]) >>> b.difference(a) set([10, 4, 6]) >>> a set([1, 3, 9, 5, 7]) >>> a.remove(9) >>> a set([1, 3, 5, 7]) >>> a.remove(9) Traceback (most recent call last): File "", line 1, in KeyError: 9 >>> a.discard(5) >>> a set([1, 3, 7]) >>> a.discard(5) >>> ^D odd% odd% vi x.py odd% vi y.py odd% cat y.py import math print math.sin(10.0),math.sin(20.0) odd% python y.py -0.544021110889 0.912945250728 odd% vi y.py odd% python Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import math >>> >>> math.sin(10.0) -0.5440211108893699 >>> math.sin(20.0) 0.9129452507276277 >>> ^D odd% python y.py odd% odd% !vi vi y.py odd% python y.py -0.544021110889 0.912945250728 odd% y.py zsh: permission denied: y.py odd% vi y.py odd% y.py zsh: permission denied: y.py odd% chmod a+x y.py odd% y.py -0.544021110889 0.912945250728 odd% idle odd% cat z.py from math import * print sin(10.0) print cos(20.0) odd% a=[1,2,3,4,5] odd% python Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> a=[1,2,3,4,5] >>> for i in a: ... print i,i*2 ... 1 2 2 4 3 6 4 8 5 10 >>> ^D odd% idle