Python的输入语句类型

1 python2的输入语句

在python2中有两种常见的输入语句,input()和raw_input()。

(1)input()函数

可以接收不同类型的参数,而且返回的是输入的类型。如,当你输入int类型数值,那么返回就是int型;其中字符型需要用单引号或双引号,否则,报错。

a.数值型输入

>>> a = input()
10
>>> type(a)
<type 'int'>
>>> a
10
>>> a = input()
1.23
>>> type(a)
<type 'float'>
>>> a
1.23

b.字符类型

如果输入的字符不加引号,就会报错

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

正确的字符输入

>>> r = input()
'hello'
>>> r
'hello'
>>> r = input()
"hello"
>>> r
'hello'

当然,可以对输入的字符加以说明

>>> name = input('please input name:')
please input name:'Tom'
>>> print 'Your name : ',name
Your name : Tom

(2)raw_input()

函数raw_input()是把输入的数据全部看做字符类型。输入字符类型时,不需要加引号,否则,加的引号也会被看做字符。

>>> a = raw_input()
1
>>> type(a)
<type 'str'>
>>> a
''
>>> a = raw_input()
'hello'
>>> type(a)
<type 'str'>
>>> a
"'hello'"

如果想要int类型数值时,可以通过调用相关函数转化。

>>> a = int(raw_input())
1
>>> type(a)
<type 'int'>
>>> a
1
>>> a = float(raw_input())
1.23
>>> type(a)
<type 'float'>
>>> a
1.23

在同一行中输入多个数值,可以有多种方式,这里给出调用map() 函数的转换方法。map使用方法请参考python-map的用法

>>> a, b = map(int, raw_input().split())
10 20
>>> a
10
>>> b
20
>>> l = list(map(int, raw_input().split()))
1 2 3 4
>>> l
[1, 2, 3, 4]

(3)input() 和raw_input()的区别

通过查看input()帮助文档,知道input函数也是通过调用raw_input函数实现的,区别在于,input函数额外调用内联函数eval()。eval使用方法参考Python eval 函数妙用

>>> help(input)
Help on built-in function input in module __builtin__: input(...)
input([prompt]) -> value Equivalent to eval(raw_input(prompt)). >>> help(eval)
Help on built-in function eval in module __builtin__: eval(...)
eval(source[, globals[, locals]]) -> value Evaluate the source in the context of globals and locals.
The source may be a string representing a Python expression
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mapping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it.

2 Python3输入语句

python3中的输入语句只有input()函数,没有raw_input();而且python3中的input()函数与python2中的raw_input()的使用方法一样。

>>> a = input()
10
>>> type(a)
<class 'str'>
>>> a
''

python2 和 pyhton3 输入语句写法的更多相关文章

  1. python最简洁的条件判断语句写法

    这篇文章主要介绍了Python返回真假值(True or False)小技巧,本文探讨的是最简洁的条件判断语句写法,本文给出了两种简洁写法,需要的朋友可以参考下 如下一段代码: def isLen(s ...

  2. discuz 万能SQL查询调用语句写法

    首先在最底层source\class\table写入底层安全调用文件例如:table_common_friendlink.php 代码: <?php /** * [Discuz!] (C)200 ...

  3. Java的输入语句以及本周对于文件读写的研究

    日期:2018.9.20 博客期:010 星期四 ##:今天下午要考试 java(小考)!那么,我就应对相应的方法给出策略吧! 首先是 Java 里的输入语句,我一般是用Scanner类,用这个之前要 ...

  4. mysql \c 终止 mysql输入语句模式

    \c 遇到这种情况怎么退出mysql 输入语句模式? mysql> select -> 输入\c退出 mysql> select -> \c mysql> 另外一种情况 ...

  5. 常用的MySQL语句写法

    常用的MySQL语句写法 MySQL的SQL语句写法,除了那些基本的之外,还有一些也算比较常用的,这里记录下来,以便以后查找.     好记性不如烂笔头,这话说的太有道理了,一段时间不写它,还真容易忘 ...

  6. 复杂Linq语句写法

    从网上收藏的复杂Linq语句写法 1.case when: 原型: sql原型: SELECT ProductID, Name, Color, CASE WHEN Color = 'Red' THEN ...

  7. Python2.3-原理之语句和语法

    此节来自于<Python学习手册第四版>第三部分 一.python语句简介(第10章) 1.首先记得一个概念:a.程序由模块构成:b.模块包含语句:c.语句包含表达式:d.表达式建立并处理 ...

  8. mysql中获取一天、一周、一月时间数据的各种sql语句写法

    今天抽时间整理了一篇mysql中与天.周.月有关的时间数据的sql语句的各种写法,部分是收集资料,全部手工整理,自己学习的同时,分享给大家,并首先默认创建一个表.插入2条数据,便于部分数据的测试,其中 ...

  9. python2.7入门---条件语句

        前段时间呢,把MongoDB的基础内容了解的差不多了.接下来,就开始学习python2.7的基础内容喽.接着前面的知识点来学习.首先,来看一下条件语句.Python条件语句是通过一条或多条语句 ...

随机推荐

  1. 用brew安装gcc48

    由于mac自带的gcc的版本过低,因此想自己装一个新的gcc. 在网上搜索了一圈后发现用brew install安装比较简单,但可能由于本地的brew有冲突,因此网上的攻略都没有效果. 通过在gith ...

  2. ModelSim高级使用进阶_1_do文件和批处理文件使用_Camp

    https://wenku.baidu.com/view/50fb251914791711cc7917fd.html https://wenku.baidu.com/view/73187dcefe47 ...

  3. Xilinx DDR3 IP核使用问题汇总(持续更新)和感悟

    一度因为DDR3的IP核使用而发狂. 后来因为解决问题,得一感悟.后面此贴会完整讲述ddr3 ip的使用.(XILINX K7) 感悟:对于有供应商支持的产品,遇到问题找官方的流程.按照官方的指导进行 ...

  4. Chap 2 Representing and Manipulating Information (CS:APP)

    -------------------------------------------------------------------------------------------------- A ...

  5. QT错误笔记-Qt Creator needs a compiler set up to build. Configure a compiler in the kit options.

    上午在linux环境下,使用QT编译一段C++代码,出现下列错误: 最近在stackoverflow上找到了答案: i was also having the same problem so what ...

  6. grub.conf文件说明

    default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title Red Hat Enterprise Linux ...

  7. eslint — js书写规范

    一.安装 npm install -g eslint 安装eslint 编辑器安装插件eslint(具体安装方法根据不同编辑器而不同) 二.使用 使用方法一: eslint --init npm中用命 ...

  8. scala 学习(三)——Array和ArrayBuffer

    ArrayBuffer:类型化数组 首先需要注意的是Array是定长数组,而ArrayBuffer是可变数组.下面是一个小例子,对数据进行便利和修改的操作.

  9. MII接口概念简述

    MII: Medium Independent Interface 媒体独立接口,也称介质无关接口 RMII: Reduced MII 简化媒体独立接口 GMII: Gigabit Medium In ...

  10. JPA(Java Persistence API)Java持久化API-介绍

    JPA全称: Java Persistence API JPA的宗旨是为POJO提供持久化标准规范,能够脱离容器独立运行,很方便开发和测试.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关 ...