1.python 環境搭建 http://www.w3cschool.cc/python/python-install.html
2.python輸入輸出
print 'The quick brown fox', 'jumps over', 'the lazy dog'#輸出結果為 The quick brown fox jumps over the lazy dog (依次打印每個字符串,遇到‘,’打印空格)
輸入前面若有空格,必須用tab
age=0; print'His age is: ' ,age
或者print'His age is: s%' %age
如果輸出多個 print'Their ages are :%s %s'%(age,age)
raw_input輸入默認為字符串,如果想輸入數字,運用強制類型轉換
data=int(raw_input('please input:')) data=raw_input('please input:')
3.list與touple
list:(可以改變,可以插入,刪除,)(list中存的數據類型可以不一樣)
classmates = ['Michael', 'Bob', 'Tracy']; classmates.append('job') #插入job至末尾 classmates.insert(1,'Tom') #classmates變為 classmates = ['Michael','Tom', 'Bob', 'Tracy'] calssmates.pop() #刪除末尾的元素 calssmates.pop(i) #刪除第i-1個元素 classmates.sort() #排序
touple:#(一旦生成就不會改變) t = (1, 2)
4.條件判斷
elif
是else if
的縮寫,
age = 3 if age >= 18: #記得: print 'your age is', age print 'adult' else: #記得: print 'your age is', age print 'teenager'
5.循環
names = ['Michael', 'Bob', 'Tracy'] for name in names: print name sum = 0 for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: sum = sum + x print sum # range(5) #生成 [0, 1, 2, 3, 4]
6.dict和set
6.1 dict使用key-value存值(注:dict的key值是不可變對象!!)
d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} d['Michael']#運行結果為 95 d.pop('Bob') #刪除‘Bob’ d.get('Mack',-1) #如果key值不存在,返回-1,默認的返回None,此時Python的交互式命令行不顯示結果
6.2 set(和dict用法相似,也存key值,但是沒有value值,且key值不能重復)
s = set([1, 1, 2, 2, 3, 3]) #運行結果為 set([1,2,3]) s.add(4) #運行結果為 set([1,2,3,4]) s.remove(3) #運行結果為 set([1,2])