Quiz 10: Tuples | Python for Everybody 配套练习_解题记录

news2024/10/7 9:27:29

文章目录

  • 课程简介
      • Quiz 10: Tuples
    • 单选题(1-11)
    • 编程题
      • Exercise 10.2


课程简介

Python for Everybody 零基础程序设计(Python 入门)

  • This course aims to teach everyone the basics of programming computers using Python. 本课程旨在向所有人传授使用 Python 进行计算机编程的基础知识。
  • We cover the basics of how one constructs a program from a series of simple instructions in Python. 我们介绍了如何通过 Python 中的一系列简单指令构建程序的基础知识。
  • The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should be able to master the materials in this course. 该课程没有任何先决条件,除了最简单的数学之外,避免了所有内容。任何具有中等计算机经验的人都应该能够掌握本课程中的材料。
  • This course will cover Chapters 1-5 of the textbook “Python for Everybody”. Once a student completes this course, they will be ready to take more advanced programming courses. 本课程将涵盖《Python for Everyday》教科书的第 1-5 章。学生完成本课程后,他们将准备好学习更高级的编程课程。
  • This course covers Python 3.

在这里插入图片描述

coursera

Python for Everybody 零基础程序设计(Python 入门)

Charles Russell Severance
Clinical Professor

在这里插入图片描述

个人主页
Twitter

在这里插入图片描述

University of Michigan


课程资源

coursera原版课程视频
coursera原版视频-中英文精校字幕-B站
Dr. Chuck官方翻录版视频-机器翻译字幕-B站

PY4E-课程配套练习
Dr. Chuck Online - 系列课程开源官网


Quiz 10: Tuples

The tuple is a Python data structure that is like a simple and efficient list.


单选题(1-11)

  1. What is the difference between a Python tuple and Python list?
  • Lists are indexed by integers and tuples are indexed by strings
  • Tuples can be expanded after they are created and lists cannot
  • Lists are mutable and tuples are not mutable
  • Lists maintain the order of the items and tuples do not maintain order
  1. Which of the following methods are available for both Python lists and Python tuples?
  • sort()
  • append()
  • index()
  • reverse()
  • pop()
  1. What will end up in the variable y after this code is executed?
x , y = 3, 4
  • 3
  • A two item list
  • A dictionary with the key 3 mapped to the value 4
  • A two item tuple
  • 4
  1. In the following Python code, what will end up in the variable y?
x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
y = x.items()
  • A tuple with three integers
  • A list of integers
  • A list of tuples
  • A list of strings
  1. Which of the following tuples is greater than x in the following Python sequence?
x = (5, 1, 3)
if ??? > x :
   ...
  • (6, 0, 0)
  • (0, 1000, 2000)
  • (4, 100, 200)
  • (5, 0, 300)
  1. What does the following Python code accomplish, assuming the c is a non-empty dictionary?
tmp = list()
for k, v in c.items() :
    tmp.append( (v, k) )
  • It computes the average of all of the values in the dictionary
  • It computes the largest of all of the values in the dictionary
  • It sorts the dictionary based on its key values
  • It creates a list of tuples where each tuple is a value, key pair
  1. If the variable data is a Python list, how do we sort it in reverse order?
  • data = sortrev(data)
  • data.sort(reverse=True)
  • data.sort.reverse()
  • data = data.sort(-1)
  1. Using the following tuple, how would you print ‘Wed’?
days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
  • print(days{2})
  • print[days(2)]
  • print(days[1])
  • print(days[2])
  • print(days(2))
  • print(days.get(1,-1))
  1. In the following Python loop, why are there two iteration variables (k and v)?
c = {'a':10, 'b':1, 'c':22}
for k, v in c.items() :
    ...
  • Because the keys for the dictionary are strings
  • Because the items() method in dictionaries returns a list of tuples
  • Because for each item we want the previous and current key
  • Because there are two items in the dictionary
  1. Given that Python lists and Python tuples are quite similar - when might you prefer to use a tuple over a list?
  • For a temporary variable that you will use and discard without modifying
  • For a list of items that want to use strings as key values instead of integers
  • For a list of items you intend to sort in place
  • For a list of items that will be extended as new items are found

编程题

Exercise 10.2

题目:
Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages.
You can pull the hour out from the 'From ’ line by finding the time and then splitting the string a second time using a colon.

From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008

Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.

[Desired Output]

04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1

解题代码:

>name = input("Enter file:")
if len(name) < 1:
   name = "mbox-short.txt"
handle = open(name)

d = dict()
for line in handle:
   ls = line.split()
   if line.startswith('From') and len(ls) == 7 :
       lh = ls[5].split(':')
       d[lh[0]] = d.get(lh[0], 0) + 1

lkv = sorted([(k, v) for k, v in d.items()], reverse=False)

for k, v in lkv:
   print(k, v)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/704203.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【软件测试】Java+selenium环境搭建

目录 1.下载Chrome浏览器&#xff0c;查看浏览器的版本 2.根据浏览器版本下载驱动 根据电脑版本下载驱动&#xff1a; 3.去maven仓库寻找selenium驱动 4.在idea中创建一个项目 1.在pom.xml中添加依赖 2.点击右侧刷新按钮 3.在Java下创建一个类Main 4.将以下代码写入 5.…

B061-ES6 NodeJS npm Vue

目录 ECMAScript6什么是ECMAScriptECMAScript历史语法申明变量解构表达式箭头函数模块化 npm引出nodejs安装VUEvue简介配置Terminalvue入门vue属性-elvue属性-datavue属性-methods vue架构认识vue表达式vue-表达式-基础vue-表达式-操作对象&数组 vue-指令v-text & v-ht…

软件工程师,入门下深度学习吧

概述 ChatGPT,英文全称为Chat Generative Pre-trained Transformer,是OpenAI研发的聊天机器人程序。ChatGPT是人工智能技术驱动的自然语言处理工具,它能够通过理解和学习人类的语言来进行对话,还能根据聊天的上下文进行互动,真正像人类一样来聊天交流。除此之外,还能进行…

kettle架构图

2、架构说明 1&#xff09;最底层的是kettle的核心引擎层&#xff0c;相关的jar在lib目录下。 2&#xff09;中间是开发层&#xff0c;在开发阶段我们接触最多的就是通过spoon进行开发&#xff0c;通过Spoon.bat或者spoon.sh即可启动客户端&#xff0c;开发文件调试之前要先保…

使用vtk创建立方体,设置顶点为不同颜色

引言 改示例为官网上的例子。创建了一个顶点是不同颜色的立方体。 示例 开发环境 使用QtCreator4.11.2,Qt5.14.2。使用的vtk9.2的库及其头文件。创建空项目。 示例代码 其pro文件中的内容&#xff1a; QT core guigreaterThan(QT_MAJOR_VERSION, 4): QT widgets…

【半监督图像分类 ICLR 2023】FreeMatch

【半监督图像分类 ICLR 2023】FreeMatch 论文题目&#xff1a;FREEMATCH: SELF-ADAPTIVE THRESHOLDING FOR SEMI-SUPERVISED LEARNING 中文题目&#xff1a;Freematch&#xff1a;用于半监督学习的自适应阈值 论文链接&#xff1a;https://arxiv.org/abs/2205.07246 论文代码&a…

数字经济下的架构新图景—2023架构·可持续未来峰会(北京主会场)成功举办!

2023 年 6 月 29日&#xff0c;由The Open Group主办的2023架构可持续未来峰会&#xff08;北京主会场&#xff09;在机械工业出版社圆满落幕。 本次大会以“可持续未来”为主题&#xff0c;采取13&#xff0c;即北京主会场上海/成都/深圳三个城市峰会场模式&#xff0c;聚焦架…

android h5 宿舍报修管理系统myeclipse开发mysql数据库编程服务端java计算机程序设计

一、源码特点 android h5 宿舍报修管理系统是一套完善的WEBandroid设计系统&#xff0c;对理解JSP java&#xff0c;安卓app编程开发语言有帮助&#xff08;系统采用web服务端APP端 综合模式进行设计开发&#xff09;&#xff0c;系统具有完整的 源代码和数据库&#xff0c;系…

idea 修改Tool Windows后新建项目又恢复默认设置

我们可能会根据自己的喜好修改idea的工具窗口,但是每次新建项目它又会重置了,解决办法如下:

vscode C++开发记录

