python基礎3——運算子

來源:互聯網
上載者:User

標籤:size   print   ima   需要   sha   import   strong   .com   blog   

註: from future import division <---除法運算中python2匯入此模組,除不盡時,小數部分也會顯示,python3中不需要匯入

1‘‘‘----------運算子 + 、—、*、/、%、*--------‘‘‘
a=9
b=a
3
print(‘b=a3--> %d3=%d‘%(a,b))
b=a+1
print(‘b=a+1--> %d+1=%d‘%(a,b))
b=a-1
print(‘b=a-1--> %d-1=%d‘%(a,b))
b=a/2
print(‘b=a/6--> %d/6=%d‘%(a,b))#只取整數部分,如需小數寫成float類型
print(9/2)
print(9//2)#只取整數
b=a%2
print(‘b=a/2的餘數--> %d/2的餘數=····%d‘%(a,b))
b=a3
print(‘b=a
3--> %d**3=%d‘%(a,b))#次方
執行效果:

2‘‘‘--------比較子----------‘‘‘
‘‘‘

> 大於

== 等於
!= 不等於
<> 不等於
< 小於
=> 大於等於
<= 小於等於
‘‘‘

3‘‘‘--------賦值運算------------‘‘‘
c=9
print(‘c=%d‘%c)
c+=1 #c+=1<=>c=c+1
print(‘c=%d‘%c)
c-=2 #c-=2<=>c=c-2
print(‘c=%d‘%c)
c=3 #c=3<=>c=c*3
print(‘c=%d‘%c)
c/=5 #c/=5<=>c=c/5
print(‘c=%0.1f‘%c)#保留1位小數
d=30
d%=9 #d%=9<=>d=d%9
print(‘d=%d‘%d)
e=20
e//=3 #e//=3<=>e=e//3
print(‘c=//%d‘%e)
f=5
f=2 #f=2<==>f=f**2
print(‘f=%d‘%f)
執行效果:

4‘‘‘---------邏輯運算---------‘‘‘
#只能返回真(True)或假(False),沒有優先順序,左--->右執行
a1=10
a2=20
if a1==10 and a2==20: #and表示並且,所有條件都成立才能執行
print(‘通過1‘)
if a1<9 or a2==20: #or表示或,條件中有一個成立就可以執行
print(‘通過2‘)
if a1!=11 or a2!=220:
print(‘通過3‘)
if a1==10 or a2>30:
print(‘通過4‘)
if not a1==11: #not表非,可以理解為不是
print(‘通過5‘)
執行效果:

5‘‘‘--------成員運算-----------‘‘‘
#只能返回真(True)或假(False)
a=‘abc‘
if ‘b‘ in a:
print(‘通過6‘)
if not ‘d‘ in a:
print(‘通過7‘)
執行效果:

python基礎3——運算子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.