Last login: Mon Mar 22 09:58:59 on console gwlan3-83:~ Jan$ python Python 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> exit() gwlan3-83:~ Jan$ ipython -colors LightBG Python 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32) 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]: x=2 In [2]: x Out[2]: 2 In [3]: type(x) Out[3]: In [4]: x=3.14159 In [5]: type(x) Out[5]: In [6]: 2+3 Out[6]: 5 In [7]: x="Hello world!" In [8]: x='Hello world!" ------------------------------------------------------------ File "", line 1 x='Hello world!" ^ SyntaxError: EOL while scanning string literal In [9]: x='Hello world!' In [10]: x="I'm Jan" In [11]: # This is a comment. In [12]: s="""Hello ....: ....: ....: world""" In [13]: s Out[13]: 'Hello\n\n\nworld' In [14]: print s -------> print(s) Hello world In [15]: # Booleans In [16]: True Out[16]: True In [17]: False Out[17]: False In [18]: 2==2 Out[18]: True In [19]: 2==3 Out[19]: False In [20]: 2==2 or 2==3 Out[20]: True In [21]: True or False and not True Out[21]: True In [22]: 2<3<=4>-1 Out[22]: True In [23]: # lists In [24]: L=[1,2,3,4] In [25]: L Out[25]: [1, 2, 3, 4] In [26]: M=[1,2,3.5,"hi", [1,2]] In [27]: L[0] Out[27]: 1 In [28]: L[-1] Out[28]: 4 In [29]: L[1:3] Out[29]: [2, 3] In [30]: L[1:-1] Out[30]: [2, 3] In [31]: L[2]=5 In [32]: L Out[32]: [1, 2, 5, 4] In [33]: L.append(6) In [34]: L Out[34]: [1, 2, 5, 4, 6] In [35]: L.extend([7,8]) In [36]: L Out[36]: [1, 2, 5, 4, 6, 7, 8] In [37]: del L[2] In [38]: L Out[38]: [1, 2, 4, 6, 7, 8] In [39]: L.reverse() In [40]: L Out[40]: [8, 7, 6, 4, 2, 1] In [41]: L.sort() In [42]: L Out[42]: [1, 2, 4, 6, 7, 8] In [43]: L.sort(reverse=True) In [44]: L Out[44]: [8, 7, 6, 4, 2, 1] In [45]: # dictionaries In [46]: D={'Mozart': 1757, 'Schubert': 1797} In [47]: D Out[47]: {'Mozart': 1757, 'Schubert': 1797} In [48]: D['Mozart'] Out[48]: 1757 In [49]: D['Beethoven']=1771 In [50]: D.update({'Beethoven': 1770}) In [51]: D Out[51]: {'Beethoven': 1770, 'Mozart': 1757, 'Schubert': 1797} In [52]: 'Mozart' in D Out[52]: True In [53]: 'Einstein' in D Out[53]: False In [54]: D.keys() Out[54]: ['Schubert', 'Beethoven', 'Mozart'] In [55]: D.values() Out[55]: [1797, 1770, 1757] In [56]: # sets In [57]: s=set([1,2,3,4]) In [58]: 2 in s Out[58]: True In [59]: 5 in s Out[59]: False In [60]: s.add(5) In [61]: s Out[61]: set([1, 2, 3, 4, 5]) In [62]: s2=set([3,4,5]) In [63]: s & s2 # intersection Out[63]: set([3, 4, 5]) In [64]: s | s2 # union Out[64]: set([1, 2, 3, 4, 5]) In [65]: s - s2 # difference Out[65]: set([1, 2]) In [66]: s ^ s2 # symmetric difference Out[66]: set([1, 2]) In [67]: n = 10 In [68]: if n == 12: ....: n = 20 ....: print 'n was 12' ....: elif n == 13: ....: print 'n was 13' ....: els: ------------------------------------------------------------ File "", line 6 els: ^ SyntaxError: invalid syntax In [69]: if n == 12: ....: n = 20 ....: print 'n was 12' ....: elif n == 13: ....: print 'n was 13' ....: else: ....: print 'something else' ....: something else In [70]: n Out[70]: 10 In [71]: while n > 2: ....: print n ....: n -= 2 ....: 10 8 6 4 In [72]: for k in [1,2,3,4]: ....: print k ....: 1 2 3 4 In [73]: range(5) Out[73]: [0, 1, 2, 3, 4] In [74]: range(1,5) Out[74]: [1, 2, 3, 4] In [75]: for n in reversed(range(3,11,2)): ....: print n ....: 9 7 5 3 In [76]: # loops In [77]: D.items() Out[77]: [('Schubert', 1797), ('Beethoven', 1770), ('Mozart', 1757)] In [78]: for key, value in D.items(): ....: print key ....: print value ....: Schubert 1797 Beethoven 1770 Mozart 1757 In [79]: for key, value in D.items(): ....: print '%s: %d' % (key, value) ....: Schubert: 1797 Beethoven: 1770 Mozart: 1757 In [80]: (1,2) Out[80]: (1, 2) In [81]: p=(1,2) In [82]: p[0] Out[82]: 1 In [83]: p[0]=3 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/Jan/ in () TypeError: 'tuple' object does not support item assignment In [84]: a, b = (1, 2) In [85]: a Out[85]: 1 In [86]: b Out[86]: 2 In [87]: a, b = 1, 2 In [88]: a Out[88]: 1 In [89]: b Out[89]: 2 In [90]: a, b = b, a In [91]: a Out[91]: 2 In [92]: b Out[92]: 1 In [93]: # list comprehensions In [94]: [x**2 for x in range(10)] Out[94]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] In [95]: [x**2 for x in range(10) if x % 2 == 0] Out[95]: [0, 4, 16, 36, 64] In [96]: values=[[0,1],[1,1]] In [97]: 'a'.join(['b','c','d']) Out[97]: 'bacad' In [98]: csvtext='\n'.join(';'.join(str(value) for value in line) for line in values) In [99]: csvtext Out[99]: '0;1\n1;1' In [100]: print csvtext --------> print(csvtext) 0;1 1;1 In [101]: # in csv file: , instead of ; In [102]: file=open('file.csv', 'w') # write access In [103]: file. file.__class__ file.__setattr__ file.newlines file.__delattr__ file.__sizeof__ file.next file.__doc__ file.__str__ file.read file.__enter__ file.__subclasshook__ file.readinto file.__exit__ file.close file.readline file.__format__ file.closed file.readlines file.__getattribute__ file.csv file.seek file.__hash__ file.encoding file.softspace file.__init__ file.errors file.tell file.__iter__ file.fileno file.truncate file.__new__ file.flush file.write file.__reduce__ file.isatty file.writelines file.__reduce_ex__ file.mode file.xreadlines file.__repr__ file.name In [103]: help(file) In [104]: file.write(csvtext) In [105]: file.close() In [106]: # functions In [107]: def f(n): .....: return n**2 .....: In [108]: f(3) Out[108]: 9 In [109]: file. file.__class__ file.__setattr__ file.newlines file.__delattr__ file.__sizeof__ file.next file.__doc__ file.__str__ file.read file.__enter__ file.__subclasshook__ file.readinto file.__exit__ file.close file.readline file.__format__ file.closed file.readlines file.__getattribute__ file.csv file.seek file.__hash__ file.encoding file.softspace file.__init__ file.errors file.tell file.__iter__ file.fileno file.truncate file.__new__ file.flush file.write file.__reduce__ file.isatty file.writelines file.__reduce_ex__ file.mode file.xreadlines file.__repr__ file.name In [109]: import re In [110]: re. re.DEBUG re.__doc__ re._expand re.DOTALL re.__file__ re._pattern_type re.I re.__format__ re._pickle re.IGNORECASE re.__getattribute__ re._subx re.L re.__hash__ re.compile re.LOCALE re.__init__ re.copy_reg re.M re.__name__ re.error re.MULTILINE re.__new__ re.escape re.S re.__package__ re.findall re.Scanner re.__reduce__ re.finditer re.T re.__reduce_ex__ re.match re.TEMPLATE re.__repr__ re.purge re.U re.__setattr__ re.search re.UNICODE re.__sizeof__ re.split re.VERBOSE re.__str__ re.sre_compile re.X re.__subclasshook__ re.sre_parse re._MAXCACHE re.__version__ re.sub re.__all__ re._alphanum re.subn re.__builtins__ re._cache re.sys re.__class__ re._cache_repl re.template re.__delattr__ re._compile In [110]: from urllib2 import urlopen In [111]: urlo --------------------------------------------------------------------------- NameError Traceback (most recent call last) /Users/Jan/ in () NameError: name 'urlo' is not defined In [112]: urlfile=urlopen('http://www.google.at') In [113]: urlfile Out[113]: > In [114]: content=urlfile.read() In [115]: content Out[115]: 'Google



 
  Erweiterte Suche
  Sprachtools
