Python系列之笨方法学Python是我学习《笨方法学Python》—Zed A. Show著

的学习思路和理解,如有不如之处,望指出!!!

文章主要分为三个部分:

  1. 原文—摘录至《笨方法学Python》第三版

  2. 学习中遇到的问题

  3. 问题的解决方法

  4. 附加练习

原文—摘录至《笨方法学Python》第三版

&

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# ex13.py

from
script, first, second, third=argv
print
print
print
print

# ex14.py

from
script, user_name =argv
prompt='> '

print
print
print
likes=raw_input(prompt)

print
lives=raw_input('> '

print
computer=raw_input(prompt)

print
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice!
"""

应该看到的结果


原文作者不建议使用python的IDLE运行程序,推荐使用windows下的Powershell(命令行)

不清楚为什么不建议使用IDLE


1
2
3
4
5
> python ex13.py 1
The script is
Your first variable is
Your second variable is
Your third variable is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> python ex14.py darer
Hi darer, I'm the ex14.py script.
I'd like to ask you a few questions.
Do
> yes

Where
> china

What kind of
> lenovo

Alright, so you said 'yes' about liking me.
You live in
And

需要注意的几个地方

  1. 怎么在当前文件夹运行Powershell

利用命令行指令 cd

  1. 在当前文件夹 shift+鼠标右键 选择“打开powershell"

  2. 注意ex14.py中,raw_input()的用法

  3. 为什么ex14.py 中,键入的字符没有单引号,但是输出的结果中却有单引号(’ ')呢? 因为%r是调试专用,它输出的是“原始表示”出来的字符 而%s是为了给用户显示

附加练习

以下摘录自原文

  1. 是否可以用双引号定义prompt变量的值?

  2. argv和raw_input()有什么不同? 如果参数是在用户执行命令时就要输入的,那就是argv 如果是在脚本运行过程中需要用户输入,那就使用raw_input()

  3. argv就是所谓的”参数变量“(argument variable),这个变量保存着你运行Python脚本时传递给Python脚本的参数。

  4. 脚本中出现import语句,非常重要,它用于导入模块(module)