在本节中,我们将检查在两个不同的python版本中打印单变量和多变量输出。
>>> #Python 2.7
>>> #Print single variable
>>> print 27
27
>>> print "Rahul"
Rahul
>>> #Print single variable, single brackets
>>> print(27)
27
>>> print("Rahul")
Rahul>>> #Python 3.6 >>> #Print single variable without brackets >>> print 27 SyntaxError: Missing parentheses in call to 'print' >>> print "Rahul" SyntaxError: Missing parentheses in call to 'print'
上面3.6中的语法,是由于:在python 3.x中,print不是语句,而是函数(print())。因此,将print更改为print()。
>>> print (27)
27
>>> print("Rahul")
RahulPython 2.x(例如:python 2.7)
>>> #Python 2.7
>>> #Print multiple variables
>>> print 27, 54, 81
27 54 81
>>> #Print multiple variables inside brackets
>>> print (27, 54, 81)
(27, 54, 81)
>>> #With () brackets, above is treating it as a tuple, and hence generating the
>>> #tuple of 3 variables
>>> print ("Rahul", "Raj", "Rajesh")
('Rahul', 'Raj', 'Rajesh')
>>>因此,从上面的输出中,我们可以在python 2.x中看到,在方括号()中传递多个变量,会将其视为多个项目的元组
#Python 3.6
#Print multiple variables
>>> print(27, 54, 81)
27 54 81
>>> print ("Rahul", "Raj", "Rajesh")
Rahul Raj Rajesh让我们再来看一个python 2.x和python 3.x中多个语句的示例