Suche:


Werben mit Google - Unternehmensangebote - \xdcber Google - Google.com in English

©2010 - Datenschutz

' In [116]: print content --------> print(content) Google



 
  Erweiterte Suche
  Sprachtools
Suche:


Werben mit Google - Unternehmensangebote - ?ber Google - Google.com in English

©2010 - Datenschutz

In [117]: len(content) Out[117]: 6926 In [118]: from xml.dom. xml.dom.NodeFilter xml.dom.minicompat xml.dom.xmlbuilder xml.dom.domreg xml.dom.minidom xml.dom.expatbuilder xml.dom.pulldom In [118]: from xml.dom.minidom import parseString In [119]: xmltext='c' In [120]: doc=parseString(xmltext) In [121]: doc Out[121]: In [122]: doc. Display all 100 possibilities? (y or n) doc.ATTRIBUTE_NODE doc.createCDATASection doc.CDATA_SECTION_NODE doc.createComment doc.COMMENT_NODE doc.createDocumentFragment doc.DOCUMENT_FRAGMENT_NODE doc.createElement doc.DOCUMENT_NODE doc.createElementNS doc.DOCUMENT_TYPE_NODE doc.createProcessingInstruction doc.ELEMENT_NODE doc.createTextNode doc.ENTITY_NODE doc.doctype doc.ENTITY_REFERENCE_NODE doc.documentElement doc.NOTATION_NODE doc.documentURI doc.PROCESSING_INSTRUCTION_NODE doc.encoding doc.TEXT_NODE doc.errorHandler doc.__class__ doc.firstChild doc.__doc__ doc.getElementById doc.__init__ doc.getElementsByTagName doc.__module__ doc.getElementsByTagNameNS doc.__nonzero__ doc.getInterface doc._call_user_data_handler doc.getUserData doc._child_node_types doc.hasChildNodes doc._create_entity doc.implementation doc._create_notation doc.importNode doc._elem_info doc.insertBefore doc._get_actualEncoding doc.isSameNode doc._get_async doc.isSupported doc._get_childNodes doc.lastChild doc._get_doctype doc.load doc._get_documentElement doc.loadXML doc._get_documentURI doc.localName doc._get_elem_info doc.namespaceURI doc._get_encoding doc.nextSibling doc._get_errorHandler doc.nodeName doc._get_firstChild doc.nodeType doc._get_lastChild doc.nodeValue doc._get_localName doc.normalize doc._get_standalone doc.ownerDocument doc._get_strictErrorChecking doc.parentNode doc._get_version doc.prefix doc._id_cache doc.previousSibling doc._id_search_stack doc.removeChild doc._magic_id_count doc.renameNode doc._set_async doc.replaceChild doc.abort doc.saveXML doc.actualEncoding doc.setUserData doc.appendChild doc.standalone doc.async doc.strictErrorChecking doc.attributes doc.toprettyxml doc.childNodes doc.toxml doc.cloneNode doc.unlink doc.createAttribute doc.version doc.createAttributeNS doc.writexml In [122]: doc. ------------------------------------------------------------ File "", line 1 doc. ^ SyntaxError: invalid syntax In [123]: xmltext Out[123]: 'c' In [124]: doc.getElementsByTagName('tag') Out[124]: [] In [125]: tag=doc.getElementsByTagName('tag')[0] In [126]: tag Out[126]: In [127]: tag.getAttribute('a') Out[127]: u'b' In [128]: tag.chil --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /Users/Jan/ in () AttributeError: Element instance has no attribute 'chil' In [129]: tag.firstChild Out[129]: In [130]: tag.firstChild.nodeValue Out[130]: u'c' In [131]: json_data='{"list":[1,2]}' In [132]: import json In [133]: json.loads(json_data) Out[133]: {u'list': [1, 2]} In [134]: json.loads(json_data)['list'] Out[134]: [1, 2] In [135]: json.loads(json_data)['list'][0] Out[135]: 1 In [136]: import networkx as nx In [137]: G=nx.Graph() In [138]: G.add_node(1) In [139]: G.add_node('me') In [140]: G.add_node('you') In [141]: G.add_edge('me','you') In [142]: G.add_edge(2,3) In [143]: G.nodes() Out[143]: ['me', 1, 'you', 3, 2] In [144]: G.edges() Out[144]: [('me', 'you'), (3, 2)] In [145]: nx.is_connected(G) Out[145]: False In [146]: nx.connected_components(G) Out[146]: [['me', 'you'], [2, 3], [1]] In [147]: import matplotlib.pyplot as plt In [148]: plt.figure() Out[148]: In [149]: nx.draw(G) In [150]: plt.savefig('plot.png') In [151]: plt.figure() Out[151]: In [152]: pos = nx.spring_layout(G) In [153]: pos Out[153]: {1: array([ 0. , 0.43226852]), 2: array([ 0.86005919, 0.04649485]), 3: array([ 0.7840669, 0. ]), 'me': array([ 0.6256048, 1. ]), 'you': array([ 0.61016559, 0.90822988])} In [154]: help(nx.spring_layout) In [155]: nx.draw_networkx_edges(G, pos) Out[155]: In [156]: nx.draw_networkx_nodes(G, pos, [1,2,3], node_color='r') Out[156]: In [157]: nx.draw_n nx.draw_networkx nx.draw_networkx_labels nx.draw_networkx_edge_labels nx.draw_networkx_nodes nx.draw_networkx_edges In [157]: nx.draw_networkx_nodes(G, pos, ["me", "you"], node_color='b') Out[157]: In [158]: nx.draw_networkx_labels(G, pos) Out[158]: {1: , 2: , 3: , 'me': , 'you': } In [159]: nx.savefig('plot2.png') --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /Users/Jan/ in () AttributeError: 'module' object has no attribute 'savefig' In [160]: plt.savefig('plot2.png') In [161]: