文章目录
- 课程简介
- 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)
- 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
- Which of the following methods are available for both Python lists and Python tuples?
- sort()
- append()
- index()
- reverse()
- pop()
- 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
- 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
- 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)
- 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
- 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)
- 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))
- 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
- 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 thembox-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)