Python-Project-4-学习笔记

[TOC]

本次的重点是:通过 list for if 等语法功能,实现数据整理的功能

尤其是 if 中的 条件表达式 的语法

代码实践验证

  • 请基于本任务已有的代码,完成代码以找到整个数据集中,免费应用程序的平均评分
  • for 循环中,将应用程序的价格以浮点数类型分配给名为price的变量。
  • 如果为price == 0.0,则使用list_name.append()将存储在rating中的值附加到free_apps_ratings列表中
  • for 循环主体之外,计算免费应用程序的平均评分,并将结果分配给名为avg_rating_free的变量。
from csv import reader

app_data_set = list(reader(open('assets/dataset/AppleStore.csv')))[1:]
free_apps_ratings = []
avg_rating_free = 0
price = '0'
for x in app_data_set:
price = x[4]
if(price == '0.0'):
free_apps_ratings.append(float(x[7]))
avg_rating_free = sum(free_apps_ratings)/len(free_apps_ratings)
print(avg_rating_free)

代码实践验证

将变量 a_price 初始化为 0,通过使用if语句将以下句子转录为代码:

  • 如果 a_price 等于0,则打印字符串’This is free’(请记住使用==运算符进行判断)。
  • 如果 a_price 等于1,则打印字符串’This is not free’
a_price = 0
if a_price == 0:
print("This is free")
elif a_price == 1:
print("This is not free")

代码实践验证

基于计算免费应用程序的代码,计算非免费应用程序的平均评分:

  • 新建名为non_free_apps_ratings的空列表
  • 更改条件if price == 0.0以解决我们现在只想获取非免费应用程序评分的问题。
  • 更改free_apps_ratings.append(rating)以确保将评分添加到non_free_apps_ratings这个新列表中。
  • non_free_apps_ratings列表中的值相加并除以该列表的长度,以此来计算平均值,并将结果分配给avg_rating_non_free
from csv import reader

app_data_set = list(reader(open('assets/dataset/AppleStore.csv')))[1:]
non_free_apps_ratings = []
avg_rating_non_free = 0
price = '0'
for x in app_data_set:
price = x[4]
if(price != '0.0'):
non_free_apps_ratings.append(float(x[7]))
avg_rating_non_free = sum(non_free_apps_ratings)/len(non_free_apps_ratings)
print(avg_rating_non_free)

代码实践验证

基于计算游戏应用程序的代码,计算非游戏应用程序的平均评分

  • 创建一个名为non_games_ratings的空列表
  • 遍历apps_data,每次循环中需要执行:
    • 提取应用程序的名称,并将其分配给名为name的变量
    • 提取应用程序的评分,并将其以浮点数类型分配给名为 rating 的变量
    • 提取应用程序的分类,并将其分配给名为genre的变量
    • 如果应用程序的分类不等于字符串'Games',则将存储在rating附加到non_games_ratings
  • 计算非游戏应用程序的平均评分,并将结果分配给avg_rating_non_games
  • 打印avg_rating_non_games
from csv import reader

app_data_set = list(reader(open('assets/dataset/AppleStore.csv')))[1:]
non_games_ratings = []
avg_rating_non_games = 0

name = ''
rating = '0'
genre = ''

for x in app_data_set:
name = x[1]
rating = x[8]
genre = x[11]
if(genre != 'Games'):
non_games_ratings.append(float(x[7]))
avg_rating_non_free = sum(non_games_ratings)/len(non_games_ratings)
print(avg_rating_non_free)

代码实践验证

基于计算非游戏应用程序的代码,编写计算免费游戏平均评分

  • 创建一个名为non_games_ratings的空列表。
  • 遍历apps_data,每次循环中需要执行:
    • 提取应用程序的名称,并将其分配给名为name的变量。
    • 提取应用程序的价格,并将其分配给名为price的变量。
    • 提取应用程序的评分,并将其以浮点数类型分配给名为 rating 的变量。
    • 提取应用程序的分类,并将其分配给名为genre的变量。
    • 如果应用程序的分类等于字符串'Games'且价格等于 0.0,则将存储在rating附加到avg_rating_free_games
  • 计算免费游戏的平均评分,并将结果分配给avg_rating_free_games
  • 打印avg_rating_free_games
from csv import reader

app_data_set = list(reader(open('assets/dataset/AppleStore.csv')))[1:]
free_games_ratings = []
avg_rating_free_games = 0

name = ''
price = '0'
rating = '0'
genre = ''

