函数:raw_input()和input()

注意:在python3.x中,已经删除raw_input(),取而代之的是input(),当然这仅仅是重命名,用法还是一样。因此在这里介绍的是python2.x中的raw_input()和input(),在python3.x中只要按raw_input()的使用方式就行

1:作用:读取控制台的输入与用户实现交互

2:语法
raw_input([prompt]) 
input([prompt])

3:参数
prompt:如果存在此参数,则会直接输出到屏幕上,不会再往下另起一行

4:两者关系:
input()本质上是使用raw_input()来实现的,即调用完raw_input()之后再调用eval()函数,调用如下:
def input(prompt):
    return (eval(raw_input(prompt)))

5:两者相同点:
都能接受字符串、数字以及表达式作为输入。

6:两者差别:
6.1、当输入为字符串时:
raw_input(): 读取控制台的输入,同时返回字符串类型
input():    读取控制台的输入,但输入时必须使用引号括起来,否则会报错

6.2、当输入为纯数字时:
raw_input(): 读取控制台的输入,同时返回字符串类型,当作字符串处理
input():    读取控制台的输入,返回输入的数值类型(int, float)

6.3、当输入为字符串表达式时:
raw_input(): 读取控制台的输入,但不会对输入的数字进行运算,直接返回字符串类型,当作字符串处理
input():    读取控制台的输入,对合法的 python 数字表达式进行运算,返回运算后的结果

6.4、输入的为特殊字符时
比如'\t','\n'等
raw_input(): 读取控制台的输入,返回字符串类型,和输入一样
input():    读取控制台的输入,但输入时必须使用引号括起来,返回特殊符号所代表的内容

注:无特殊要求建议使用 raw_input() 来与用户交互

7:实例:
7.1、输入为字符串的时:

>>> a1 = raw_input("raw_input_str: ")
raw_input_str: hello
>>> print a1,type(a1)
hello <type 'str'>

>>> a2 = input("input_str: ")
input_str: hello
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a2 = input("input: ")
  File "<string>", line 1, in <module>
NameError: name 'hello' is not defined

>>> a2 = input("input_str: ")
input_str: 'hello'
>>> print a2,type(a2)
hello <type 'str'>

7.3、输入为字符串表达式时:

>>> c1 = raw_input("raw_input_exp: ")
raw_input_exp: 3 + 3
>>> print c1,type(c1)
3 + 3 <type 'str'>

>>> c2 = input("input_exp: ")
input_exp: 3 + 3
>>> print c2,type(c2)
6 <type 'int'>

7.4、输入的为特殊字符时:

>>> d1 = raw_input("raw_input_sp: ")
raw_input_sp: \t
>>> print d1,type(d1)
\t <type 'str'>

>>> d2 = input("input_sp: ")
input_sp: \t
Traceback (most recent call last):
  File "<pyshell#57>", line 1, in <module>
    d2 = input("input_sp: ")
  File "<string>", line 1
    \t
    ^
SyntaxError: unexpected character after line continuation character

>>> d2 = input("input_sp: ")
input_sp: '\t'
>>> print d2,type(d2)
 <type 'str'>

Python自用笔记的更多相关文章

  1. Web Scraping with Python读书笔记及思考

    Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...

  2. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  3. VS2013中Python学习笔记[Django Web的第一个网页]

    前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...

  4. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  5. python datetime笔记

    python datetime笔记 http://mint-green.diandian.com/post/2011-09-09/4892024 获取当前时间,并通过字符串输出. 格式为:%Y-%m- ...

  6. python学习笔记(六)文件夹遍历,异常处理

    python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...

  7. python学习笔记--Django入门四 管理站点--二

    接上一节  python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...

  8. python学习笔记--Django入门0 安装dangjo

    经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...

  9. python学习笔记(一)元组,序列,字典

    python学习笔记(一)元组,序列,字典

随机推荐

  1. Zookeeper-Watcher机制与异步调用原理

    转载于:http://shift-alt-ctrl.iteye.com/blog/1847320 Watcher机制:目的是为ZK客户端操作提供一种类似于异步获得数据的操作. 1)在创建Zookeep ...

  2. mysql:赋予用户权限、查看及修改端口号

    一.mysql 赋给用户权限 grant all privileges on *.* to joe@localhost identified by '1'; flush privileges; 即用u ...

  3. emoji错误:ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value:

    1 前言 由于mysql数据库要存储微信昵称,但是当微信昵称带有emoj表情会出现标题的错误. 然后发现是emoj编码是4个字节保存的,于mysql数据库编码格式utf8默认保存的是1到3个字节. 2 ...

  4. 15)django-ORM(多对多关系)

    django ORM多对多关系使用 一:多对多关系创建 多对多关系创建:分为两种情况,手动创建和django自动创建 1)手动创建:自定义关系表 通过自定义表,通过models.ForeignKey创 ...

  5. Python-Mongodb vs mysql

    mongodb https://www.cnblogs.com/kermitjam/articles/10147254.html#_label5 centos安装mongodb: https://bl ...

  6. JsonResponse

    1.JsonResponse class JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, json_dumps_params=None ...

  7. swift 实践- 14 -- UIScrollView

    import UIKit class ViewController: UIViewController ,UIScrollViewDelegate{ override func viewDidLoad ...

  8. Swift可选项

  9. FFmpeg 常用指令集合

    不精确裁剪视频 # -ss 指定从什么时间开始 # -t 指定需要截取多长时间 # -i 指定输入文件 ffmpeg -ss 00:00:00 -t 00:00:30 -i test.mp4 -vco ...

  10. oracle 12c ORA-01017:invalid username/password; logon denied

    Oracle 12C 中,想通过操作系统认证登录Oracle 数据库,有一些要注意的地方.不然就会遇到 ORA-01017:invalid username/password; logon denie ...