一、算术运算符
1、除法
/:正常除法,/计算的结果都是小数哪怕能整除。
//:往小取整,除不尽的部分扔掉。注意小数除以小数,有小数参与的表达式结果就一定是小数。
print(10/2) #>>5.0 print(3.4/2.2) #>>1.5454545454545452 print(3.4//2.2) #>>1.0 print(-8/3) #>>-2.6666666666666665 print(-8//3) #>>-3,往小里取整 print(-8 % 3) #>>1,-2+3
2、-x相当于0-x
print(-a) #>>-10
print(-a*3) #>>(-10)*3
print(5+-a) #>>5+(-10)
3、int(x) 去尾取整
x=2.8
print(int(x)) #>>2
4、/ 是 ,不是分数线
如:a/b*c 等价于 (ab)*c
a = 2
b = 3
c = 1
print(a/b*c)
# >>0.6666666666666666
5、算术符优先级
第一级:**
第二级:* / // %
第三级:+ -
二、关系运算符
六种运算符用于数值的比较
== != > < >= <=
比较的结果是bool类型,成立则为 True 反之为 False
bool类型数据只有两种取值,True 或 False
python里 1 == Ture , 0 == False
关系运算符也可以用于比较字符串(按字典序,大小写相关)
三、逻辑运算符
1、逻辑运算符用于表达式的逻辑操作,有 and , or , not 三种,操作的结果是 True 或 False
a = 4
b = 5
print(a<=4 and a>5 and a%2==0) #>>False
2、相当于True ,False和等于True ,False不一样
· 0," "(空字符串),[ ](空表) 都相当于False ,但只有 0 == False
·非 0 的数,非空的字符串和非空列表,都相当于True,但只有 1==True
print(True == 1) #>>True
print(False == 0) #>>True
print("" == False) #>>False
print(2 == True) #>>Fale
print([] == False) #>>False
print([2, 3] == True) #>>False
3、与,或,非
·与:exp1 and exp2
当且仅当exp1 和exp2 的值都为 True (或相当于 True),结果为 True (或相当于 True)
·或:exp1 or exp2
当且仅当 exp1 和exp2 的值都为False (或相当于 False),结果为 False (或相当于 False)
·非:not exp
exp 的值为True(或相当于 True)时,结果为 False(或相当于 False)。
exp 的值为 False(或相当于 False)时,结果为 False(或相当于 True)。
print(not 4 > 5) #>>True
print(not 0) #>>True
print(not 5) #>>False
print(not "abc") #>>False
print(not []) #>>True
print(not [1]) #>>False
print(not 4 < 5 and 4 > 6) #>>False 等价于 (not 4 < 5)and (4>6 )
4、逻辑运算符的优先级
not > and > or
print(3 < 4 or 4 > 5 and 1 > 2) #>>True 等价于 3 < 4 or (4 > 5 and 1 > 2)
print((3 < 4 or 4 > 5) and 1 > 2) #>>False
print(not 4 > 5 and 4 > 6) #>>False 等价于 (not 4 > 5) and (4 > 6)
5、惰性求值
四、各运算符的优先级
·算术运算符 ** * / // % + -
·关系运算符 <> == != <= >=
·逻辑运算符 not and orwu
print( 3 + 2 < 5) #>>False
print( 3 + (2 < 5)) #>>4
五、条件分支
1、什么是相当于True或False
if "ok":
print("ok") #>>ok
if "":
print("null string") #无输出
a = [4, 2]
if a:
print(a) #>>[4, 2]
if 20:
print(20) #>>20
if 0:
print(0) #无输出
2、if 嵌套
a = int(input())
if a>0:
if a%2:
print("奇数")
else:
print("偶数")
实例1:输入密码
password = "python"
userInput = input()
if userInput == password:
print("对了")
else:
print("错了")
实例2:判断年份
year = int(input())
if year < 0:
print("illegal year")
else:
print("legal year")
if year>1949 and (year-1949)%10 == 0:
print("lucky year")
elif year>1951 and (year-1951)%10 ==0:
print("good year")
elif year%4 ==0 and year%10 !=0 or year%400 ==0:
print("Leap year")
else:
print("common year")
实例3:
字符串切片
若 s 是一个字符串,则 s[x:y] 是 s 的从下标 x 到下标 y 的左边的那个字符构成的子串(切片),不包括 s[y]。
print("1234"[0:1]) #>>1
a='abcdef'
print(a[2:4]) #>>cd
print(a[2:-1]) #>>cde
print(a[0:6]) #>>abcdef
实例4:温度转换
记录错误:can only concatenate str (not "int") to str;
没有做数据类型的转换,python并不能像java一样,在做拼接的时候自动把类型转换为string类型;
故而需要进行一个类型转换,譬如将print(1+"a")改为print(str(1)+"a")就可以了
tempStr = input("请输入带有格式的温度")
if tempStr[-1] in ['F', 'f']:
C = (float(tempStr[0:-1])-32)/1.8
print("转换后的温度是"+str(C)+'C')
elif tempStr[-1] in ['C', 'c']:
F = 1.8*float(tempStr[0:-1])+32
print("转换后的温度是"+str(F)+'F')
else:
print("输入格式错误")
六、输出格式控制
字符串中的格式控制符,格式控制符只能出现在字符串中
%s 表示此处要输出字符串
%d 表示此处要输出一个整数
%f 表示此处要输出一个小数
%.nf 表示此处要输出一个小数,保留小数点后面 n 位,四舍六入,五可能入也可能舍
h = 1.746
print("My name is %s,I am %.2fm tall." % ("Linda", h))
print("My age is %d." % 18)
print("%d%s" % (18, 'hello'))
print("%.2f,%.2f" % (5.235, 5.265))
# 输出:
# My name is Linda,I am 1.75m tall.
# My age is 18.
# 18hello
# 5.24,5.26 5一半的可能舍一般的可能入