python入門6 python檢視資料型別及型別轉換
阿新 • • 發佈:2018-11-03
檢視資料型別:type()
型別轉換:int(),float(),char(),ord(),str(),bool()
#coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 檢視資料型別,型別轉換 """ """檢視資料型別type()""" print(type(1),type(3.14), type('a'),type('abd'), type((1,2,3)),type(True),type(None), type([]),type({1,2}),type({1:'monday',2:'tuesday'})) """強制型別轉換""" """整數,浮點數,ascii碼轉換:int(),float(),chr(),ord()""" f = 3.14 print('f=%f,int(f)=%d '%(f,int(f))) print(float(int(f))) print(chr(65)) #整數轉化為ascii碼字元 print(ord('a')) #ascii字元轉化為整數 '''轉成布林型別 bool()''' print(bool('1'),bool('0'),bool([0])) #True print(bool(''),bool(0),bool([]),bool(()),bool({})) #False '''字串 str()''' print(type(str(f))) '''特殊字元-轉義字元 r忽略轉義字元''' print('abc\nghigk\tjfwejf\\fjewfjoewjfjfwejgwe\'jfwifoe?') print(r'abc\nghigk\tjfwejf\\fjewfjoewjfjfwejgwe\'jfwifoe?') print('c:\\user\\pycharm') #\\轉義\ print(r'c:\user\pycharm') #用r無需加轉義 print("i'm busy") #裡面帶'的用" print('he said:"ok"') #裡面帶"用'
the end!