for x in app_data_set:
name = x[1]
price = x[4]
rating = x[8]
genre = x[11]
if(genre == 'Games' and price == '0.0'):
free_games_ratings.append(float(x[7]))
avg_rating_free_games = sum(free_games_ratings)/len(free_games_ratings)
print(avg_rating_free_games)

代码实践验证

先思考以下代码输出是什么,再实际让计算机输出,比较你和计算机的答案是否一样:

  • print(True or True and True)
  • print(True and False or False)
  • print(False or True and True)
  • print(False and False or True and False or True)

代码实践验证

计算类型为“社交网络”或“游戏”的非免费应用程序平均评分

from csv import reader

app_data_set = list(reader(open('assets/dataset/AppleStore.csv')))[1:]
out_list = []
avg_out = 0
# name = x[1]
# price = x[4]
# rating = x[8]
# genre = x[11]
for x in app_data_set:
if(x[4] != '0.0' and (x[11] == 'Games' or x[11] == 'Social Networking')):
out_list.append(float(x[7]))
avg_out = sum(out_list)/len(out_list)
print(avg_out)

代码实践验证

  • 有多少个应用的价格大于 9
  • 价格大于 9 的应用程序的平均评分是多少?
from csv import reader

app_data_set = list(reader(open('assets/dataset/AppleStore.csv')))[1:]
out_list = []
avg_out = 0
# name = x[1]
# price = x[4]
# rating = x[8]
# genre = x[11]
for x in app_data_set:
if(float(x[4]) > 9):
out_list.append(float(x[8]))
avg_out = sum(out_list)/len(out_list)
print("有 %d 个应用的价格大于 9" % len(out_list))
print("价格大于 9 的应用程序,他们的平均评分是:%f" % avg_out)

代码实践验证

编写代码,根据价格将每个应用标记为“免费”或“非免费”。

  • for 循环中:
  • 如果应用程序的价格为0.0
    • 则在当前迭代变量最后加上字符串’free’
    • 即意味着将应用程序标记为“免费”
  • 否则,
    • 在当前迭代变量最后加上字符串’non_free’
    • 即意味着将应用程序标记为“non_free”
  • 通过在每行的末尾添加标签
  • 我们基本上创建了一个新列。
  • 这个时候,你要增加一个表头
  • 即将字符串’free_or_not’添加到apps_data数据集的第一行,
  • 记得这个操作是要在 for 循环之外完成的哦。
  • 打印标题行和前五行,以查看我们所做的一些更改。
from csv import reader

app_data_set = list(reader(open('assets/dataset/AppleStore.csv')))
# out_list = []
# avg_out = 0
# name = x[1]
# price = x[4]
# rating = x[8]
# genre = x[11]
for x in app_data_set[1:]:
if(x[4] == '0.0'):
x.append('free')
else:
x.append('non_free')
app_data_set[0].append('free_or_not')
for x in app_data_set[0:6]:
print(x)

语法知识汇总

恭喜,你已经掌握了:

  • 条件语句及其子句(if,else,elif)
  • 逻辑运算符(and,or)
  • 比较运算符(==,!=,>,>=,<,<=)

我们一起回顾刚刚遇到的困难和总结出来的经验:

语法

  1. 使用if语句控制代码:
if True:
print(1)
if 1 == 1:
print(2)
print(3)

2.结合多个条件:

if 3 > 1 and 'data' == 'data':
print('Both conditions are true!')
if 10 < 20 or 4 <= 5:
print('At least one condition is true.')

3.构建更复杂的if语句:

if (20 > 3 and 2 != 1) or 'Games' == 'Games':
print('At least one condition is true.')

4.使用else子句:

if False:
print(1)
else:
print('The condition above was false.')

5.使用elif子句:

if False:
print(1)
elif 30 > 5:
print('The condition above was false.')

概念

  • 我们可以使用if语句在代码中实现条件判断。
  • elif语句内的代码仅在同时满足以下2个情况下执行:
    • 前面的if语句或前面的elif语句解析为 False
    • 在elif关键字后的条件计算结果为 True
  • True和False是布尔值。
  • and和or是逻辑运算符,它们将两个或多个布尔值桥接在一起。
  • 我们可以将一个值A与另一个值B进行比较,以确定:
    • A是等于对B,反之亦然(B等于A)。
    • A是不相等到B,反之亦然。
    • A是更大的比B,或反之亦然。
    • A是大于或等于B,或反之亦然。
    • A是少比B,或反之亦然。
    • A是小于或等于 B,或反之亦然。

image-20201020141012229