注意if/while/for等(或函數定義)語句在結尾處包含一個冒號——我們通過它告訴python下面跟著一個語句塊。
--------------冒號的用法
if guess == number:print 'Congratulations, you guessed it.' # New block starts hereprint "(but you do not win any prizes!)" # New block ends hereelif guess < color="#ff0000">:print 'No, it is a little higher than that' # Another block# You can do whatever you want in a block ...else:print 'No, it is a little lower than that' --------------def printMax(x, y): '''輸出最大的2個數.2個數值必需是整數.'''x = int(x) # convert to integers,if possibley = int(y)if x > y: print x, 'is maximum'else:print y, 'is maximum'========逗號的特殊用途for item in shoplist:print item,
我們在print語句的結尾使用了一個 逗號 來消除每個print語句自動打印的換行符。這樣做有點難看,不過確實簡單有效。
以上這篇詳談python中冒號與逗號的區別就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答