Python raw_input 函数 - Python零基础入门教程

目录

基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门

一.前言

Python 2.x 版本中才有内置函数 raw_inputinput 两个函数;

Python 3.x 版本中并没有内置函数 raw_input,如果在 Python 3.x 版本中使用内置函数 raw_input,会提示:NameError: name ‘raw_input’ is not defined

二.Python input 和 raw_input 区别

1.当输入为纯数字时

2.当输入字符串为表达式

Python input 会计算在字符串中的数字表达式,而 raw_input 不会。

如输入 “57 + 3” : input 会得到整数 60 ;raw_input 会得到字符串 ”57 + 3”;

三.Python input 函数实现原理

看 Python input 文档,可以看到 input 其实是通过 raw_input 来实现的,原理很简单,就下面一行代码:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python raw_input 函数.py
@Time:2021/05/04 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

"""

def input(prompt):

    return (eval(raw_input(prompt)))

四.猜你喜欢

  1. Python for 循环
  2. Python 字符串
  3. Python 列表 list
  4. Python 元组 tuple
  5. Python 字典 dict
  6. Python 条件推导式
  7. Python 列表推导式
  8. Python 字典推导式
  9. Python 函数声明和调用
  10. Python 不定长参数 *argc/**kargcs
  11. Python 匿名函数 lambda
  12. Python return 逻辑判断表达式
  13. Python 字符串/列表/元组/字典之间的相互转换
  14. Python 局部变量和全局变量
  15. Python type 函数和 isinstance 函数区别
  16. Python is 和 == 区别
  17. Python 可变数据类型和不可变数据类型
  18. Python 浅拷贝和深拷贝

未经允许不得转载:猿说编程 » Python raw_input 函数

本文由博客 - 猿说编程 猿说编程 发布!

posted @ 2021-07-21 16:24  猿说编程  阅读(747)  评论(0编辑  收藏  举报