python入门5 运算符

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!

posted @ 2018-11-03 20:57  dinghanhua  阅读(253)  评论(0编辑  收藏  举报