学习网站:
教程:
http://woodpecker.org.cn/abyteofpython_cn/chinese/
python学习
http://www.ibm.com/developerworks/cn/linux/theme/python/
http://www.w3cschool.cc/python/python-intro.html
用python写监控(glances)
https://github.com/nicolargo/glances
GAE:
http://www.appifan.com/jc/201209/35517.html
ubuntu自带python,版本为2.7
sn@sn:~/script$ python Python 2.7.5+ (default, Sep 19 2013, 13:48:49) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 3+4 7 >>> 100*12 1200 >>> help Type help() for interactive help, or help(object) for help about object. >>> print("Welcome to python") Welcome to python >>> print "Python is fun" Python is fun
########## () :函数 # :单行注释 "" :字符串 ''' ''':多行注释 >>> print(3+4*4) 19 >>> print(3 + 4 * 4) 19
######计算圆的面积 vim test.py r = eval(input("Enter a value for radius:")) area = r * r * 3.14 print "r is:",r," area is:",area //执行 python test.py
###
标识符:
(1)字母、数字、_
(2)不能是以数字开头
(3)不能使用关键字
(4)可以任意长度
#### i=k=l=3 一个变量必须先初始化 >>> a,b,c=1,2,3 >>> print a 1 >>> print b 2 >>> print c 3 ##交换ab的值 >>> a,b=b,a >>> print a 2 >>> print b 1 ##输入多个值 >>> n1,n2,n3 = eval(input("values:")) values:"1,2,3" >>> a=(n1+n2+n3)/3 >>> print(a); 2 ### / :代表不整除 //:代表整除 ### 取整 >>> value =6.7 >>> print value 6.7 >>> value =int(value) >>> print value 6 四舍五入 >>> value1 = 8.9 >>> print(round(value1)) 9.0 >>> print(value1) 8.9 abs(x):绝对值 min() max() round(x,n):小数点后保留n位 ""与''都代表字符串 改变类型 s = str(3.4) s="ss" print(s.lower()) strip for i in range(1,10): for var in [1,2,3]: function: def max(n1,n2): if n1>n2: result = n1 else: result = n2 return result large =max(3,2) print(large) ##变量 globalvar = 1 def f1(): localvar = 2 globalvar = 3 print(globalvar) print(localvar) f1() print(globalvar) #print(localvar) x = 1 def increase(): global x x = x + 1 print(x) increase() print(x) ##参数的默认值 def printArea(w =1, h =2): area = w * h print "w:", w , "h:", h ," area:" , area printArea() printArea(3,5) printArea(h=5,w=3) printArea(h=5) printArea(w=3) ### def sort(n1,n2): if n1 < n2: return n1,n2 else: return n2,n1 n1, n2 = sort(8, 4) print("n1 is",n1) print("n2 is",n2) ### >>> a = "qwert" >>> len(a) 5 >>> max(a) 'w' >>> min(a) 'e' === >>> a="HELLO" >>> print(a[1]) E >>> print(a[1:3]) EL >>> print(a[1:4]) ELL >>> print(a[:4]) HELL >>> print(a[3:]) LO >>> print(a[1:-1]) ELL >>> print(a[:]) HELLO ==== >>> s1 = "Welcome " >>> s2 = 3*s1 >>> print s2 Welcome Welcome Welcome === >>> s1 = "Welcome" >>> print("come" in s1) True >>> print("come" not in s1) False ===比较的是 >>> print("green" == "glow") False >>> print("green" != "glow") True >>> print("green" > "glow") True >>> print("green" >= "glow") True >>> print("green" < "glow") False >>> print("green" <= "glow") False >>> print("ab" <= "abc") True >>> print("az" <= "azzz") True >>> print("az" >= "azzz") False ==遍历字符串 s = "Welcome" for ch in s: print(ch) == s = "welcome to python" print(s.isalnum()) --是否有数字 print(s.islower()) --是否都是小写 print(s.isupper()) --是否都是大写 print(s.isspace()) --是否为空格 print("Welcome".isalpha()) --全部都是字母 print("2012".isdigit()) --全部都是数字 print("first number".isidentifier()) --该方法在2.7没有 print(s.endswith("thon")) --以thon结尾 print(s.startswith("good")) --以good开头 print(s.find("come")) --come开始的下标 print(s.find("become")) --没有这个则为-1 --- >>> s = "welcome to python come" >>> print(s.rfind("come")) 18 >>> print(s.find("come")) 3 s1 = s.capitalize() --s的首字母大写 print(s1) s2 = s.title() --s的所有单词首字母都大写 print(s2) s = "New England" s3 = s.lower() --s的所有字母都小写 print(s3) s4 = s.upper() --s的所有字母都大写 print(s4) s5 = s.swapcase() --s的所有首字母都小写,其它字母大写 print(s5) s6 = s.replace("England", "Haven") --把s中的"England"替换为"Haven" print(s6) print(s) --注意原来的s不会改变 print(s.rfind("o")) --s中最后一个"o"的下标 print(s.count("o")) --s中"o"的个数 s = " Welcome to Python\t" s1 = s.lstrip() --去掉首的空格 print(s1) s2 = s.rstrip() --去掉尾的空格 print(s2) s3 = s.strip() --去掉行首和尾的空格 print(s3) s = "Welcome" s1 = s.center(11) --剧中,长度11 print(s1) s2 = s.ljust(11) --居左,长度11 print(s2) s3 = s.rjust(11) --居右,长度11 print(s3) s4 = s.format(11) <<< #### list [] list [2,1,3] list ["kk","ll"] list [1,"aa"] import random random.shuffle(list) print(list) append(x: object) 在最后增加x >>> list = [2,3,4,1,4] >>> list.append(5) >>> print(list) [2, 3, 4, 1, 4, 5] >>> print(list.count(4)) ---4的下标 2 >>> list2 = [99,52] >>> list.extend(list2) >>> print(list) [2, 3, 4, 1, 4, 5, 99, 52] >>> print(list.index(4)) 2 >>> list.insert(1,25) --下标为1的位置加25 >>> print(list) [2, 25, 3, 4, 1, 4, 5, 99, 52] >>> print(list.pop(2)) --下标为2的元素 3 >>> print(list.pop()) --最后一个 52 >>> list.remove(1) --移除元素 >>> print(list) [2, 25, 4, 4, 5, 99] >>> list.reverse() --换位置,首尾 >>> print(list) [5, 4, 4, 25] >>> list.sort() --排序 >>> print(list) [4, 5, 25] >>> list = [1,2,3] >>> list2 = [3,4,5] >>> list3 = list +list2 >>> print(list3) [1, 2, 3, 3, 4, 5] >>> list = [1,2,3] >>> list2 = 3*list >>> print(list2) [1, 2, 3, 1, 2, 3, 1, 2, 3] sets 不能重复 字典: ####files def main(): outfile = open("a.txt", "w") outfile.write("Hello") outfile.write("World\n") outfile.write("Python") outfile.close main() python test7.py 出现一个a.txt的文件