突然進度開始加快了,一章夾雜了這麼多內容。條件、循環還有其它語句,看來是要分上中下了。
第五章 條件、循環和其它語句
5.1 print和import的更多消息
print在python3.0中不再是語句,而是函數,基本功能不變。
5.1.1 使用逗號輸出
打印多個表達式時,用逗號隔開。
print('Age:',42) #返回值Age: 42,打印出的結果會自動在每個參數之間插入一個空格符。
print(1,2,3) #返回值1 2 3,不會構成元組。
print((1,2,3)) #返回值(1, 2, 3)
name='Gumby'
salutation='Mr.'
greeting='Hello,'
print(greeting,salutation,name) #返回值Hello, Mr. Gumby
print(greeting,',',salutation,name) #返回值逗號前會自動插入空格符
prrint(greeting+',',salutation,name) #返回值Hello, Mr. Gumby
#print在3.0以後有了變化
print('hello',
print('world')) #返回值world hello None,不但打印順序變了,而且會在第一個打印的結果後面加None,說明先執行括號裏面的函數。
5.1.2 把某件事作爲另一件事導入
從模塊導入函數的格式:
import somemodule,
from somemodule import somefunction,
from some module import somefunction,anotherfunction,yetanotherfunction
from somemodule import *(只有確定自己想要從給定的模塊導入所有功能時,才使用最後一個版本。
如果兩個模塊都有open函數
只需要使用第一種方式導入,然後按下面方式使用:
module1.open(...)
module2.open(...)
也可以在語句末尾加一個as子句,在該子句後給出名字,或爲整個模塊提供別名:
import math as foobar
foobar.sqrt(4) #返回值2.0
from math import sqrt as foobar #爲整個模塊提供別名
foobar(4) #返回值2.0
對於open函數可以像下面這樣用:from module1 import open as open1
from module2 import open as open2
5.2 賦值魔法
5.2.1 序列解包
x,y,z=1,2,3 #多個賦值同時進行
print(x,y,z) #返回值1 2 3
print(y,z,x) #返回值2 3 1,自動將x,y,z和值一一對應
x,y=y,x
print(x,y,z) #返回值2 1 3
#序列解包
values=1,2,3
values #返回值(1, 2, 3)
x,y,z=values
x #返回值1。
scoundrel={'name':'Robin','girlfriend':'Marion'}
key,value=scoundrel.popitem()
key #返回值'girlfriend'
value #返回值'Marion'
#所解包的序列中的元素數量必須和放置在賦值符號=左邊的變量數量完全一致,否則會引起異常
x,y,z=1,2 #報錯
a,b,rest*=[1,2,3,4] #操作也會報錯illegal expression for augmented assignment。
5.2.2 鏈式賦值(chained assignment)
將同一個值賦給多個變量。
x=y=somefunction()跟下面語句是一樣的效果
y=somefunction()
x=y
不等同於
x=somefunction()
y=somefunction()
5.2.3.增量賦值(augmented assignment)
表達式運算符放在賦值運算符左邊。
x=2
x+=1 #意思是x=x+1
x #返回值3
x*=2
x #返回值6
#只要二元運算符本身適用於這些數據類型都可以使用
fnord='foo'
fnord+='bar'
fnord*=2
fnord #返回值'foobarfoobar'
5.3 語句塊:縮排的樂趣
語句縮排是在條件爲真時執行或者執行多次的一組語句。代碼前可以通過放置空格來縮進語句即可創建語句塊。類似於寫作文分段會在段首加兩個空格,看起來比較清楚。
5.4 條件和條件語句
5.4.1 這就是布爾變量的作用
布爾值用真(True)假(False)來表示。
下面的值在作爲布爾表達式時,會被看作假(false)
False None 0,"",(),[],{}
其它一切都爲真(True)
bool函數可以用來將其它值轉換成布爾值
bool('I think,therefore I am') #返回值True,這裏可以調戲電腦了,隨便寫什麼電腦都會回答你true,因爲返加值既不是空值也不是各種形式的0.
bool(42) #返回值True
bool('') #返回False
bool(0) #返回False
雖然[]和""都是假值,它們本身卻不相等,對於其它不同的假值對象也是如此。
5.4.2 條件執行和if語句
#運行腳本
name=input("What is your name?")
if name.endswith('Gumby'): #條件判定語句如果爲真,執行冒號後面的語句。如果爲假,不執行。
print('Hello,Mr.Gumby')
5.4.3 else子句
#else不是獨立語句,只能作爲if語句的一部分,增加一種選擇。
name=input("What is your name?")
if name.endswith('Gumby'):
print('Hello,Mr.Gumby')
else: #if語句爲假,執行else冒號後面的語句。
print('Hello,stranger')
5.4.4 elif子句
需要檢查多個條件時,就可以用elif。
num=int(input("Enter a number:")) #這裏必須指定數據類型
if num>0:
print('The number is positive')
elif num<0:
print('The number is negative')
else:
print('the number is zero')
5.4.5 嵌套代碼塊
name=input('What is your name?')
if name.endswith('Gumby'):
if name.startswith('Mr.'): #if語句中嵌套if語句
print('Hello,Mr.Gumby')
elif name.startswith('Mrs.'):
print('Hello,Mrs.Gumby')
else:
print('Hello,Gumby')
else:
print('hello, stranger')
5.4.6 更復雜的條件
1.比較運算符
x==y | x等於y |
x<y | x小於y |
x>y | x大於y |
x>=y | x大於或等於y |
x<=y | x小於或等於y |
x!=y | x不等於y |
x is y | x,y是同一對象 |
x is not y | x,y是不同的對象 |
x in y | x是容器y的成員 |
x not in y | x不是容器y的成員 |
2.相等運算符
單個等號是賦值,兩個等號才表示相等
3. is:同一性運算符
x=[1,2,3]
y=[2,4]
x is not y #返回值True
del x[2]
y[1]=1
y.reverse()
x==y #返回值True,x和y的值相等了
x is y #返回值False
4.in:成員資格運算符
name=input('What is your name?')
if 's' in name:
print('You name contains the letter "s".')
else:
print('Your name does not contains the letter "s".')
5.字符串和序列比較
字符串可按字母順序進行比較。
'alpha'<'beta' #返回True
'FnOrD'.lower()=='Fnord'.lower() #返回值True,如果有大小寫就比較混亂,可以用upper和lower忽略大小寫再比較。
[1,2]<[2,1] #返回值True。
[2,[1,4]]<[2,[1,5]] #返回True。
6.布爾運算符
number=int(input('Enter a number between 1 and 10:'))
if number <=10 and number >=1: #and 就是布爾運算符,連接兩個布爾值,並且在兩者都爲真是返回真
print('Great!')
else:
print('Wrong!')
5.4.7 斷言
assert語句在程序中置入檢查點,條件後添加字符串,用來解釋斷言。