0. 輸入
- Python2.x
# raw_input # 功能 1. 會等待用戶輸入內(nèi)容,知道用戶按下 Enter 2. 會將用戶輸入的內(nèi)容當(dāng)做 字符串,傳遞給接收的變量 inputStr = raw_input("請輸入信息") print type(inputStr) print inputStr # input # 功能 1. 會等待用戶輸入內(nèi)容,直到用戶按下 Enter 2. 會將用戶輸入的內(nèi)容當(dāng)做 代碼 進(jìn)行處理 inputResult = input("請輸入代碼") print type(inputResult) print inputResult - Python3.x
# input # 功能 1. 會等待用戶輸入內(nèi)容,直到用戶按下 Enter 2. 會將用戶輸入的內(nèi)容當(dāng)做 字符串,傳遞給接收的變量 inputStr = input("請輸入消息") print(type(inputStr)) print(inputStr) inputResult = eval(inputStr) print(type(inputResult)) print(inputResult)
1. 輸出
- Python2.x
# print 語句:print xxx print "my name is luck" - Python3.x
# print函數(shù):print(values, sep, end, file, flush) # values:需要輸出的值 1. 多個(gè)值,使用 , 進(jìn)行分割 # sep:分割符 1. 多個(gè)值,被輸出出來之后,值與值之間,會添加指定的分隔符 2. 默認(rèn)值 ' ' # end:輸出完畢之后,以指定的 字符 結(jié)束 1. 默認(rèn)是換行 \n # file:表示 輸出的目標(biāo) 1. 默認(rèn)是標(biāo)準(zhǔn)的輸出,即控制臺,file = sys.stdout 2. 還可以是一個(gè)可寫入的文件句柄,file = open("luck.txt", "w") # flush:表示 是否立即 輸出 1. 流程 (1) 需要輸出的內(nèi)容,先存放在 緩沖區(qū),然后再輸出到目標(biāo) (2) flush,就代表是否刷新緩沖區(qū),讓緩沖區(qū)的內(nèi)容,立即輸出到目標(biāo) 2. 若輸出內(nèi)容末尾是以 \n 結(jié)尾,則會 立即刷新 緩沖區(qū) 3. 默認(rèn)是 False
2. 占位格式符
- 格式:
%[(name)][flags][width][.precision]typecode- 中括號
[]包含的部分,代表可選
- 中括號
-
(name):用于選擇指定的名稱對應(yīng)的值age = 20 luckAge = 18 print("我年齡是:%(my)d,luck 年齡是:%(luck)d" % ({"my": age, "luck": luckAge})) -
flags-
空:表示右對齊,在左側(cè)空出位置firstNum = 20 secondNum = 18 print("%10d%d" % (firstNum, secondNum)) -
-:表示左對齊,在右側(cè)空出位置firstNum = 20 secondNum = 18 print("%-10d%d" % (firstNum, secondNum)) -
空格:表示在正數(shù)的左側(cè)填充一個(gè)空格,從而與負(fù)數(shù)對齊firstNum = 20 secondNum = 18 print("% d%d" % (firstNum, secondNum)) -
0:表示使用0填充左側(cè)minutes = 5 sec = 18 print("%02d:%02d" % (minutes, sec))
-
-
width:表示顯示寬度firstNum = 20 secondNum = 18 print("%10d%d" % (firstNum, secondNum)) -
.precision:表示小數(shù)點(diǎn)后精度score = 59.9 print("%.5f" % score) -
typecode:轉(zhuǎn)換說明符- 數(shù)值
數(shù)值.png - 字符串
-
s:獲取傳入對象的_str_方法的返回值,并將其格式化到指定位置 -
r:獲取傳入對象的_repr_方法的返回值,并將其格式化到指定位置 -
c# 整數(shù):將數(shù)字轉(zhuǎn)換成其 unicode 對應(yīng)的值 # Python2.x,只支持 0 ~ 255 # Python3.x,10 進(jìn)制范圍支持 0 ~ 1114111 print("我就是很厲害啊%c" % 19999) # 字符:將字符添加到指定位置 print("我就是很厲害啊%c" % "哦")
-
- 注意
-
%%表示一個(gè)% -
不存在自動將整數(shù)轉(zhuǎn)換成二進(jìn)制表示的方式
-
- 數(shù)值
3. 應(yīng)用場景
- 輸出一個(gè)值
# Python2.x print 666 # Python3.x print(3) - 輸出一個(gè)變量
# Python2.x num = 88 print num # Python3.x num = 66 print(num) - 輸出多個(gè)變量
# Python2.x a, b, c = 66, 88, 100 print a, b, c # Python3.x a = b = c = 100 print(a, b, c) - 格式化輸出
# Python2.x # % 寫法 name = "秦子陽" age = 18 formatStr = "my name is %s, and age is %d" % (name, age) print type(formatStr), formatStr # format 寫法 name = "秦子陽" age = 18 formatStr = "my name is {0}, and age is {1}".format(name, age) formatStr = "my name is {a}, and age is ".format(a=name, b=age) print type(formatStr), formatStr # Python3.x # % 寫法 name = "小熊bliving" age = 18 formatStr = "my name is %s, and age is %d" % (name, age) print(type(formatStr), formatStr) # format 寫法 name = "小熊bliving" age = 18 formatStr = "my name is {0}, and age is {1}".format(name, age) formatStr = "my name is {a}, and age is ".format(a=name, b=age) print(type(formatStr), formatStr) - 輸出到文件中
# Python2.x filePath = open("luck.txt", "w") print >> filePath, "my name is 秦子陽, and age is 18." # Python3.x filePath = open("luck.txt", "w") print("my name is 秦子陽, and age is 18", file=filePath) - 輸出不自動換行
# Python2.x a = b = c = 100 print a, print b, print c, # Python3.x print("hello,", "i am luck.", end="") - 輸出的各個(gè)數(shù)據(jù),使用分隔符分隔
# Python2.x print "_".join(["a", "b", "c"]) # Python3.x print("my", "name", "is", "luck", sep="---")
