栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

学习Python 03天

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学习Python 03天

01-while循环

# 使用while循环实现 0-100 相加
i = 0
s = 0
while i < 100:
    i += 1
    s += i
print(s)

'''
第1次循环 i = 0,s += i = 0 + 0 = 0,s = 0,i = 1
第2次循环 i = 1,s += i = 0 + 1 = 1,s = 1,i = 2
第3次循环 i = 2,s += i = 1 + 2 = 3,s = 3,i = 3
。。。
第99次循环 i = 99,s += i = 4851 + 99 = 4950,s = 4950,i = 100
第100次循环 i = 1,s += i = 4950 + 100 = 5050,s = 5050,i = 101
'''

'''
1、 将0-100所有3的倍数相加
2、 将0-100所有3的倍数相加 含3相加
'''
i = 0
s = 0
while i <= 100:
    if i % 3 == 0 or i % 10 == 3 or i // 10 ==3:
        s += i
    i += 1
print(s)

02-break终止循环

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

print("================================")

# 吃四个苹果 吃到第三个就饱了 不吃第四个
i = 1
while i <= 4:
    print(f"吃第{i}个苹果")
    if i == 3:
        break
    i += 1

print("================================")

03-continue

i = 0
while i < 4:
    i += 1
    if i == 3:
        print(f"这个苹果有虫子")
        continue
    print(f"吃第{i}个苹果")

04-for循环

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

print("n===========================")

'''
while循环取出y和o
'''
i = 0
str1 = "python"
while i < 6:
    if i == 1 or i == 4:
        print(str1[i], end=' ')
    i += 1

print("n===========================")

# 打印James 跳过 m
for i in "James":
    if i == "m":
        continue
    print(i, end=' ')

05-猜拳游戏

'''
石头 (1) 剪刀 (2) 布 (3)
电脑赢:
平局:1-1,2-2,3,3
玩家赢:1-3,2-1,3-2
'''

import random
user = int(input("请输入猜拳数字"))
# computer是一个int类型
computer = random.randint(1, 3)
print(computer, type(computer))
print(computer, "-", user)
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("电脑赢")

06-range的用法

i = 1
while i <= 5:
    print(i)
    i += 1

print("===========================")

for i in range(1, 6):
    print(i)

print("===========================")
'''
随堂练习
1、打印
*
**
***
****
*****
2、打印
*
***
*****
'''

for i in range(1, 6):
    print("*" * i)

print("===========================")

for i in range(1, 6):
    if i % 2 == 1:
        print("*" * i)

07-字符串操作

# 定义一个字符串 字符串使用双引号或者单引号定义
a = "Hello Python"
b = 'abcdef'
print(type(a))
print(type(b))

name1 = 'petter'
name2 = "park"
name3 = '''human'''
name4 = """batter 
man"""

print(type(name1))
print(type(name2))
print(type(name3))
print(type(name4))
# 格式化字符串
print(f"你的名字是{name1,name2}")

print("=======================================")

#       01234   字符下标,或者叫做索引
name = "simon"
print(name[0])  # 取s
print(name[2])  # 取m
print(name[4])  # 取n

print("=======================================")

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

print("=======================================")

# 字符串查找 find
#        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 位置在3
print(result5)

print("=======================================")

# 字符串查找index 用法与find类似
mystr = "hello world"
# 查找到则返回第一个字符的下标
ret1 = mystr.index("hello")
print(ret1)
ret2 = mystr.index("world")
print(ret2)
# ValueError: substring not found
# 表示没有找到 则报错 报错会导致程序结束
# ret3 = mystr.index("python")
# print(ret3)
# 在指定位置上查找 找不到 则报错
# ret4 = mystr.index("lo", 4, 8)
# print(ret4)
ret5 = mystr.index("lo", 1, 8)
print(ret5)

10-字符串操作

# 统计字符串出现次数

com_apply = "This class is very good, its so good, its amazing"
# 计算good在字符串中的次数
result1 = com_apply.count("good")
print(result1)
# 计算good 从第24位到50位的次数
result2 = com_apply.count("good",24 , 50)
print(result2)

print("============================================")

# 修改字符串 replace
com_apply = "This class is very good, its so good, its amazing"
# 将所有的good 替换为bad 生成一个新的字符串 没有第三个参数时 原来的字符串没有变化
# 有第三个参数时,第三个参数是替换多少次
result3 = com_apply.replace("good", "bad", 1)
print(result3)
print(com_apply)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744851.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号