Python Tool Visual Studio簡單使用,pythonstudio
由於一直在做.NET的開發,一直用的IDE是VS系列的,所以想用VS也能開發Python,剛好微軟提供一個外掛程式PTVS(Python Tool Visual Studio)專門應用於Python開發的,但是很可惜沒有提供中文版本的,只有英文版本的,對於英語很差的我來說是一種考驗呀!!!!!!!!!!!
Sort與Sorted
無意中用PTVS外掛程式做列表的排序的時候發現,用Python自己帶的SCMD命令Sort或者Sorted排序都行,但是在外掛程式中Sort就不行
貌似必須使用Sorted使用才行,這真是百思不得。
s=[1,2,3,4,15,1,1]print(sorted(s))
格式化日期的使用
使用 time 模組的 strftime 方法來格式化日期
import time# 格式化成2016-03-20 11:45:39形式print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 格式化成Sat Mar 28 22:24:24 2016形式print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) # 將格式字串轉換為時間戳記a = "Sat Mar 28 22:24:24 2016"print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
擷取日曆
import calendarcal=calendar.month(2016,7)print(cal)
對IO的一些操作
讀取檔案
>>>f=open("G:\\Python\\txt1.txt","r")>>>f.read()讀出內容>>>f.close()
寫入檔案內容
>>>f=open("G:\\Python\\txt1.txt","w")>>>f.write("Python") #寫入內容>>>f.close()
自訂函數的寫法,這裡面多注意冒號:的寫法,還有就是空格的使用,不然很容易報錯的
>>>def my_function(x):... if x>=0:... return x... else:... return -x...>>> my_function(5)5
>>>def my_print():... print("i am is python")... return...>>> my_print()i am is python
匿名函數的寫法
關鍵字lambda表示匿名函數,冒號前面的x表示函數參數。
匿名函數有個限制,就是只能有一個運算式,不用寫return,傳回值就是該運算式的結果。
>>>f=lambda x:x+x>>>f(5)10