`
jimichan
  • 浏览: 278071 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

python 中的print 是语句还是函数

阅读更多

在python 2.5 系列中,最简单的print语句,总是让初学者疑惑?到底是函数还是一个语句(语句的意思是和 if for等是一个级别的)。

 

看起来 print "hellow " 和 print("hellow") 都是正确的。

但是执行 type(print) 时确得到了一个异常,说明print 是语句而不是函数。

 

接下来让我们看看在 2.6版本里面。

同样得到 和 2.5版本一样的结果。但是从2.6里面 增加了一个新方法。

执行 

 

from
 __future__
 import
 print_function


 

那么此时你执行 type(print) 命令,就会发现 print是个函数了。原来的print "hellow"就不能使用了。

 

2.6的这个做法大概是为了3000吧。到了3000里面 print 就只能是函数了。避免歧义。

 

 

分享到:
评论
1 楼 supermarshal 2009-03-19  
文档里有
6.6 The print statement

print_stmt ::= "print" ([expression ("," expression)* [","]
  | ">>" expression [("," expression)+ [","])

Download entire grammar as text.
print evaluates each expression in turn and writes the resulting object to standard output (see below). If an object is not a string, it is first converted to a string using the rules for string conversions. The (resulting or original) string is then written. A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line. This is the case (1) when no characters have yet been written to standard output, (2) when the last character written to standard output is "\n", or (3) when the last write operation on standard output was not a print statement. (In some cases it may be functional to write an empty string to standard output for this reason.) Note: Objects which act like file objects but which are not the built-in file objects often do not properly emulate this aspect of the file object's behavior, so it is best not to rely on this.

A "\n" character is written at the end, unless the print statement ends with a comma. This is the only action if the statement contains just the keyword print.

Standard output is defined as the file object named stdout in the built-in module sys. If no such object exists, or if it does not have a write() method, a RuntimeError exception is raised.

print also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as ``print chevron.'' In this form, the first expression after the >> must evaluate to a ``file-like'' object, specifically an object that has a write() method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to None, then sys.stdout is used as the file for output.




--------------------------------------------------------------------------------

相关推荐

    python的for循环语句range-Python基础教程:for循环语句与range()函数.pdf

    python的for循环语句range_Python基础教程:for循环语句与 range()函数 for 循环 For … in 语句是另⼀种循环语句,其特点是会在⼀系列对象上进⾏迭代(Iterates),即它会遍历序列中的每⼀个项⽬ 注意: 1、else ...

    Python中return语句用法实例分析

    本文实例讲述了Python中return语句用法。分享给大家供大家参考。具体如下: return语句: return语句用来从一个函数 返回 即跳出函数。我们也可选从函数 返回一个值 。 使用字面意义上的语句 #!/usr/bin/python # ...

    python输入输出语句例子.docx

    在Python中,输入输出语句是非常重要的基础语句,本文将介绍Python中常用的输入输出语句,并给出相应的例子。 一、输入语句 1. input()函数 input()函数是Python中最常用的输入函数,它可以从控制台获取用户输入的...

    Python的print用法示例

    Python 2.6中print不是函数,而是一个关键字,使用方式如下:复制代码 代码如下:print 1, 2 print ‘a’, ‘b’ 显示结果如下,用逗号分隔的各项之间会打印出一个空格,默认以换行结束: 复制代码 代码如下:1 2 a...

    Python2和Python3中print的用法示例总结

    在python2中print是一种输出语句,和if语句,while语句一样的东西,在python3中为了填补python2的各种坑,将print变为函数,因此导致python3中print的一些使用和python2很不一样。下面就来给大家详细的总结了关于...

    python中常用的输入输出语句.docx

    Python中常用的输入输出语句是input()和print(),分别用于接收用户输入和输出程序结果。 1. input()函数可以接收用户输入的数据,并将其存储在一个变量中,如:name = input("请输入您的姓名:") 2. 如果需要输入多...

    python判断题题库-《Python程序设计》判断题1-240题.pdf

    (错) 3、Python 3.x和Python 2.x唯⼀的区别就是:print在Python 2.x中是输出语句,⽽在Python 3.x中是输出函数。(错) 4、在Windows平台上编写的Python程序⽆法在Unix平台运⾏。(错) 5、不可以在同⼀台计算机上...

    Python 3中print函数的使用方法总结

    前言 Python 思想:“一切都是对象!”,最近发现python3和python2中...所以就想着给大家总结一下Python3中print函数用法的相关内容,话不多少了,来一起看看详细的介绍: 1. 输出字符串和数字 >>> print(runoob

    python中return的理解.docx

    下面是一个简单的例子,演示了如何使用return语句从函数中返回一个值: ``` def add_numbers(x, y): result = x + y return result sum = add_numbers(5, 10) print(sum) # 输出15 ``` python中return的理解全文共3...

    python2与python3的print及字符串格式化小结

    在python2中print是一种输出语句,和if语句,while语句一样的东西,在python3中为了填补python2的各种坑,将print变为函数,因此导致python3中print的一些使用和python2很不一样。同时,python3大改python2中的字符...

    python基础试题(含答案)图文复习知识点试卷试题.doc

    在Python中,"print(100-33*5%3)"语句输出的是( ) A.34 B.67 C.100 D.1 5.下列属于计算机编程语言的有( ) Python Visual Basic Java C++ CPU A. B. C. D. 6.下列不是 Python 程序基本结构的是()。...

    python的语句.docx

    例如,我们可以使用re.search()函数来查找一个字符串中是否包含某个模式: import re python的语句全文共7页,当前为第1页。 python的语句全文共7页,当前为第1页。 text = "The quick brown fox jumps over

    python中的函数用法入门教程

    本文较为详细的讲述了Python程序设计中函数的用法,对于Python程序设计的学习有不错的借鉴价值。具体分析如下: 一、函数的定义: Python中使用def关键字定义函数,函数包括函数名称和参数,不需要定义返回类型,...

    python高中基础代码.docx

    输出语句 Python中的输出语句使用print函数实现,例如: print("Hello, World!") 这行代码将在屏幕上输出"Hello, World!"。在Python中,字符串需要用双引号或单引号括起来。 2. 变量 在Python中,变量可以用来存储...

    python中return用法.docx

    Python中的return语句用于从函数中返回一个值,并终止函数的执行。下面是return语句的基本用法: ```python def add_numbers(x, y): sum = x + y return sum result = add_numbers(3, 5) print(result) # 输出8 ```...

    python中的ch表示什么-Python基础教程Ch5-条件、循环和其他语句.pdf

    python中的ch表⽰什么_Python基础教程Ch5-条件、循环和其 他语句 第5章 条件、循环和其他语句 第5章 条件、循环和其他语句 5.1print和import的更多信息 print语句可以同时打印多个表达式,只需要⽤逗号分隔即可,...

    第五章-python中的函数与模块-青少年编程练习测试题及答案解析.docx

    a+b 5 报错 答案解析:C,自定义函数,fun( )调用函数,执行print()语句,输出a+b的值为5 执行下列代码: 第五章-python中的函数与模块-青少年编程练习测试题及答案解析全文共5页,当前为第2页。第五章-python中的...

    详解Python中的各种函数的使用

    函数是有组织的,可重复使用的代码,用于执行一个单一的,相关的动作的块。函数为应用程序和代码重用的高度提供了更好的模块。 正如我们知道的,Python的print()等许多内置函数,但也... 每个函数中的代码块以冒号(:)

    python的for函数.docx

    python的for函数 Python的for函数是一种非常常用的循环语句,它可以让我们在程序中重复执行某些操作,从而实现一些复杂的功能。在本文中,我们将详细介绍Python的for函数的用法和特点。 我们来看一下Python的for函数...

    Python语言基础:输入和输出.pptx

    在Python开发中,经常会遇到需要控制台输入的情况,在Python3中,提供了input()函数来从控制台接收一个标准输入数据。 语法格式:input([p]) 说明:p是进行输入之前的提示信息。 input()函数可以接收任意输入,将...

Global site tag (gtag.js) - Google Analytics