Python 中的语句和函数有什么区别?
What is the difference between a statement and a function in Python?
编辑:建议的重复项没有回答我的问题,因为我主要关心的是 Python 中的差异。建议的重复比这个问题要广泛得多。
最近开始学习Python。我目前正在阅读 "Learn Python the Hard Way"。我有一些临时编程经验,但这次我要回到起点,从头开始学习所有内容。
在书中,第一课之一涉及 print
,作者在 Python 2.7 中提供了各种使用说明,例如:
print "This is fun."
我发现自己在想,从编程的角度来看,print
在这里的技术名称是什么。 Some research found this, PEP-3105
在这种情况下使print
成为一个函数:
The print statement has long appeared on lists of dubious language
features that are to be removed in Python 3000, such as Guido's
"Python Regrets" presentation 1 . As such, the objective of this PEP
is not new, though it might become much disputed among Python
developers.
所以print
在Python2.7中是一个语句,在Python3中是一个函数。
但是我一直无法找到 statement
和 function
之间区别的直接定义。我发现 this 也是发明 Python、Guido van Rossum 的人,他在其中解释了为什么让 print 成为函数而不是语句会更好.
据我所读,函数似乎是一些采用参数和 returns 值的代码。但是 print
不是在 python 2.7 中这样做吗?它不是接受字符串并返回一个连接的字符串吗?
Python中的语句和函数有什么区别?
语句是一种语法结构。函数是一个对象。有创建函数的语句,例如 def
:
def Spam(): pass
因此,语句是向 Python 表明您希望它创建一个函数的方法之一。除此之外,他们之间真的没什么关系。
Python 中的语句是您编写的任何代码块。它更像是一个理论概念,而不是真实的事物。如果您在编写代码时使用正确的语法,您的语句将得到执行 ("evaluated")。如果您使用不正确的语法,您的代码将抛出错误。大多数人交替使用 "statement" 和 "expression"。
查看语句和函数之间区别的最简单方法可能是查看一些示例语句:
5 + 3 # This statement adds two numbers and returns the result
"hello " + "world" # This statement adds to strings and returns the result
my_var # This statement returns the value of a variable named my_var
first_name = "Kevin" # This statement assigns a value to a variable.
num_found += 1 # This statement increases the value of a variable called num_found
print("hello") # This is a statement that calls the print function
class User(BaseClass): # This statement begins a class definition
for player in players: # This statement begins a for-loop
def get_most_recent(language): # This statement begins a function definition
return total_count # This statement says that a function should return a value
import os # A statement that tells Python to look for and load a module named 'os'
# This statement calls a function but all arguments must also be valid expressions.
# In this case, one argument is a function that gets evaluated
mix_two_colors(get_my_favorite_color(), '#000000')
# The following statement spans multiple lines and creates a dictionary
my_profile = {
'username': 'coolguy123'
}
下面是一个无效语句的示例:
first+last = 'Billy Billson'
# Throws a Syntax error. Because the plus sign is not allowed to be part of a variable name.
在 Python 中,您倾向于将每条语句放在自己的行中,嵌套语句除外。但是在 C 和 Java 等其他编程语言中,只要用冒号 (;) 分隔,您可以在一行中放置任意多的语句。
在Python2和Python3中,都可以调用
print("this is a message")
它会将字符串打印到标准输出。这是因为它们都定义了一个名为 print 的函数,该函数接受一个字符串参数并将其打印出来。
Python2 还允许您在不调用函数的情况下声明打印到标准输出。该语句的语法是它以单词 print 开头,后面的内容就是打印的内容。在 Python3 这不再是一个有效的声明。
print "this is a message"
编辑:建议的重复项没有回答我的问题,因为我主要关心的是 Python 中的差异。建议的重复比这个问题要广泛得多。
最近开始学习Python。我目前正在阅读 "Learn Python the Hard Way"。我有一些临时编程经验,但这次我要回到起点,从头开始学习所有内容。
在书中,第一课之一涉及 print
,作者在 Python 2.7 中提供了各种使用说明,例如:
print "This is fun."
我发现自己在想,从编程的角度来看,print
在这里的技术名称是什么。 Some research found this, PEP-3105
在这种情况下使print
成为一个函数:
The print statement has long appeared on lists of dubious language features that are to be removed in Python 3000, such as Guido's "Python Regrets" presentation 1 . As such, the objective of this PEP is not new, though it might become much disputed among Python developers.
所以print
在Python2.7中是一个语句,在Python3中是一个函数。
但是我一直无法找到 statement
和 function
之间区别的直接定义。我发现 this 也是发明 Python、Guido van Rossum 的人,他在其中解释了为什么让 print 成为函数而不是语句会更好.
据我所读,函数似乎是一些采用参数和 returns 值的代码。但是 print
不是在 python 2.7 中这样做吗?它不是接受字符串并返回一个连接的字符串吗?
Python中的语句和函数有什么区别?
语句是一种语法结构。函数是一个对象。有创建函数的语句,例如 def
:
def Spam(): pass
因此,语句是向 Python 表明您希望它创建一个函数的方法之一。除此之外,他们之间真的没什么关系。
Python 中的语句是您编写的任何代码块。它更像是一个理论概念,而不是真实的事物。如果您在编写代码时使用正确的语法,您的语句将得到执行 ("evaluated")。如果您使用不正确的语法,您的代码将抛出错误。大多数人交替使用 "statement" 和 "expression"。
查看语句和函数之间区别的最简单方法可能是查看一些示例语句:
5 + 3 # This statement adds two numbers and returns the result
"hello " + "world" # This statement adds to strings and returns the result
my_var # This statement returns the value of a variable named my_var
first_name = "Kevin" # This statement assigns a value to a variable.
num_found += 1 # This statement increases the value of a variable called num_found
print("hello") # This is a statement that calls the print function
class User(BaseClass): # This statement begins a class definition
for player in players: # This statement begins a for-loop
def get_most_recent(language): # This statement begins a function definition
return total_count # This statement says that a function should return a value
import os # A statement that tells Python to look for and load a module named 'os'
# This statement calls a function but all arguments must also be valid expressions.
# In this case, one argument is a function that gets evaluated
mix_two_colors(get_my_favorite_color(), '#000000')
# The following statement spans multiple lines and creates a dictionary
my_profile = {
'username': 'coolguy123'
}
下面是一个无效语句的示例:
first+last = 'Billy Billson'
# Throws a Syntax error. Because the plus sign is not allowed to be part of a variable name.
在 Python 中,您倾向于将每条语句放在自己的行中,嵌套语句除外。但是在 C 和 Java 等其他编程语言中,只要用冒号 (;) 分隔,您可以在一行中放置任意多的语句。
在Python2和Python3中,都可以调用
print("this is a message")
它会将字符串打印到标准输出。这是因为它们都定义了一个名为 print 的函数,该函数接受一个字符串参数并将其打印出来。
Python2 还允许您在不调用函数的情况下声明打印到标准输出。该语句的语法是它以单词 print 开头,后面的内容就是打印的内容。在 Python3 这不再是一个有效的声明。
print "this is a message"