day02 运算符

                              运算符2019-04-01

目录

一、算数运算符

     + = * / % // **

二、比较运算

     > < == != >= <= <>

三、成员运算

    in   not in

四、逻辑运算

   and or not

五、赋值扩展运算

   = += -= *= /= //= **=

一. 算数运算符

  常用的符号:

 + 加法运算符求两个数的和

x = 10
y = 20
z = x + y
print(z)

- 减法运算跟上面的相反

x = 10
y = 20
z = x - y
print(z)

* 求连个数相乘

x = 10
y = 20
z = x * y
print(z)

/ 求两个数的商

x = 10
y = 20
z = x / y
print(z)

%求两个数相乘的余数

x = 10
y = 20
z = x % y
print(z)

// 当两个数相除的时候只取商而不去管是否能除尽是否有余数

x = 10
y = 21
z = y // x
print(z)

** 算两个数的次方

x = 8
y = 2
z = y ** x
print(z)

二. 比较运算符

    比较运算符主要用来对两个值的比较

> 比较a是否大于b

x = 8
y = 2
if x > y:
print("yes")
else:
print("No")

<比较a是否小于b

x = 8
y = 2
if x < y:
print("yes")
else:
print("No")

>=比较a是否小于等于b

x = 8
y = 2
if x >= y:
print("yes")
else:
print("No")

<=查看a是否小于等于b

x = 8
y = 2
if x <= y:
print("yes")
else:
print("No")

!=查看a是否不等于b

x = 8
y = 2
if x != y:
print("yes")
else:
print("No")

==查看a是否等于b

x = 8
y = 2
if x == y:
print("yes")
else:
print("No")

三. 成员云算法

in   not in

in:a是否在x里面

comment = input("请输入您的评论: ")
if "今天天气真好" in comment:
print("是的,适合出去晒太阳")

not in:a是否不存在x里面

comment = input("请输入您的评论: ")
if "我想学python" not in comment:
print("你不是个好孩纸")

四. 逻辑运算符

and or not 一般判断顺序为not and or

a or b
if a == 0: b else a
and和or相反     ps:做运算的时候会用到这个公式

and:两边条件都 为真则为成立

username = input("please input the username: ")
pwd = input("please input the password: ")
if username == "ivy" and pwd == "123":
print("welcome to the homepage")
else:
print("username or password wrong")

or:两边条件一边为真则成立

comment = input("请输入您的评论:")
if "苍老师" in comment or "法论功" in comment:
print("sorry, 您的评论含有非法字符评论失败")
else:
print("评论成功")

not: 用于布尔型True和False,not True为False,not False为True

a = input("请输入alpha: ")
list = ["a","c","b"]
if a not in list:
print("false")
else:
print("true")

五. 赋值运算符

= += -= *= /+ //= **= %=

=:一般用来赋值,右边的值传给左边

a = "xxxxx"

+= 什么加什么等于什么

count = 1
while count < 10:
print(count)
count += 1

-=什么减什么等于什么

count = 20
while count > 0:
print(count)
count -= 1

*=什么乘什么等于什么

a = 10
b = 20
b *= a
print(b)

/= 什么除以什么 等于什么

a = 10
b = 20
b /= a
print(b)

//= 什么取整等于什么

a = 10
b = 23
b //= a
print(b)

**=什么的n次方等于什么

a = 8
b = 2
b **= a
print(b)

%=什么的 什么的取余等于什么

a = 10
b = 23
b %= a
print(b)


We are down, but not beaten. tested but not defeated.
原文地址:https://www.cnblogs.com/guniang/p/10636374.html