1.python缩进很重要,语句与上下有关联时,一旦要注意缩进,特别是for遍历
#for遍历
#遍历一道100万的数
#for i in range(1,1000001):
# print(i)
#1到100万的和
print(sum(list(range(1,1000001))))#list()是小写,不是[],sum()接收list,tuple,dict
print('-----------------------------------')
#计算1到9
print(sum(list(range(1,10))))
print('-----------------------------------')
#sum函数神奇的用法
a = range(1, 11)
b = range(1, 10)
c = sum([item for item in b if item in a])#神奇,没搞懂
print (c)
print('-----------------------------')
#奇数
for i in range(1,100,2):
print(i)
print('-----------------------------')
#立方
#for i in range(1,11):
# a = i * i * i
# print(a)
#立方解析
print ([i * i * i for i in range(1,11)])#记住加上[],否则是错的
print('-------------------------------------')
for i in [i * i * i for i in range(1,11)]:
print(i)
2.数字列表: range(a,b,c),左有右没有,c参数步长,有返回值list(range(a,b)),专门处理数字列表的函数,max(),min(),sum()
3.列表解析:简化语句
squares = [value ** 2 for value in range(1,11)]
print(squares)
4.切片,访问工具,对象[ : ]
#切片
m = 'I love the world'
print(m)
print(m[1:])
print(m[:-5])
#复制列表[:]
n = m[:]
print(n)
5.元组,括号声明,除了不能修改,除非整体修改
dinner =('西红柿炒鸡蛋','凉拌鸡','烧牛肉')
print(dinner)
#dinner [1] ='炒青菜'#不允许修改
dinner =('西红柿炒鸡蛋','炒青菜','烧牛肉')#允许整体修改
print(dinner)
``