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)
# 设置代码格式 不用学习 逆水行舟 你我皆是陌生人
相关推荐
5G微创业17 分钟前
Python / Node.js 调用短视频去水印 API 完整示例(含 SDK)
python·node.js·音视频·api·sdk·短视频
长不胖的路人甲19 分钟前
斐波那契查找Java 实现 + 完整思路
java·开发语言
星恒随风26 分钟前
C++ 继承进阶:默认成员函数、多继承、虚继承与组合设计
开发语言·c++·笔记·学习
Yeauty30 分钟前
用 Whisper 转录前,你不用再离开 Rust
开发语言·rust·ffmpeg·音视频·视频
电子云与长程纠缠32 分钟前
UE中使用TGuardValue与TInlineComponentArray数据结构
开发语言·数据结构·学习·ue5·游戏引擎
phltxy38 分钟前
LangGraph智能租房助手实践
大数据·人工智能·python·深度学习·语言模型·langchain
码银1 小时前
放弃了豆包,我使用Python做了一个桌面宠物
python·microsoft·宠物
呜喵王阿尔萨斯1 小时前
C/C++ const -- 多义混乱
c语言·开发语言·c++
__log1 小时前
幂等性设计:从“重复提交“到“稳如磐石“的系统防护
java·开发语言·spring boot
测功机之家1 小时前
国内口碑好的测功机公司找哪家
python·物联网