Python每日笔记02(运算符)

Python中的运算符

 ***第一天的干货比较少,第二天来点硬货***

>> Python每日笔记—目录 <<

一、算术运算符
+ - * / 	加减乘除
%			模运算(取余数)
**:			次幂
//:			取整
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 8:07 
# 文件名称:demo01.PY
# 开发工具:PyCharm
a = 10
b = 22
c = a + b
print(c)
c = a - b
print(c)
c = a / b
print(c)
c = b % a
print(c)
b = 2
c = b ** a
print(c)
d = b // 1.5
print(d)
d = b << b;
print(d)

二、比较运算符
==		判断值相等
!=		判断值不相等
<		小于
<= 		小于等于
> 		大于
>=    	大于等于
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 8:19 
# 文件名称:demo02.PY
# 开发工具:PyCharm   
age = 19
flag = 18 < age < 100
print(age)
num = 19
print(num == age)

name1 = "zhangsan"
name2 = "zhangsan"
print(name1 == name2)
三、赋值运算符
+=		等价于+后赋值= 
-= 		等价于-后赋值= 
*= 		等价于*后赋值= 
/= 		等价于/后赋值= 
%= 		等价于%后赋值= 
**= 	等价于**后赋值= 
//=		等价于//后赋值= 
四、按位运算符
&:按位与运算符
|:按位或运算符
^:按位异或运算符
~:取反运算符
<<:左移运算符
>>:右移运算符
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 8:30 
# 文件名称:demo03.PY
# 开发工具:PyCharm   
a = 31
b = 18
c = a & b
print(c)
c = a | b
print(c)
c = a ^ b
print(c)
c = ~a
print(c)
c = a << 1;
print(c)
c = a >> 1;
print(c)
五、逻辑运算符
and	与
or	或
not	非
六、分支结构语句
if 判断条件:
    执行语句块
课堂作业1:
    在控制台内输入一个学生的分数为int类型:
    [90-100]之间为优秀
    [70-90)之间为良好
    [60-70)之间为及格
    [0,60)为不及格
    其他输入不合法
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 10:43 
# 文件名称:demo08.PY
# 开发工具:PyCharm

score = int(input("请输入分数:"))
if score < 0:
    print("输入错误")
elif score < 60:
        print("不及格")
elif score < 70:
        print("及格")
elif score < 90:
        print("良好")
elif score <= 100:
        print("优秀")
else:
        print("输入错误")

三目运算符
((((print("优秀") if score <=100 else print("error")) if score >= 90 else print("良好")) if score >= 70 else print("及格")) if score >= 60 else print("不及格")) if score >= 0 else print("error")
七、随机函数random()
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 14:05 
# 文件名称:demo08.PY
# 开发工具:PyCharm   
import random
# 从序列的元素当中,随机挑选一个元素
message = "上海自来水来自海上"
m = random.choice(message)
print(m)
# 从列表中返回一个随机元素
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
num = random.choice(list1)
print(num)
# 从指定范围内,按照指定的基数递增的集合中获取一个随机数,基数默认为1
# range(1,100,2)
'''
    range(start,stop,step)
    start:开始值
    stop:指定的结束值
    step:指定的递增基数
'''
num = random.randrange(0, 100, 2)
num = random.random()
print(num)
# 生成一个浮点数
num = random.uniform(5, 10)
print(num)
num = random.randint(0, 2)# [0,2]
print(num)

课堂作业:使用random.choice()方法实现猜拳游戏
    要求:输入的是字符串 剪刀、石头、布-
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 14:47 
# 文件名称:ClassWork-猜拳.PY
# 开发工具:PyCharm
import random
win = 0
draw = 0
file = 0
count = 0
while True:
    player = input("请出剪刀石头布:")
    computer = random.choice(["剪刀", "石头", "布"])
    print("电脑出",computer)
    if count == 10:
        break
    if player == computer:
       print("平局")
       draw += 1
    elif(player == "剪刀" and computer == "石头") or (player == "石头" and computer == "布") or (player == "布" and computer == "剪刀"):
        print("电脑获胜")
        file += 1
    elif(player == "石头" and computer == "剪刀") or (player == "布" and computer == "石头") or (player == "剪刀" and computer == "布"):
       print("玩家获胜")
       win += 1
    else:
      print("输入错误")
    count += 1
    print("胜利%d次,平局%d次,失败%d次" %(win, draw, file))
八、成员运算符、身份运算符
成员运算符、身份运算符
in: 如果在指定的序列中,找到该值,则返回true,否则返回false
    用来判断某一对象,是否是某一序列中的成员(元素)
not in: 如果在指定序列中没有找到,则返回true,否则返回false