vscode C开发记录 插件管理Linux 平台针对编译好的程序进行 Dedug 插件管理 Linux 平台 针对编译好的程序进行 Dedug 右击源文件后&#xff0c;添加debug 配置 Add Debug Configuration 这是一个示例的调试配置文件&#xff0c;用于在VS Code中使用GDB调试已经编译好的程序 …

Sublime Text 初步使用

Sublime Text &#xff0c;最初被设计为一个具有丰富扩展功能的Vim。 Sublime Text具有漂亮的用户界面和强大的功能&#xff0c;例如代码缩略图&#xff0c;Python的插件&#xff0c;代码段等。还可自定义键绑定&#xff0c;菜单和工具栏。Sublime Text 的主要功能包括&#xf…

Vue常见面试题整理

一、对于MVVM的理解&#xff1f; MVVM是Model-View-ViewModel的缩写。 Model&#xff1a;代表数据模型&#xff0c;也可以在Model中定义数据修改和操作的业务逻辑。View&#xff1a;代表UI组件&#xff0c;它负责将数据模型转化成UI展现出来。ViewModel&#xff1a;监听模型数…

Equivariant Graph Neural Networks

论文链接&#xff1a;https://arxiv.org/abs/2102.09844 一种新模型来学习与旋转、平移、反射和排列等变的图神经网络&#xff0c;称为 E(n)-等变图神经网络 (EGNN) 尽管深度学习在很大程度上取代了手工制作的特征&#xff0c;但许多进步严重依赖于深度神经网络中的归纳偏差。…

linux watch命令

在Linux中&#xff0c;有时需要每隔一段时间比如一秒或者两秒刷新一次&#xff0c;这时就可以使用watch 命令。如图&#xff1a; 每隔两秒刷新查看一下磁盘使用情况。

Chrome DevTools常用功能指南

目录 Elements Styles DOM结构 增删属性 模拟元素的伪状态&#xff0c;方便调试 Computed Layout Event Listeners Network Application 资源列表&#xff08;可改&#xff09;本地存储Cookie、WebStorage&#xff08;localStorage、sessionStorage&#xff09; Sourc…

【Linux进程】进程状态 {进程状态的介绍,进程状态的转换,Linux中的进程状态,浅度睡眠VS深度睡眠,僵尸进程VS孤儿进程,调度器的作用}

进程状态 一、基本进程状态 1.1 进程状态介绍 创建状态&#xff1a;当一个进程被创建时&#xff0c;它处于创建状态。在这个阶段&#xff0c;操作系统为进程分配必要的资源&#xff08;将代码和数据拷贝到内存&#xff0c;创建PCB结构体等&#xff09;&#xff0c;并为其分配一…

SpringBoot开发Restful风格的接口实现CRUD功能

基于SpringBoot开发一个Restful接口 前言一、什么是SpringBoot&#xff1f;二、实战---基于SpringBoot开发一个Restful接口1.开发前的准备工作1.1 添加相关依赖 &#xff08;pom文件&#xff09; 1.2 创建相关数据库和表1.3 数据库配置文件 2.实战开发---代码逻辑2.1 实体类2.2…

数据结构 | 顺序队列

一、数据结构定义 typedef int QueueType; typedef struct seqQueue {int MAXNUM; // 队列中能存放的最大元素个数int front, rear; // 队列的队首&#xff0c;队尾QueueType element[100]; // 存放连续空间的起始地址 } *SeqQueue; 二、方法概览 SeqQueu…

软件测试不行了?2023软件测试行情分析

1 绪论 本文先对互联网对时代和社会变革进行了论述&#xff0c;然后再由互联网时代对软件工业模式变革进行了介绍&#xff0c;最后引出附属于软件工业的测试行业在新形势下的需求变化&#xff0c;并对趋势进行了分析&#xff0c;并最终给出了相关的从业人员的职业发展建议。…

《PyTorch深度学习实践》第五讲 用PyTorch实现线性回归

b站刘二大人《PyTorch深度学习实践》课程第五讲用PyTorch实现线性回归笔记与代码&#xff1a;https://www.bilibili.com/video/BV1Y7411d7Ys?p5&vd_sourceb17f113d28933824d753a0915d5e3a90 PyTorch官网教程&#xff1a;https://pytorch.org/tutorials/beginner/pytorch_w…