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 print()函数详讲
开发语言·python
奶思图米球16 分钟前
Python多环境管理
开发语言·python
Aerelin19 分钟前
iframe讲解(爬虫playwright的特殊应用)
前端·爬虫·python·html
JienDa20 分钟前
JienDa聊PHP:基于协同架构的PHP主流框架优势整合与劣势补救策略
开发语言·架构·php
i***395823 分钟前
JAVA系统中Spring Boot 应用程序的配置文件:application.yml
java·开发语言·spring boot
时光追逐者32 分钟前
C# 中 ?、??、??=、?: 、?. 、?[] 各种问号的用法和说明
开发语言·c#·.net·.net core
量化Mike34 分钟前
【python报错】解决卸载Python时报错问题:No Python installation was detected
开发语言·python
丝斯201134 分钟前
AI学习笔记整理(21)—— AI核心技术(深度学习5)
人工智能·笔记·学习
q***017737 分钟前
PHP进阶-在Ubuntu上搭建LAMP环境教程
开发语言·ubuntu·php
q***017740 分钟前
Java进阶学习之路
java·开发语言·学习