2算符和表達(dá)式

算符是連接一個(gè)或多個(gè)元素的符號,用來表達(dá)計(jì)算。

常見算符

運(yùn)算按種類可分為算術(shù)運(yùn)算、比較運(yùn)算、邏輯運(yùn)算、賦值運(yùn)算、成員運(yùn)算、身份運(yùn)算、位運(yùn)算.

  • 算數(shù)運(yùn)算符
a = 10;
b = 20;
print(a + b); # 30
print (a - b); #-10
print (a * b); #200
print (b/a);#2
print(b%a);#0取余
print(a**b);#求10的20次方
print(b // a);#整除沒有小數(shù)
----------------------------------------------
30
-10
200
2.0
0
100000000000000000000
2
  • 比較運(yùn)算符
a = 10;
b = 20;
print(a == b); # 比較是否相等
print (a != b); #不相等
print (a > b); #
print (a < b);#
print(a <= b);#
print(a >= b);#
----------------------------------------
False
True
False
True
True
False
  • 賦值運(yùn)算符
a = 10;
b = 20
c = a+b;
print("賦值運(yùn)算符={0}".format(c));
c += a;
print("加法賦值運(yùn)算符c={0},等效于c=c+a".format(c));
c -= a; #運(yùn)算前此時(shí)C = 40啦
print("減法賦值運(yùn)算符c={0},等效于c=c-a".format(c));
c *= a;
print("乘法賦值運(yùn)算符c={0},等效于c=ca".format(c));
c /= a;#運(yùn)算前此時(shí)C = 300啦
print("除法賦值運(yùn)算符c={0},等效于c=c/a".format(c));
c %= a;
print("取余賦值運(yùn)算符c={0},等效于c=c%a".format(c));
c = 2;
c **=a;
print("冪賦值運(yùn)算符c={0},等效于c=**a".format(c));
c = 25;
c //= a;
print("取整除賦值運(yùn)算符c={0},等效于c=c//a".format(c));
-------------------------------------------------
賦值運(yùn)算符=30
加法賦值運(yùn)算符c=40,等效于c=c+a
減法賦值運(yùn)算符c=30,等效于c=c-a
乘法賦值運(yùn)算符c=300,等效于c=ca
除法賦值運(yùn)算符c=30.0,等效于c=c/a
取余賦值運(yùn)算符c=0.0,等效于c=c%a
冪賦值運(yùn)算符c=1024,等效于c=**a
取整除賦值運(yùn)算符c=2,等效于c=c//a
  • 邏輯運(yùn)算符

    • and 布爾與
      and 前項(xiàng)為真看后項(xiàng)
      x and y, x為真,值是y,x為假,值是x
    • or 布爾或
      or 前項(xiàng)為真即前項(xiàng)
      x or y, x為真,值就是x,x為假,值是y
    • not 布爾非
      在沒有()情況下 not 優(yōu)先級高于 and, and 優(yōu)先級高于 or,即優(yōu)先級關(guān)系為()> not > and > or, 同意優(yōu)先級從左到右進(jìn)行計(jì)算。
  • 成員運(yùn)算
    判斷子元素是否在原字符串(字典、列表、集合)中

    • in
    • not in
print ('喜歡' in 'sldkfjlkwje喜歡slkdfjlksjldf');
print ('a' in 'asfe');
print ('y' not in 'sssssss');
print ('y' not in 'ssyssssssss');
--------------------------------
True
True
True
False

格式化字符串
將變量的值格式化到字符串中,形成新的字符串。常用于輸出數(shù)據(jù)內(nèi)容

name = 'coco';
print('my name is {0}, and the length of name if {1}'.format(name, len(name)));
print('name is %s , length is %d' % (name, len(name)));
------------------------------------
my name is coco, and the length of name if 4
name is coco , length is 4
  • format的標(biāo)準(zhǔn)格式化格式
    ‘{0}{1}’.format(a,b)前面0、1 實(shí)際就是對應(yīng)括號里面的a,b不過在很長字符串的時(shí)候一般使用
    '{a}'.format(a=xxx,b='xxx')

  • %d表示按整型數(shù)據(jù)的實(shí)際長度輸出數(shù)據(jù)。

  • %c用來輸出一個(gè)字符。

  • %s用來輸出一個(gè)字符串。

  • %x表示以十六進(jìn)制數(shù)形式輸出整數(shù)

表達(dá)式

表達(dá)式為一串由算符連接的元素

執(zhí)行流控制

執(zhí)行流控制代碼控制流的先后執(zhí)行順序

邏輯行和物理行

Python語句執(zhí)行以行為單位。每行可選的可以以分號;結(jié)尾。
但是物理上的一行可以包含多個(gè)執(zhí)行單位。

單行不超過 80 個(gè)字符

格式對齊

python 嚴(yán)格對齊 縮進(jìn)不同 邏輯不同

  • if
number = 23
guess = int (input('Please enter an integer:'))
if guess == number:
    print('Congratulations! You win.')
elif guess < number:
    print ('higher,please')
else:
    print('lower,please')
    print('DONE') #注意縮進(jìn)不同,邏輯不同

Pdb工具: 基于命令行的代碼調(diào)試工具

  • while
number = 23
running = True
while running:
    guess = int (input('Please enter an integer:'))
    if guess == number:
        print('Congratulations! You win.')
        running = False
    elif guess < number:
        print ('higher,please')
    else:
        print('lower,please')
else:
    print('The while loop is over')
print('DONE') 
---------------------------------
Please enter an integer:2
higher,please
Please enter an integer:20
higher,please
Please enter an integer:32
lower,please
Please enter an integer:23
Congratulations! You win.
The while loop is over
DONE
  • for
for i in range(1,5): # 前閉后開區(qū)間
    print(i)
else:
    print('The for loop is over')
------------------------------------
1
2
3
4
The for loop is over
  • break
number = 23
running = True
for i in range (1, 10):
    print(i)
    if (i > 5):
        print('{0}---break了?'.format(i))
        break
        print('break你執(zhí)行了')
print('DONE') 
----------------------------------------------
1
2
3
4
5
6
6---break了?
DONE

今天你又博學(xué)了么?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容