# range中定步长range(start, end, step)
members = list(range(2,10,2))
print(members) #[2, 4, 6, 8]
# 两个星号**表示乘方
squares = []
for val in range(1,10): # 不包含右闭包
square = val **2
squares.append(square)
print(squares) #[1, 4, 9, 16, 25, 36, 49, 64, 81]
# the statements equal to follow
squares = [value**2 for value in range(1,10)] # 列表解析
print(squares)
# 几个专门处理数字列表的Python函数
digits = [3, 1, 2, 6, 4, 5, 9, 7, 8, 0]
print(min(digits)) # 0
print(max(digits)) # 9
print(sum(digits)) # 45
# 列表切片(即使用索引截取列表范围,注意不包含右闭包),下标从0开始
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:3]) # ['martina', 'michael']
print(players[2:]) # ['michael', 'florence', 'eli']
print(players[:3]) # ['charles', 'martina', 'michael']
for player in players[1:3]:
print(player)
# 列表复制
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:] # 正确,产生副本
friend_foods = my_foods # 错误,指向同一个列表
# 求列表元素个数用len()
print("my_foods has {0} elements".format(len(my_foods)))
# 列表具有pop()函数
# 以下检查一个列表是否为空
question_wait_to_answer = ['A', 'B', 'C',]
while question_wait_to_answer:
cur_question = question_wait_to_answer.pop()
print(cur_question)
# 不可变的列表称为元组
my_friends = ('xiaoming', 'xiaohong')
#my_friends[0] = 'xiaochen' # 错误,不可修改
# 虽然不能修改元组元素值,但可以给整个元组变量重新赋予元组
my_friends = ('charles', 'martina')
for friend in my_friends:
print(friend)
# 集合set类似列表,但每个元素值必须唯一
score = [98, 97, 96, 91, 90, 96]
print(set(score))
# Python中用==来检查是否相等,用!=检查是否不相等
# 使用and / or 来连接多个检查条件# 检查特定值是否不包含再列表中,用not in
colors = ['red', 'green', 'blue']
cur_color = 'yellow'
if cur_color not in colors:
print("%s is not a original color"%(cur_color))
# 在if 语句中将列表名用在条件表达式中时,Python将在列表至少包含一个元素时返回True ,并在列表为空时返回False# Python中字典是用大括号括起来的一系列键值对,任何Python对象都可以用作Python的值
cur_stu = {'name':'ali', 'age':18, 'sex':'male'}
print("My name is {0}, my age is {1} and my sex is {2}".format(cur_stu['name'], cur_stu['age'], cur_stu['sex']))
cur_stu['address'] = '广州市天河区'
del cur_stu['sex']
print(cur_stu)
# Python中字符串拼接运算符+
# Python中对字符大小写敏感
# Python中遍历字典用items()
for k,v in cur_stu.items():
print("key is {0}, it's value is {1}".format(k,v))
# 遍历字典的所用键用keys(),遍历字典中的所有值用values()
for attr in cur_stu.keys():
print(attr.title())
for val in cur_stu.values():
print(val)
# 可以在列表中嵌套字典、在字典中嵌套列表甚至在字典中嵌套字典
# 函数input()让程序暂停运行,等待用户输入一些文本,Python将其存储于变量以供后续使用
# 数字字符串转化为整形用int()
age = input("How old are you?")
if int(age) > 18:
print("you are a adult.")
else:
print("you are a juveniles.")
# Python中使用3个双引号括起来,表示文档字符串注释,用来生成程序中有关函数的文档
def my_func():
"""this is my_func, here is it's description"""
print("this is my_func")
my_func()
# 有时候需要禁止函数修改列表,此时可将列表的副本作为参数传入函数
def process_model(uncompleted_models, finished_models):
while uncompleted_models:
cur_model = uncompleted_models.pop()
finished_models.append(cur_model)
print("finished models list is {0}".format(finished_models))
return None
unfinished = ['modelA', 'modelB', 'modelC']
completed = []
process_model(unfinished[:], completed)
print("original unfinished models list is {0}".format(unfinished))
# 模块化编程,函数归集到文件里,调用时导入文件采用import py文件名
# 也可以再导入模块时制定需要的任意数量的函数
# 例如 from my_model import func_a, func_b, func_c
# 给函数制定别名用as,例如 from my_model import my_func as mf
# 当调用时,使用 mf(a, b)
# from my_model import * #表示导入模块中的所用函数,不推荐此种方式