Edit: The suggested duplicate, does not answer my question, as I am primarily concerned with the difference in Python specifically. The suggested duplicate is far broader than this question.
I have recently started to learn Python. I'm currently reading "Learn Python the Hard Way". I have some ad-hoc programming experience, but am going back to the beginning to learn everything from the ground up this time.
In the book, one of the first lessons concerns print
and the author provides various instructions of its use in Python 2.7, e.g.:
print "This is fun."
I found myself wondering what print
is technically called here from the programming perspective. Some research found this, PEP-3105
In which case is made to make print
a function:
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.
So print
is a statement in Python 2.7, and a function in Python 3.
But I have been unable to find a straight-forward definition for the difference between a statement
and a function
. I found this also by the person who invented Python, Guido van Rossum in which he explains why it would be good to make print a function instead of a statement.
From what I have read it appears that a function is some code that takes parameters and returns a value. But isn't print
doing this in python 2.7? Isn't it taking in strings and returning a concatenated string?
What is the difference between a statement and a function in Python?
A statement is a syntax construct. A function is an object. There's statements to create functions, like def
:
def Spam(): pass
So statements are one of the ways to indicate to Python that you want it to create a function. Other than that, there's really not much relation between them.