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=[[1,2,3],
   [4,5,6],
   [7,8,9]]
>>> M
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> M[1]
[4, 5, 6]
>>> M[1][2]
6
>>> cols=[row[1] for row in M]
>>> cols
[2, 5, 8]
>>> M
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

>>> [row[1] + 1 for row in M]
[3, 6, 9]
>>> [row[1]+2 for row in M]
[4, 7, 10]
>>> [row[1] for row in M if row[1]%2==0]
[2, 8]
>>> diag=[M[i][i] for i in [0,1,2]]
>>> diag
[1, 5, 9]
>>> 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,'=>',D[key]

	
a => 1
c => 3
b => 2
>>> squares = [x **2 for x in [1,2,3,4,5]]
>>> squares
[1, 4, 9, 16, 25]
>>> squares = []
>>> for x in [1,2,3,4,5]:
	squares.append(x ** 2)

	
>>> squares
[1, 4, 9, 16, 25]

文件:
>>> 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 bytes
Hello
woeld

>>> bytes.split()
['Hello', 'woeld']

用户自定义类:
>>> class Worker:
	def __init__(self,name,pay):
		self.name=name
		self.pay=pay
	def 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.pay
6600.0000000000009

你可能感兴趣的:(C++,c,python,F#,C#)