Python运算符

一、用户交互与运算符

1.1用户交互

打个比方:ATM自动取款机——》交互

input——》输入数据

print——》输出结果

1.1.1输入 input

numbers=input("请输入你的账号:")
cord=input("请输入你的密码:")
print(type(numbers))
print(type(cord))

请输入你的账号:123
请输入你的密码:456
<class 'str'>
<class 'str'>

Process finished with exit code 0

注意:无论我们输入的值是数值类型、字符串类型、列表类型还是其它的,input的接收值都是字符串类str

1.1.2输出print

print("500 + 500 =",500+500)

end参数,这个参数默认值是换行"\n"

print("aa")
print("aa",end="&")
print("bb",end="@")
print("cc",end="12")
print("aa",end="&")

aa
aa&aa&
Process finished with exit code 0

1.2运算符

·算术运算符

· 赋值运算符

·比较运算符

·逻辑运算符

·成员运算符

·身份运算符

·运算符优先级

1.2.1算术运算符

这里就不一一列举

/——》除法

%——》取余

+——》加法

-——》减法

**——》幂次方

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

2.0
Process finished with exit code 0

a=10
b=20
print(b%a)
0

Process finished with exit code 0
a=10
b=20
print(b**a)
10240000000000

Process finished with exit code 0
str1="hello"
print(str1*10)
hellohellohellohellohellohellohellohellohellohello

Process finished with exit code 0

1.2.2赋值运算符

在这里插入图片描述

a=20
b=10
c=a+b
# c+=a
# c-=a
# c*=a
# c/=a
# c%=a
# c**=a
# c//=a
print(c)
5e+18

Process finished with exit code 0

1.2.3比较运算符

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MArRhaz9-1668587116066)(C:\Users\YiYouLi\AppData\Roaming\Typora\typora-user-images\1668584395603.png)]

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

False
True
Process finished with exit code 0

1.2.4逻辑运算符

p为真命题,q为假命题,p且q为假,p或q为真,非q为真

and or not——》全部是英文,不是中文的与、或、非

a=14>6
b=50>90
print(a and b)
False

Process finished with exit code 0
a=14>6
b=50>90
print(a and not b)
True

Process finished with exit code 0

本质

在python中,and和or不一定会计算右边的值,有时候只计算左边表达式的值就能得到最终结果

and和or运算符会将其中一个表达式的值作为最终解雇,而不是将Ture或者Flase作为最终结果

1.2.5成员运算符(in、not in)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8dRioHC7-1668587116066)(C:\Users\YiYouLi\AppData\Roaming\Typora\typora-user-images\1668585462170.png)]

a="dog"
b="rabbit"
animals=["dog","elephant","snake"]
print(a in animals)
print(a not in animals)
print(b not in animals)
print(b in animals)

True
False
True
False

Process finished with exit code 0

1.2.6身份运算符(is、is not)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6mA1F9TU-1668587116066)(C:\Users\YiYouLi\AppData\Roaming\Typora\typora-user-images\1668585714317.png)]

a=5
b=5.0
print(a is b)
print(a is not b)

False
True

Process finished with exit code 0

注意:is比较的是本质,而==比较的是表面

可以用id()查看两个变量的地址是否一致,如果不一致,则本质不同,Flase,反之,True

c=3.0
d=9/3
print(c is d)
print(id(c))
print(id(d))

True
1700035863440
1700035863440

Process finished with exit code 0

1.2.7运算符优先级

最高到最低的优先级的运算符,优先级最高的运算符优先计算或者处理,同级别的按照从左往右计算(赋值运算符除外,它是从右往左计算的)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YZXAYjZj-1668587116066)(C:\Users\YiYouLi\AppData\Roaming\Typora\typora-user-images\1668586511457.png)]

算术运算符优先级大于比较运算符

比较运算符优先级大于逻辑运算都

逻辑运算符内部三个优先级 not>and>or

c=not 1 and 1>2 or 2==8
print(c)

False

Process finished with exit code 0
#1、用户输入两个数完成两个数的加法运算
a=input("请输入第一个数:")
b=input("请输入第二个数:")
c=int(a)+int(b)
print(c)
#或者
a=int(input("请输入第一个数:"))
b=int(input("请输入第二个数:"))
c=a+b
print(c)
请输入第一个数:100
请输入第二个数:200
300

Process finished with exit code 0
#计算100除以3得到的商、余数分别是多少?
a=100
b=3
c=a/b
d=a%b
print(c)
print(d)

33.333333333333336
1

Process finished with exit code 0
#要求输入姓名和年龄,并且将年龄加10之后与姓名一起输出
name=input("请输入你的名字:")
age=input("请输入你的年龄:")
print(name+":"+str(int(age)+10))

请输入你的名字:李仪有
请输入你的年龄:18
李仪有:28

Process finished with exit code 0
#模拟账号登录
number=int(input("请输入你的账号:"))
cord=int(input("请输入你的密码:"))
if number==52211206182 and cord==123456:
    print("恭喜你,登录成功!")
else:
    print("对不起,账号者密码错误,请重新输入")

请输入你的账号:151451
请输入你的密码:51513
对不起,账号者密码错误,请重新输入

Process finished with exit code 0
#模拟登录,只有三次机会
i=0
while i<3:
    i = i + 1
    number = int(input("请输入你的账号:"))
    cord = int(input("请输入你的密码:"))
    if number == 52211206182 and cord == 123456:
        print("恭喜你,登录成功!")
    else:
        print("对不起,账号者密码错误,请重新输入,你还有"+str(3-i)+"次机会")
print("你的输入超过三次,已经冻结")

请输入你的账号:151451
请输入你的密码:51513
对不起,账号者密码错误,请重新输入

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值