Coursera课程: Interactive Python part I自学内容
因为毕业设计老师想让我用Python做一个用户端程序,所以我决定自学Python以及面向对象编程。因此我用三天学完了Coursera上的Interactive Python 课程。这门课程介绍了如何使用Python语言创建交互式程序,比如说Pong游戏、Asteroid游戏等。
课程分为三个部分:Lectures/Quizzes/Mini Projects.
Lectures由老师讲课介绍了许多Python语言的特性和用法,以及怎么通过CodeSkulptor(需要翻墙)编写Python交互式程序。完成Quizzes和Mini Projects则是检验学习的最好方式。1
Lectures的收获
完成一个Python交互式程序,需要按照以下七个步骤写出程序:
- Globals (statement)
- Helpful Functions
- Classes()
- Define Event Handlers
- Create a Frame
- Register Event Handlers
- Start Frame & Timers
Quizzes的收获
在测试中,我学习到了Python语言中对于全局变量(Global)和局部变量(Local)的定义。
Question 1
Assume the following global definition is part of your program.
x = 5
If each of the following function definitions are also part of your program, which of them needs a global x declaration?
选项A
def c(y):
return x + y
# 不需要声明为全局变量,因为只是调用了x,没有对x重新赋值。
选项B
def d(y):
y = x + y
return y
# 不需要声明为全局变量,因为只是调用了x,没有对x重新赋值。
选项C
def b(x,y):
x = x + y
return x
# 需要声明为全局变量,因为要对x赋值。
选项D
def a(y):
x = x + y
return y
# 需要声明为全局变量,因为要对x赋值。
Correct Answer: C D
Question 2
Consider the following code.
a = 3
b = 6
def f(a):
c = a + b
return c
Which names occur in the global scope?
Correct answers : a b f
Question 3
Consider the following code.
a = 3
b = 6
def f(a):
c = a + b
return c
Which names occur in a local scope?
Correct answers : a c
Python语言中对于and和or的优先级顺序。
Question 4
Which expressions are the same as:
a or b and c
1 : a or b and not c
2 : a or (b and c)
3 : (a or b) and c
4 : b and c or a
Correct answer: 2 4
Python语言的and和or比较中有“短路”原则:(参考资料:Python 里 and、or 的计算规则是怎样的?)
or表达式:只要or的左边表达式返回了真值,就会终止对右边表达式取值,直接返回左边表达式的值。
and表达式:比较and左右两边表达式,当它们均为真值,就返回右边表达式的值作为and表达式整体的值,如果有一边为假值则只会返回假值。
比如 0 or 5 and 7 or 0
求值过程是 先看or表达式,取左值 0,没有短路,取右值,是and表达式,再取and表达式左值 5,为真,再取and表达式右值,是个or表达式,取左值,7,为真,短路之,后面不管了,返回7,and表达式左右都是真,返回右值,or 表达式最后返回右值,就是7
>>> print 0 or 5 and 7 or 0
>>> 7
还遇到了一个公式:
Question 5
The Python code below uses a timer to execute the function update() 10 times, computing a good approximation to a common mathematical function. Examine the code, and run it while varying the input value n.
What is the common name for what this computes?
# Mystery computation in Python
# Takes input n and computes output named result
import simplegui
# global state
result = 1
iteration = 0
max_iterations = 10
# helper functions
def init(start):
"""Initializes n."""
global n
n = start
print "Input is", n
def get_next(current):
"""??? Part of mystery computation."""
return 0.5 * (current + n / current)
# timer callback
def update():
"""??? Part of mystery computation."""
global iteration, result
iteration += 1
# Stop iterating after max_iterations
if iteration >= max_iterations:
timer.stop()
print "Output is", result
else:
result = get_next(result)
# register event handlers
timer = simplegui.create_timer(1, update)
# start program
init(13)
timer.start()
Correct Answer: Square root of n
Mini Projects的收获
课程中一共完成了四个小游戏:
- Rock-paper-scissors-lizard-Spock
- Guess the number
- Stopwatch: The Game
- Pong
这些代码都非常简单,最后一个小游戏也不超过200行。如果需要代码可以向我要。
下周开始学习Algorithms或者Introduction to Java Programming的课程,都需要安装编程环境,时间可能会拖得久一点。
- CodeSkulptor是由Scott Rixner教授开发的,基于Javascript编写的,可在浏览器上直接写Python代码的编程环境。 ↩