1 Python根据缩进来进行判断代码行与前一个代码行的关系
for name in names:
print(name)
names = ['baker','david','philp','rose'] for name in names: print(name.title()+',that was a great trick!') print('I cannot wait to see your next trick '+ name.title()+' ') print('thank you,everyone,That was a great magic show!')
2 使用函数range() 范围 比如range(1,5)
打印的是1-4
for number in range(1,5): print(number)
numbers=list(range(1,5)) print(numbers) even_numbers=list(range(2,11,2)) print(even_numbers) squares=[] for value in range(1,11): squares.append(value**2) print(squares) digits=[1,2,3,4,5,6,7,8,9,10] print(min(digits)) print(max(digits)) print(sum(digits))
3 列表切片
plays=['baker','dingzong','xiaocui','philp'] print(plays[0:3])
4 不可变的列表叫做 元组
元组看起来犹如列表,但是使用圆括号二不是方括号来标识。
dimensions=(200,50) print(dimensions[0]) print(dimensions[1]) for dimension in dimensions: print(dimension)
可以通过重新给元组赋值的方法,修改元组的值
dimensions=(400,100) for dimension in dimensions: print(dimension)
用于存储生命周期内不可变的程序
5 设置代码格式 PEP8
1)每级缩进4个空格
2)行长不超过80字符
3)适当的空行