习题 11:提问python
print "How old are you?", age = raw_input() print "How tall are you?", height = raw_input() print "How much do you weight?", weight = raw_input() print "So,you're %r old,%rtall and %r heavy." %( age,height,weight)
运行结果:linux
How old are you? 35 How tall are you? 6'2'' how much do you weight? 180lb So,you're '35' old,"6'2''"tall and '180lb' heavy.
加分题:shell
1. raw_input 无论用户输入什么类型的都会转变成字符型浏览器
2. 另:input会根据用户输入变换相应的类型,并且若是要输入字符和字符串的时候必需要用引号包起来。服务器
3. 本身再写一段相似格式的:函数
print "How many times did you go to the park?", times = raw_input() print "How often do you go home a week?", fluence = raw_input() print "What food is you favorite?", food = raw_input() print """So, you went the park %r times,go home %r times a week, like %r very much."""%(times,fluence,food)
运行结果:工具
How many times did you go to the park? 4 How often do you go home a week? 3 What food is you favorite? fish So, you went the park '4' times,go home '3' times a week, like 'fish' very much.
习题 12:提示别人spa
y = raw_input("Name?") # Name? 起到了提示别人的做用
再练习一次习题11的内容:操作系统
age = raw_input("How old are you? ") height = raw_input("How tall are you?") weight = raw_input("How much do you weight?") print "So you're %r old, %r tall and %r heavy."%( age,height,weight)
运行结果:命令行
How old are you? 35 How tall are you?6'2'' How much do you weight?180lb So you're '35' old, "6'2''" tall and '180lb' heavy.
加分题:
1. 在Linux中的命令行输入pydoc raw_input 的结果是
2. pydoc 命令是文档生成工具:
python -m pydoc raw_input S C:\Documents and Settings\jdu> python -m pydoc pydoc - the Python documentation tool pydoc.py <name> ... Show text documentation on something. <name> may be the name of a Python keyword, topic, function, module, or package, or a dotted reference to a class or function within a module or module in a package. If <name> contains a '\', it is used as the path to a Python source file to document. If name is 'keywords', 'topics', or 'modules', a listing of these things is displayed. pydoc.py -k <keyword> Search for a keyword in the synopsis lines of all available modules. pydoc.py -p <port> Start an HTTP server on the given port on the local machine. pydoc.py -g Pop up a graphical interface for finding and serving documentation. pydoc.py -w <name> ... Write out the HTML documentation for a module to a file in the current directory. If <name> contains a '\', it is treated as a filename; if it names a directory, documentation is written for all the contents. PS C:\Documents and Settings\jdu>
让咱们来看一个使用大部分元素的简化示例:
清单 1: 附带典型文档的模块 mymod.py
#!/usr/bin/python """Show off features of [pydoc] module This is a silly module to demonstrate docstrings """ __author__ = 'David Mertz' __version__= '1.0' __nonsense__ = 'jabberwocky' class MyClass: """Demonstrate class docstrings""" def __init__ (self, spam=1, eggs=2): """Set default attribute values only Keyword arguments: spam ― a processed meat product eggs ― a fine breakfast for lumberjacks """ self.spam = spam self.eggs = eggs
pydoc 模块利用了 Python 文档的约定,又使用了一些有关 Python 导入、继承和其它相似的实用知识。此外, pydoc 有绝对的天赋可使本身在不一样的操做模式下被使用(立刻就能看到更多有关这个论点的资料)。让咱们用一些时间,看看经过 OS 命令行调用的 manpage 风格的用法。
假设您已将上述模块 mymod 安装在您的系统上,但不知道它有什么用处(在示例中并很少)。您能够阅读源代码,不过更简单的方法多是:
清单 2:获取‘manpage’风格的文档
% pydoc.py mymod Python Library Documentation: module mymod NAME mymod - Show off features of [pydoc] module FILE /articles/scratch/cp18/mymod.py DESCRIPTION This is a silly module to demonstrate docstrings CLASSES MyClass class MyClass | Demonstrate class docstrings | | __init__(self, spam=1, eggs=2) | Set default attribute values only | | Keyword arguments: | spam ― a processed meat product | eggs ― a fine breakfast for lumberjacks DATA __author__ = 'David Mertz' __file__ = './mymod.pyc' __name__ = 'mymod' __nonsense__ = 'jabberwocky' __version__ = '1.0' VERSION 1.0 AUTHOR David Mertz
根据特定的平台和安装过程,上述样本可能会显示在一个容许滚屏、搜索等功能并突出显示某些关键字的文本查看器中。对于像这样简单的示例,只是比纯粹的阅读源代码好一点。但请考虑一下像下面这样简单的示例:
清单 3:检查类的继承结构
% cat mymod2.py from mymod import MyClass class MyClass2(MyClass): """Child class""" def foo(self): pass % pydoc.py mymod2.MyClass2 Python Library Documentation: class MyClass2 in mymod2 class MyClass2(mymod.MyClass) | Child class | | __init__(self, spam=1, eggs=2) from mymod.MyClass | | foo(self)
在这个快速报告中,咱们能够知道 MyClass2 有 __init__() 和 foo() 方法(以及相应的参数),哪一个方法是类自身实现的以及其它哪些方法是继承而来(以及被继承的类所处的位置)。
另外一个美妙的相似于 manpage 的功能是用来在模块中搜索关键字的 -k 选项。例如:
清单 4:为任务定位适当的模块
% pydoc.py -k uuencode uu - Implementation of the UUencode and UUdecode functions. % pydoc.py uu Python Library Documentation: module uu NAME uu - Implementation of the UUencode and UUdecode functions. [...]
pydoc 除了它的命令行用法以外,还有其它四种“模式”能够显示被生成的一样的文档。
Shell 模式:在 Python 交互式 shell 中,您能够导入 pydoc 的 help() 函数,这样就可以在不离开交互式会话的状况下得到任何对象的帮助。也能够只输入一个 help 进入交互式“help 解释器”。例如:
清单 5:shell 模式下的交互式 help 解释器
#------- Interactive shell with help enhancements ------# >>> from pydoc import help >>> import uu >>> help(uu.test) Help on function test in module uu: test() uuencode/uudecode main program >>> help Welcome to Python 2.0! This is the online help utility. [...introductory message about help shell...] help>
Web 服务器模式:仅使用 -p 选项, pydoc 就会在 LOCALHOST 上做为一个简单的 Web 服务器自启动。您可使用任何 Web 浏览器浏览全部已安装在现有操做系统上的模块。这个服务器的主页是一张模块列表,根据目录(并用浏览器支持的醒目色块)将它们分组。此外,您查看其文档的每一个模块也普遍分布着它导入的函数、方法以及指向任何模块的连接。
HTML 生成器模式: -w 选项对于 pydoc 能够归档的任何文档都能生成 HTML 文档页面。这些页面与您在 Web 服务器模式下可能会浏览到的页面本质上是一回事,但页面是静态的,能够进行存档、传输等等。
TK 浏览器模式: -g 选项将建立一个和 xman 或 tkman 风格很类似的“图形帮助浏览器。”
http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-19/
------------------------------------------------------------------------------------------------------------------------------------------------------
来自网页:http://m.douban.com/note/316798580
4. 使用pydoc 再看一下open,file,os,和sys的含义,通读了解。
习题 13:参数、 解包、变量
from sys import argv script,first,second,third=argv print "The script is called:",script print "Your first variable is:",first print "Your second variable is:",second print "Your third variable is:",third
运行结果:
$ python ex13.py first 2nd 3rd The script is called: ex13.py Your first variable is: first Your second variable is: 2nd Your third variable is: 3rd
加分题:
习题 14:提示和传递
from sys import argv script,user_name = argv prompt = '>' print "Hi %s,I'm the %s script." %(user_name,script) print "I'd like to ask you a few questions." print "Do you like me %s?" % user_name like = raw_input(prompt) print "Where do you live %s?" % user_name lives = 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. """%(likes,lives,computer)
运行结果: