码迷,mamicode.com
首页 > 编程语言 > 详细

python入门7 运算符

时间:2018-11-03 21:54:39      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:end   lse   print   code   cond   pre   pytho   not   res   

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 运算符

标签:end   lse   print   code   cond   pre   pytho   not   res   

原文地址:https://www.cnblogs.com/dinghanhua/p/9902072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!