身份运算符
is:用来表示两个标识符是否引用自同一对象。如果引用的是同一个对象,返回True,否则返回False
is not:是用来判断两个标识符是否引用自不同的对象,如果引用的不是同一个对象,则返回True

经典面试题:is 和 == 区别
is用于判断两个变量的引用对象是否是同一个, ==用于判断变量的值是否相等
is类似于 id()
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 15:18 
# 文件名称:demo10.PY
# 开发工具:PyCharm
a = 4
b = 5
list1 = [4, 6, 7, 9, 2]
if a in list1:
    print("a是list1中的成员")
else:
    print("a不是list1中的成员")
message = "第二届全国高校计算机能力挑战赛JSNU赛点"
mess = input("输入关键字:")
if mess in message:
    print("改内容中包含关键字")
else:
    print("不包含")
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 15:29 
# 文件名称:demo11.PY
# 开发工具:PyCharm   
name1 = "zs"
name2 = "zs"
flag = id(name1)
flag2 = id(name2)
print(flag, flag2)
print(name1 is name2)
九、运算符的优先级

由高到低一次如下

  1. **
  2. ~、+ (!@)、-(-@)一元运算符,一元的加减表示正负符号
  3. +、- 二元运算符的加减
  4. *、/、%、//
  5. << 、 >>
  6. &
  7. ^、|
  8. <=、<、>、>=
  9. ==、!=
  10. 赋值运算符
  11. 身份运算符
  12. 成员运算符
  13. 逻辑运算符
十、三目运算符/三元运算符
语法格式  : 值1  if  条件  else  值2
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 15:39 
# 文件名称:demo12.PY
# 开发工具:PyCharm   
'''三目运算符'''
a = 1
b = 2
c = a if a > b else b
c = a if a > b else b+1 if False else a+1
print(c)
十一、循环结构语句

while语法结构
 while循环条件:
         循环体

#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 15:49 
# 文件名称:demo13.PY
# 开发工具:PyCharm
count = 0
while count < 5:
    print("测试")
    count += 1
    # if count == 5:
    #     print("循环结束")
    #     break;

十二、format函数

format函数的使用

#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 9:04 
# 文件名称:demo04.PY
# 开发工具:PyCharm   
name = input("Name:")
tel = int(input("Tel:"))
address = input("Address:")
info = '''
———————info of {_name}————————
    Name:{_name}
    Tel:{_tel}
    Address:{_address}
'''.format(_name = name,
           _tel = tel,
           _address = address)

info = '''
———————info of {0}————————
    Name:{0}
    Tel:{1}
    Address:{2}
'''.format(name, tel, address)

print(info)
十三、课后练习1-超市找零

问题:输入商品数量和单价,输入付款金额,判断需要找零还是补款。
考察知识点:if 判断
——————————————————————思考一下

#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 16:42 
# 文件名称:ClassWork-supermar.PY
# 开发工具:PyCharm
p_num = int(input("请输入商品数量:"))
p_money = float(input("请输入商品价格:"))
money = float(input("请输入付款金额:"))
product = float(p_num * p_money)
if product > money:
    print("金额不足,需补充%.2f" % float(product-money))
else:
    print("找零%.2f" % float(money-product))

十四、课后练习2-ATM模拟

问题:假设用户中有50000元,有1-4中操作代码,输入其他代码无效,分别是1-查询 2-取款 3-转账 4-离开,模拟ATM操作。
考察知识点:while循环和if选择
——————————————————————思考一下

#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/7 16:34 
# 文件名称:ClassWork-ATM.PY
# 开发工具:PyCharm
pwd = int(input("请输入银行卡密码:"))
money = 50000
if pwd == 123456:
    while True:
        choice = int(input("请输入操作代码:1-查询 2-取款 3-转账 4-离开"))
        if choice == 1:
            print("当前余额为%.2f" % money)
        elif choice == 2:
            get = float(input("请输入取款金额:"))
            if get > money:
                print("余额不足,请重新选择")
            else:
                money -= get
                print("当前余额为%.2f" % money)
        elif choice == 3:
            put = float(input("请输入转账金额:"))
            money += put
            print("当前余额为%.2f" % money)
        elif choice == 4:
            break
        else:
            print("操作代码错误!请重新选择")
            continue

>> Python每日笔记—Day03 <<
>> Python每日笔记—Day04 <<
>> Python每日笔记—Day05 <<
>> Python每日笔记—Day06 <<
>> Python每日笔记—Day07 <<
>> Python每日笔记—Day08 <<
>> Python每日笔记—Day09 <<


如果文章内容对你有帮助,点个赞呗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值