学习python的第三天

一.

break终止循环:

# 吃四个苹果
i= 1
while i <= 4:
    print(f"吃第{i}个苹果")
    i += 1
print("===================")

# 吃四个苹果,吃到第三个我就饱了,不吃第四个
i = 1
while i <= 4:
    print(f"吃第{i}个苹果")
    if i == 3:
        print("我吃饱了")
        # 终止所有循环
        break
    i += 1

二.

continue跳出循环:

# 吃四个苹果,吃到第三个有虫,不吃了,吃第四个
i = 0
while i < 4:
    i += 1
    if i == 3:
        print(f"吃第{i}个苹果,有虫,不吃了,吃第四个")
        continue
    print(f"吃第{i}个苹果")

# break 和 continue 区别,break是终止所以循环,continue是跳出本次循环

三.

for循环:

# 循环打印python 每个单词
#       012345
str1 = "python"
print(str1[3])
# i 是临时变量 , 用来取值
for i in str1:
    print(i)

用for循环打印三角形

range的用法

# 用for循环打印五角星
1.
*
**
***
****
*****
for i in range(6):
    print("*" * i)

四.

课堂练习:猜拳游戏

'''
石头  1  ,剪刀   2 ,  布  3
电脑赢:1-2   2-3   3-1
平局: 1-1   2-2   3-3
我赢: 1-3   2-1   3-2
'''

import random
user = int(input("请输入猜拳数字,石头1,剪刀2,布3"))
# computer 是一个int类型
computer = random.randint(1,3)
print(computer,type(computer))

if user == computer:
    print("平局")
elif (computer == 1 and user == 3) or (computer == 2 and user == 1) or (computer == 3 and user == 2):
    print("用户赢")
else:
    print("电脑赢")

五.字符串操作

1.切片操作

#        0123456
name6 = "abcdefg"
#  第一个数字是起始位置
#  第二个位置上的是结束位置(不包含)
#  要前不要后
print(name6[2:5])
print(name6[2:5:1])  # 同上  第三个参数是步长 默认是1
print(name6[:5])  # 第一次参数不写,默认是0
print(name6[4:])  # 第二个参数不写,默认取到最后
print(name6[:])  # 第一个参数和第二个参数都不写,表示取整个字符串
print(name6[::2])  # 步长是2 表示跳着取0 2 4 6位置上的值
print(name6[-1])  # 倒数第一个值
print(name6[:-1])  # 从开始取到最后一个值,但是最后一个值不要
print(name6[-4:-2])  # 从倒数第四和第三取出
print(name6[::-1])  # 把字符串倒过来

2.字符串查找

# 字符串查找
#        012345678910
mystr = "hello world"
# 查找hello 查找到了返回  子串在原来字符串中的起始位置
result1 = mystr.find("hello")
print(result1)
result2 = mystr.find("world")
print(result2)
# 在字符串中查找一个不存在的值,返回-1
result3 = mystr.find("python")
print(result3)
# 第二个参数和第三个参数表示是 在哪一个区间查找
result4 = mystr.find("lo", 6, 10)  # 在6-10,查找不到lo
print(result4)
result5 = mystr.find("lo", 2, 6)  # 在2-6,可以找到lo
print(result5)

3.字符串查找index

# 字符串查找index
#         012345678910
mystr1 = "hello world"
# 查找到则返回第一个字符的位置下标
result6 = mystr1.index("hello")
print(result6)
result7 = mystr1.index("world")
print(result7)
# ValveError: substring not found
# 表示没有找到 则报错 报错会导致程序结束
# result8 = mystr1.index("python")
# print(result8)
# 在指定位置查找 找不到则报错
# result9 = mystr1.index("lo", 4, 8)
# print(result9)
result10 = mystr1.index("lo", 1, 8)
print(result10)

4.统计字符串次数

# 统计字符串次数count
com_apply = "This class is very good, so good, people good, so amazing"
# 计算good在字符串中的次数
result1 = com_apply.count("good")
print(result1)
# 24 , 50 是指定范围内查找good的次数
result2 = com_apply.count("good",24,50)
print(result2)
print('============')

5.修改字符串,或替代

# 修改字符串 replace
com_apply = "This class is very good, so good, people good, so amazing"
# 没有第三个参数的情况下将所有的good替换为bad
# 生成一个新的字符串 原来的字符串没有变化
# 第三个参数是次数
result3 = com_apply.replace("good","bad",2)
print(result3)
print(com_apply)

平平无奇的一天,继续努力。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值