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

python课程3

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

python课程3

1.作业回顾

# # 练习1
# age = int(input("请输入一个年龄:"))
# if age >= 22 and age <= 35:
#     print("结婚年龄")
# else:
#     print("爬")
#     print("=======================")
#
# # 练习2
# age = int(input("请输入年龄:"))
# sick = input("请输入是否有老年痴呆(是输入1,不是输入2):")
# # 只要满足一个条件就执行
# if age >= 70 or sick == "1":
#     print("不能开车")
# else:
#     print("可以开车")
#     print("========================")
# # 练习1
# i = 1
# while i <= 5:
#     print("*" * i)
#     i += 1
#     print("========================")
# # 练习2
# j = 1
# while j <= 5:
#     print("*" * j)
#     j += 2
#     print("========================")
# 练习3
j = 1
while j <= 5:
    #取余操作 当j是奇数的时候 执行被打印
    if j % 2 == 1:
        print("*" * j)
    j += 1

2.作业回顾

j = 1
while j <= 5:
    #取余操作 当j是奇数的时候 执行被打印
    if j % 2 == 1:
        print("*" * j)
    j += 1

3.while循环

# 使用while循环实现0~100 相加
# 1.定义一个整数记录循环的次数
i = 1
sum = 0
while i <= 100:
    # sum=sum+i
    sum += i
    i += 1
    print(sum)
'''
第一次循环 i=1,sum=0,~~sum=sum+i=0+1=1,sum=1,i=2
第二次循环 sum=1,i=2,~~sum=sum+i=1+2=3,sum=3,i=3
第三次循环 sum=3,i=3,~~sum=sum+I+3+3=6,sim=6,i=4
......
第九十九次循环 sum=4051,i=99,~~sum=sum+i=4051+99=4950,sum=4950,i=100
第一百次循环sum=4950,i=100,~~sum=sum+i=4950+100=5050,sum=5050,i=101
第一百零一次循环 不满足条件结束循环
'''
'''
1随堂练习0-100所有3的倍数相加
2将0-100所有3的倍数相加 含有3(13.23.37)的相加
3基数排序思想:先判断十位数上的大小,在排个位数上的大小
46,37,42
37,46,42
37,42,46
使用基数排序思想给上面三个数进行排序,从大到小,打印出来
a=46,b=37,c=42
4.归并排序
a=46,b=37,c=42,d=39
把ab放在一组,把cd放一组
ab选出较大,cd选出较大值,然后,再把ac放在一起比较,
选出谁是最大打印出来,同样的方法选出最小的打印出来
'''

4.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
print("==================")


5.fro循环
'''
# 循环打印python每个单词
str1 = "python"
# i是临时变量,用来取值
for i in str1:
    print(i)

课堂练习
1使用while循环取出y和o打印出来

j = 0
while j < 6:
    if j == 1 or j == 4:
        print(str1[j])
        j += 1
print("==================")
'''
#使用for 打印bobboy 跳过b
for i in "babboy":
    if i=="a":
        continue
    print(i,end="")

6.countinue跳出本次循环

i = 0
while i < 4:
    i += 1
    if i == 3:
        print("这个苹果有毒!你个牛马")
        continue
    print(f"吃第{i}个苹果")

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

7.猜拳游戏

# 猜拳游戏
# 石头(1)剪刀(2)布(3)
# 电脑赢:1~2 2~3 3~1
# #平局:1~1 2~2 3~3
# #我赢:1~3 2~1 3~2
import random

usre = int(input("请输入猜拳数字石头(1)剪刀(2)布(3):"))
# computer是一个int类型
computer = random.randint(1, 3)
print(computer, "-", usre)
if usre == computer:
    print("平局")
elif (computer == 1 and usre == 3) or (computer == 2 and usre == 1) or (computer == 3 and usre == 2):
    print("用户赢")
else:
    print("电脑赢")

8.range的用法

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

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

9.字符串操作

"""
# 定义一个字符串 字符串使用双引导或者单引号定义
a = "hello python"
b = "xiaoniuma"
print(type(a))
print(type(b))

name1 = '貂蝉'
name2 = "西施"
name3 = '''西施'''
name4 = """"""
name5 = '''蒸鸡'''
print(type(name1))
print(type(name2))
print(type(name3))
print(type(name4))
print(type(name5))
print(f"你的名字是{name1}")
print("===================")
# 01234字符串下标或者叫做索引
name = "simon"
print(name[0])  # 取s
print(name[2])  # 取m
print(name[3])  # 取n
print("=========================")
print("====     ======     =====")
print("===       ===        ====")
print("====       =        =====")
print("======            =======")
print("=======        ==========")
print("=========================")
# 切片0123456
name6 = "sb"
# 第一个数字是开始为(包含)
# 第二个位置上的之是结束为(不包含)
# 要前不要后
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])#把字符串倒过来
"""
#字符串查找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)
print(result4)
result5=mystr.find("lo",2,6)
print(result5)

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

10.字符串操作

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

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

11.课堂练习

'''
# 课堂练习
# 1. 将0-100 所有的3的倍数相加
# 2. 将0-100 所有的3的倍数 含有3(13,23,37)的相加
# 3. 基数排序
46,37,42
37,46,42
37,42,46
使用基数排序思想给上面三个数进行排序,从大到小,打印出来
a = 46 ,b = 37 ,c = 42
4. 归并排序
a = 46 , b = 37 , c = 42 ,d = 39
把ab放在一组,把cd放在一组
ab选出较大,cd选出较大值,然后 再把ac放在一起比较,选出谁是最大的打印出来
同样方法选出最小的打印出来
'''

# 练习1
i = 0
sum = 0
while i <= 100:
    if i % 3 == 0:
        sum += i
    i += 1
print(sum)
print("==========")

# 练习2

i = 0
sum = 0
while i <= 100:
    if i % 3 == 0 or i // 10 == 3 or i // 100 == 3:
        sum += i
    i += 1
print(sum)
print('==========')

# 练习3
a = 46
b = 37
c = 42
lst1 = [a, b, c]
lst1.sort()
lst2 = [b, a, c]
lst2.sort()
lst3 = [b, c, a]
lst3.sort()
print(lst1, lst2, lst3)
print('==========')
a = 46
b = 37
c = 42
d = 39
x_value = a
if x_value < b:
    x_value = b
if x_value < c:
    x_value = c
if x_value < d:
    x_value = d
print(x_value)
print('=========')
x_value = a
if x_value > b:
    x_value = b
if x_value > c:
    x_value = c
if x_value > d:
    x_value = d
print(x_value)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744301.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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