Python 入门学习第三课

python 复制代码
# 遍历列表
# 通常使用for循环 格式 for 自己定义的名字:列表:
# 冒号不要忘记了 在这之后 需要缩进 如果不缩进 将进行一次
message = ['zahngsan','lisi','wangwu']
for wq in message:
    print(wq)
# 练习题
message = ['xiande','tiande','choude']
for wq in message:
    print(wq)
for wq in message:
    print(f" I like {wq} pizza")
print("I like relly pizza")
animals = ['pig','cat','dog']
for an in animals:
    print(an)
for an in animals:
    print(f"A {an} would make a great pet")
print(" Any of these animals would make a great pet!")
# 创建数值列表
# list(range())
# range(1,6) --->> 1 2 3 4 5 其实没有六 差一原则
# range(6) ------------>> 0 1 2 3 4 5
# 还能根据步长设置数值列表 range(1,10,2) --->>>3 5 7 9
# python中 乘方就是平方定义为**
# 同时可以对列表就行最大值,最小值,求和等快捷运算
wq = list(range(2,6))# range 左边为起始处数值 右边为小于的 [2,6)
print(wq)
now = []
for ss in range(1,9):
    now.append(ss ** 2)
print(now)
print(max(now))
print(min(now))
print(sum(now))
# 比较重要的一点 列表推导式 我感觉不好用
now = [value ** 2 for value in range(1,6)]# 有意思的是 for中没有冒号
print(now)
# 练习题
now = list(range(1,20,2))
for wq in now:
    print(wq)
now = list(range(3,31,3))
for wq in now:
    print(wq)
now = []
for wq in range(1,11):
    now.append(wq ** 3)
print(now)
now = [wq ** 3 for wq in range(1,11)]
print(now)
# 切片
now = ['pet','pig','uyy','trrt','yyt','tyfgfd']
print(now)
print(now[1:3])
# 复制列表
yesterday = now[:]
print(yesterday)
# 一种有趣的表达方式 以下的方法 都同时指向了同一个列表
future = now
print(future)
future.append('nihao')
print(future)
print(now)
now.append('gg')
print(future)
print(now)
print(now[-3:])
# 元组
# Python将值不变的称为元组 类似于线性代数的矩阵
# 不能直接修改元组的值 可以通过重新赋值元组的值来进行改变
now = (1,2,3,4)
print(now[0])
for wq in now:
    print(wq)
# 设置代码格式 不用学习 逆水行舟 你我皆是陌生人
相关推荐
黑客笔记8 小时前
python小工具win7翻车事故现场
python
pps-key9 小时前
网络攻防入门:从“黑客梦”到合法实操(生存篇)
开发语言·web安全·php
汤姆宝库10 小时前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
岁月宁静10 小时前
从零拆解 AI 语音模拟面试系统
人工智能·python·agent
benchmark_cc10 小时前
个人开发者接入五档盘口:用 QuantDash 读取单只股票的验收清单与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
ZhengEnCi11 小时前
项目越写越乱?先别改架构:用一次目录重排 + 一份可复算的体检脚本把乱治住
python·架构·ai编程
驭渊的小故事12 小时前
订单服务如何调用商品服务?一次 RestTemplate 跨服务调用入门
java·开发语言·php
电科_银尘12 小时前
【深度学习项目实战·22】灰度图像着色:用小型U-Net学习从灰度到RGB
人工智能·深度学习·学习
Dmz丶12 小时前
前端转 AI 100 天 Day 45:阶段复盘——从“会调 API”到“做出 AI 产品”
python
Sakura_j_h_12 小时前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver