【汇总】:https://blog.csdn.net/wistonty11/article/details/121348048
【2章:变量和简单数据类型】:https://blog.csdn.net/wistonty11/article/details/114553239
【3章:列表简介】:https://blog.csdn.net/wistonty11/article/details/114673314
【4章:列表操作】:https://blog.csdn.net/wistonty11/article/details/114684679
【5章:if语句】:https://blog.csdn.net/wistonty11/article/details/114932777
【6章:字典】https://blog.csdn.net/wistonty11/article/details/117432520
【7章:用户输入和while循环】:https://blog.csdn.net/wistonty11/article/details/117437656
【8章:函数】:https://blog.csdn.net/wistonty11/article/details/117448978
【9章:类】:https://blog.csdn.net/wistonty11/article/details/117521111
目录
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210308221346458.png)
《Python编程:从入门到实践》_2章:变量和简单数据类型
《Python编程:从入门到实践》_3章:列表简介
《Python编程:从入门到实践》_4章:列表操作
——————
1 遍历整个列表
motocycles = ['handa', 'yamaha', 'suzuki', 'ducadi']
for motocycle in motocycles:
print(motocycle)
>>>handa
yamaha
suzuki
ducadi
# 严格缩进,有冒号:缩进的在for循环里,没缩进的不在。
# for循环,motocycle是个变量,在motocycles变,输出这个变量
较复杂的变化:
motocycles = ['handa', 'yamaha', 'suzuki', 'ducadi']
for motocycle in motocycles:
print(motocycle+' is my favourite band!')
print('\n'+'I will buy one of them.')
>>>handa is my favourite band!
yamaha is my favourite band!
suzuki is my favourite band!
ducadi is my favourite band!
I will buy one of them.
# 用了\n 空了一行
2 创建数值列表
2.1 用range()
range: 一系列范围的意思
for value in range(1, 5):
print(value)
>>>1
2
3
4
# 范围1~5 其实【),左闭区间右开区间
# 他的形式是一系列数字,不是列表
- range范围1~5 其实【),左闭区间右开区间
- 他的形式是一系列数字,不是列表
- 用list将range生成的数字变成列表
numbers = list(range(1, 5))
print(numbers)
>>>[1, 2, 3, 4]
# 看外围有【】,在python里就是列表
【例题】创建一个列表,包含1~10平方和:
squares = []
# 先创建一个列表
for value in range(1, 11):
squares.append(value**2) # 用for循环和append尾部添加方法
print(squares)
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
2.2 对列表简单的统计(min, max, sum)
digits = [1,2,3,4,5,6,7]
print(min(digits))
print(max(digits))
print(sum(digits))
>>>1
7
28
2.3 列表解析
squares = [value**2 for value in range(1, 11)]
print(squares)
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
分析:
1. squares = [ ] 定义了一个列表;
2. value**2 是个表达式
3. for value in range(1, 11) value值从for循环里来,!!这里没有冒号;!!
3 切片
也就是使用列表的一部分
3.1【开始部分:结束部分】
也是左闭右开
squares = [value**2 for value in range(1, 11)]
print(squares)
#!!!!注意下面这句:
print(squares[1:4])
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[4, 9, 16]
分析:
1. print(squares[1:4]) 输出1号位到3号位:4, 9, 16
3.2 【:结束部分】
也是左闭右开,开头提取
squares = [value**2 for value in range(1, 11)]
print(squares)
#!!!!注意下面这句:
print(squares[:4])
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16]
3.3【开始部分:】
也是左闭右开,末尾结束提取
squares = [value**2 for value in range(1, 11)]
print(squares)
#!!!!注意下面这句:
print(squares[1:])
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[4, 9, 16, 25, 36, 49, 64, 81, 100]
3.4 利用切片输出后三个元素
squares = [value**2 for value in range(1, 11)]
print(squares)
#!!!!注意下面这句:
print(squares[-3:])
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[64, 81, 100]
3.5 遍历切片
squares = [value**2 for value in range(1, 11)]
#!!!!注意下面这句:
for square in squares[-3:]:
print(square)
>>> 64
81
100
注意两个输出形式,for下面每个i输出完,会换行
4 复制列表
这里要注意:复制成为两个列表 改变一个,另一个不受影响
结论:
切片方式复制的时候,一个列表改变不影响另一个列表;而列表直接等的赋值方式,一个列表改变,另一个列表也相应改变。
motocycles = ['handa', 'yamaha', 'suzuki', 'ducadi']
motocycles_copy1 = motocycles[:]
print("复制成为两个列表:"+ str(motocycles_copy1))
motocycles_copy2 = motocycles
print("复制成为一个列表:"+ str(motocycles_copy2))
print("------改变原始数据--------")
motocycles[3] = '川崎'
print("分片方式新数据没有改变:"+ str(motocycles_copy1))
print("列表直接=新数据跟着改变:"+ str(motocycles_copy2))
>>>复制成为两个列表:['handa', 'yamaha', 'suzuki', 'ducadi']
复制成为一个列表:['handa', 'yamaha', 'suzuki', 'ducadi']
------改变原始数据--------
分片方式新数据没有改变:['handa', 'yamaha', 'suzuki', 'ducadi']
列表直接=新数据跟着改变:['handa', 'yamaha', 'suzuki', '川崎']
5 元组
- 格式是( , )
- 只是定义格式不一样,输出等语法都一样
motocycles = ('handa', 'yamaha', 'suzuki', 'ducadi')
print(motocycles[3])
>>>ducadi
- 列表可以被修改,元组不行
motocycles = ('handa', 'yamaha', 'suzuki', 'ducadi')
motocycles[3] = '川崎'
print(motocycles[3])
报错:
5.1 遍历元组
print("-------1")
motocycles = ('handa', 'yamaha', 'suzuki', 'ducadi')
#print(motocycle for motocycle in motocycles)
for motocycle in motocycles:
print(motocycle)
>>>-------1
handa
yamaha
suzuki
ducadi
5.2 修改元组变量
虽然不能修改元组变量的元素,但是可以给存储元组的变量赋值
motocycles = ('handa', 'yamaha', 'suzuki', 'ducadi')
for motocycle in motocycles:
print(motocycle)
print("-----分割线----")
motocycles = ('川崎', '光阳')
for motocycle in motocycles:
print(motocycle)
>>>handa
yamaha
suzuki
ducadi
-----分割线----
川崎
光阳
6 容易犯的错:输出格式混用
motocycles = ['handa', 'yamaha', 'suzuki', 'ducadi']
motocycles_copy = motocycles[:]
print("复制全部列表:"+ motocycles_copy)
解决1:
motocycles_copy = motocycles[:]
print("复制全部列表:"+ str(motocycles_copy))
>>>复制全部列表:['handa', 'yamaha', 'suzuki', 'ducadi']
解决2:
motocycles = ['handa', 'yamaha', 'suzuki', 'ducadi']
motocycles_copy = motocycles[:]
print("复制全部列表:")
print(motocycles_copy)
>>>复制全部列表:
['handa', 'yamaha', 'suzuki', 'ducadi']