2057 发表于 2013-1-19 04:15:01

LearningPython——No.1

Python的传统运行执行模式:录入的源代码转换为字节码,之后字节码在Python虚拟机中运行。代码自动被编译,之后再解释。
Python程序:
1.程序由模块构成
2.模块包含语句
3.语句包含表达式
4.表达式建立并处理对象
使用帮助:
dir(S)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
例子:
M=[,   ,   ]>>> M[, , ]>>> M>>> M6>>> cols= for row in M]>>> cols>>> M[, , ]>>> + 1 for row in M]>>> +2 for row in M]>>> for row in M if row%2==0]>>> diag= for i in ]>>> diag>>> D={'a':1,'b':2,'c':3}>>> D{'a': 1, 'c': 3, 'b': 2}>>> Ks =D.keys()>>> Ks['a', 'c', 'b']>>> for key in Ks:print key,'=>',Da => 1c => 3b => 2>>> squares = ]>>> squares>>> squares = []>>> for x in :squares.append(x ** 2)>>> squares
文件:
>>> f=open('data.txt','w')>>> f.write ('Hello\n')>>> f.write('woeld\n')>>> f.close()>>> f=open('data.txt')>>> bytes=f.read()>>> bytes'Hello\nwoeld\n'>>> print bytesHellowoeld>>> bytes.split()['Hello', 'woeld']
用户自定义类:
>>> class Worker:def __init__(self,name,pay):self.name=nameself.pay=paydef lastName(self):return self.name.split()[-1]def giveRaise(self,percent):self.pay *=(1.0+percent)>>> bob=Worker('Bob Smith',5000)>>> sue=Worker('Sue Jones',6000)>>> bob.lastName()'Smith'>>> sue.lastName()'Jones'>>> sue.giveRaise(.10)>>> sue.pay6600.0000000000009
页: [1]
查看完整版本: LearningPython——No.1