Python 列表
Python内置的一种数据类型是列表:list。list是一种有序的集合,可以随时添加和删除其中的元素。
这是一个简单的列表,列表里面有3个元素
students = [lloyd, alice, tyler]
用len函数来获取列表的个数
len(students)
通过列表的索引来访问列表
print students[0]
print students[1]
print students[-1]
往list中追加元素到末尾
students.append('Adam')
删除list末尾的元素
students.pop()
students.pop(1)
要把某个元素替换成别的元素
students[0] = 'Amy'
Python 条件判断和循环
条件判断
格式如图 通过条件判断对具体的分数赋等级
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
循环
for循环(1):for item in list 方法 1 对于遍历列表很有用,但无法以这种方式修改列表。 打印各个元素的名子和成绩,这里用到后面的字典
for student in students:
print student["name"]
score = get_average(student)
print score
print get_letter_grade(score)
for循环(2):循环访问索引 方法 2 使用索引遍历列表,以便在需要时也可以修改列表。
for turn in range(4):
board[guess_row][guess_col] = "X"
Python 字典
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。
创建一个字典
里面包含这位同学的名字和成绩
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
访问字典里的元素
print student["name"]
average(student["homework"])
Python 函数
Python内置了很多有用的函数,我们可以直接调用。如:abs(),max(),min()等等。 更多内置函数根据Python官网的文件
定义函数: 这里定义了一个计算平均成绩的函数
def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)
在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。
这里通过上面的函数计算出学生最后的加权成绩
def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
total = homework * 0.1 + quizzes * 0.3 + tests * 0.6
return total
引入Python的包 这里加入一个生成随机数的包 随机生成一个点的横纵坐标
from random import randint
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
实现包含3位同学的成绩管理
达到目的:用字典定义学生,用列表定义班级,计算学生成绩,得到等级以及班级平均分
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students = [lloyd, alice, tyler]
def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)
def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
total = homework * 0.1 + quizzes * 0.3 + tests * 0.6
return total
def get_letter_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
for student in students:
print student["name"]
score = get_average(student)
print score
print get_letter_grade(score)
def get_class_average(class_list):
results = []
for student in class_list:
student_avg = get_average(student)
results.append(student_avg)
return average(results)
print get_class_average(students)
运行结果
Lloyd
80.55
B
Alice
91.15
A
Tyler
79.9
C
83.8666666667
一个找战舰位置的游戏
内容:在5*5的区域随机确认一个点为战舰的位置,玩家通过输入位置判断是否正确(有4次机会)
from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
for turn in range(4):
print "Turn", turn + 1
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
break
else:
if guess_row not in range(5) or \
guess_col not in range(5):
print "Oops, that's not even in the ocean."
elif board[guess_row][guess_col] == "X":
print( "You guessed that one already." )
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
if turn == 3:
print "Game Over"
|