python入門7 運算符
阿新 • • 發佈:2018-11-03
bin 算術運算符 true nbsp utf 加減乘 python 取余 clas
python運算符:
1 算術運算符 加減乘除 取余 求商 求冪等
2 比較運算符== != > >= < <=
3 邏輯運算符 and not or
4 賦值運算符 = += -= *= /=等
5 成員運算符in ,not in
6 三目運算符 result1 if condition else result2
#coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 運算符 """ ‘‘‘算術運算符 + - * / ** % //‘‘‘ num1 = 100 num2 = 300.3 print(num1 + num2) #加 print(‘hello ‘+‘world ‘+‘!‘) #字符串連接 print(num1 - num2) #減 print(num1 * num2) #乘 print(num1 / num2) #除 print(310 % num1) #余,求模 print(310/100,310//100.0,310 % 100.0)# //商 print(-310/100,-310//100,-310 % 100) # //商 print(num1**4) #冪 ‘‘‘比較運算符 == != > >= < <= ,身份運算符is ,is not‘‘‘ print(1 == 1) # ==是否相等 print(1 != ‘1‘,True != 0 ) #!= 是否不等 print(num1 > 2) print(num1 <= 2) num3 = 100 print(num1 is num3) #is 變量所指對象是否相同 print(num1 is not num3) ‘‘‘邏輯運算符 and or not‘‘‘ print(1>0 and True and ‘a‘==‘a ‘) print(1>0 or 9<3) print(not True) ‘‘‘賦值運算符 = += -+ *= /= %= //=‘‘‘ num1,num2,num3=10,2,3 num1,num2 = num2,num1 #數據交換 #(num1,num2) = (num2,num1)print(num1,num2) num1 += 10 print(num1) num1 -= 2 print(num1) num1 *= 5 print(num1) num1 /= 2 print(num1) num1 //= 6 print(num1) num1 %= 3 print(num1) ‘‘‘成員運算符 in ,not in‘‘‘ list = [2,3,1] print(1 in list,5 in list) print(‘ab‘ in ‘fwei xabs‘) ‘‘‘三目運算符‘‘‘ result = num1 if num1>num2 else num2 print(‘最大值: ‘,result)
the end!
python入門7 運算符