Python学习笔记(四):运算符

运算符:
算数运算符:+ - * / % // **
赋值运算符:= += -= *= /= **= //= %=
比较运算符:> < >= <= == != is
逻辑运算符:and or not

# 算数运算符:
a = 1
b = 2
c = a + b

# print(a, b, c, end='\n') # 1 2 3 \n表示末尾换行
print(a, b, c) # 1 2 3
print(c - a) # 2
print(c * a) # 3
print(c ** 2) # c的2次幂 # 9
print(c / 2) # c直接除 # 1.5
print(c // 2) # c被2整除 # 1
print(c % 2) # 取余、取模 # 1

"""
练习:
键盘输入一个三位数的整数,打印个位数,十位数,百位数
"""
number = int(input('请输入一个三位数的整数:')) # 123
print("个位数为", number % 10) # 个位数为 3
print("十位数为", number // 10 % 10) # 十位数为 2
print("百位数为", number // 100) # 百位数为 1
# 赋值运算符:
a = 10
b = 10
c = 10
d = 10
e = 10
f = 10
g = 10

a += 1
b -= 1
c *= 2
d /= 2
e **= 2
f //= 2
g %= 3
print('赋值运算符运算结果',a,b,c,d,e,f,g) # 赋值运算符运算结果 11 9 20 5.0 100 5 1
# 比较运算符:> < >= <= == != is
a = 10
b = 25

print(a > b) # False
print(a < b) # True
print(a == b) # False
print(a != b) # True
print(a is b) # False

字符串比较大小,比较ASC||码,从第一个字母开始往后比,一个字母一个字母比,当可以比出来的时候则不再继续往后比较
x = 'abc'
y = 'abd'

print(x == y) # False
print(x < y) # True

"""
输入考试分数,判断成绩是否在100到80之间
"""
score = float(input('输入分数:'))
print(80 <= score <= 100) # 只有python中可以这么写,其他语言必须与逻辑运算符混合使用

"""
输入两个商品的价格,比较两个商品的价格是否相同
"""
price1 = float(input('价格一:'))
price2 = float(input('价格二:'))
print(price1 == price2)
# 逻辑运算符:
'''
A and B
False and False --> False
False and True --> False
True and False --> False
True and True --> True
'''
a = 1
b = 0
c = 2
# and:若两数非零,则得后边的数字值,若有0,则得0,即两侧存在一个假,结果为假
print(b and a) # 0
print(a > b and c < a) # False

print('-' * 30)


'''
A or B
False or False --> False
False or True --> True
True or False --> True
True or True --> True
'''
c = 0
d = 4
# or:若两数非零,则得前边的数字值,若有一个0,则得另一个数字值,若均为0,则得0,即两侧均为假,结果为假
print(c or d) # 4
print(a > b or c > a) # True
print(a == c or a < b) # 账号密码 or 手机号验证码

print('-' * 30)

'''
not True --> False
not False --> True
'''
flag = True
print(not flag)
print(not a > c)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冬嫱姐姐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值