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

Question:

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?

Asked By: Gary

||

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.

A statement in Python is any chunk of code you’ve written. It’s more a theoretical concept than a real thing. If you use the correct syntax when writing your code, your statements will get executed (“evaluated”). If you use the incorrect syntax, your code will throw an error. Most people use “statement” and “expression” interchangeably.

Probably the easiest way to see the difference between a statement and a function is to see some example statements:

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' 
}

Here is an example of a statement that is invalid:

first+last = 'Billy Billson'
# Throws a Syntax error. Because the plus sign is not allowed to be part of a variable name.

In Python, you tend to put each statement on their own line, except in the case of nested statements. But in other programming languages like C and Java, you could put as many statements on a single line as you wanted as long as they are separated by a colon (;).

In both Python2 and Python3, you can call

print("this is a message") 

and it will print the string to standard out. This is because they both have a function defined called print that takes in a string argument and prints it.

Python2 also allowed you to make a statement to print to standard out without calling a function. The syntax of this statement was that it started with the word print and whatever came after was what got printed. In Python3 this is no longer a valid statement.

print "this is a message"
Answered By: Kevin

Both function and statement are words that Python understands.

Function needs parenthesis to act on anything (including nothing).

Statement does not.

Hence in Python 3 print is function not statement.

Let us take a funny case. not True and not(True) both work. But type(not) is not function hence not is statement. not(True) works only because Python takes parenthesis also for grouping. Bad design, indeed.

Another difference: (not) fails, (print) does not fail, because a statement has no value while a function has one (for the interpreter, not in the mathematical sense of the image of some antecedent).

Answered By: Pierre ALBARÈDE