GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing raw.githubusercontent.com — raw.githubusercontent.com #字符串(2)
##raw_input和print
自从本课程开始以来,我们还没有感受到computer姑娘的智能。最简单的智能应该体现在哪里呢?想想小孩子刚刚回说话的时候情景吧。
>小孩学说话,是一个模仿的过程,孩子周围的人怎么说,她(他)往往就是重复。看官可以忘记自己当初是怎么学说话了吧?就找个小孩子观察一下吧。最好是自己的孩子。如果没有,就要抓紧了。
通过python能不能实现这个简单的功能呢?当然能,要不然python如何横行天下呀。
不过在写这个功能前,要了解两个函数:raw_input和print
>这两个都是python的内建函数(built-in function)。关于python的内建函数,下面这个表格都列出来了。所谓内建函数,就是能够在python中直接调用,不需要做其它的操作。
Built-in Functions
--------------------------------------------------------------
|abs() | divmod() | input()| open()| staticmethod()|
--------------------------------------------------------------
|all() | enumerate() | int() | ord() | str()|
--------------------------------------------------------------
|any() | eval() | isinstance()| pow()| sum()|
--------------------------------------------------------------
|basestring() | execfile() | issubclass() | print() | super()|
--------------------------------------------------------------
|bin() | file() | iter()| property()| tuple()|
--------------------------------------------------------------
|bool() | filter() | len() | range() | type()|
--------------------------------------------------------------
|bytearray() | float()| list() | raw_input()| unichr()|
--------------------------------------------------------------
|callable() | format() | locals() | reduce() | unicode()|
--------------------------------------------------------------
|chr() | frozenset() | long() | reload() | vars()|
--------------------------------------------------------------
|classmethod()| getattr()| map() | repr() | xrange()|
--------------------------------------------------------------
|cmp() | globals()| max()| reversed()| zip()|
--------------------------------------------------------------
|compile() |hasattr() | memoryview()| round() | __import__()|
--------------------------------------------------------------
|complex() |hash() | min()| set() | apply()|
--------------------------------------------------------------
|delattr() |help()| next()| setattr()| buffer()|
--------------------------------------------------------------
|dict() | hex() |object() |slice() | coerce()|
--------------------------------------------------------------
|dir() | id() |oct() |sorted() |intern()|
--------------------------------------------------------------
这些内建函数,怎么才能知道哪个函数怎么用,是干什么用的呢?
不知道你是否还记得我在前面使用过的方法,这里再进行演示,这种方法是学习python的法宝。
>>> help(raw_input)
然后就出现:
Help on built-in function raw_input in module __builtin__:
raw_input(...)
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled. The prompt string, if given,
is printed without a trailing newline before reading.
从中是不是已经清晰地看到了`raw_input()`的使用方法了。
还有第二种方法,那就是到python的官方网站,查看内建函数的说明。https://docs.python.org/2/library/functions.html
其实,我上面那个表格,就是在这个网页中抄过来的。
例如,对`print()`说明如下:
print(*objects, sep=' ', end='\n', file=sys.stdout)
Print objects to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Output buffering is determined by file. Use file.flush() to ensure, for instance, immediate appearance on a screen.
分别在交互模式下,将这个两个函数操练一下。
>>> raw_input("input your name:")
input your name:python
'python'
输入名字之后,就返回了输入的内容。用一个变量可以获得这个返回值。
>>> name = raw_input("input your name:")
input your name:python
>>> name
'python'
>>> type(name)
|
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |