Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a statement and a function in Python?

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?

like image 816
Gary Avatar asked Apr 16 '17 09:04

Gary


People also ask

What is a statement in Python?

A statement is an instruction that a Python interpreter can execute. So, in simple words, we can say anything written in Python is a statement. Python statement ends with the token NEWLINE character. It means each line in a Python script is a statement. For example, a = 10 is an assignment statement.

Is statement a function?

A statement function statement is a function-like declaration, made in a single statement. Expression. e can be any of the types arithmetic, logical, or character.

What is the difference between statement and expression?

In programming language terminology, an “expression” is a combination of values and functions that are combined and interpreted by the compiler to create a new value, as opposed to a “statement” which is just a standalone unit of execution and doesn't return anything.

Is if a statement or a function?

The IF function is one of the most popular functions in Excel, and it allows you to make logical comparisons between a value and what you expect. So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.


1 Answers

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.

like image 87
Dimitris Fasarakis Hilliard Avatar answered Oct 19 '22 08:10

Dimitris Fasarakis Hilliard