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 broade
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 is everything that python can understand. A function is one of those things and he have different capabilities than other statements. A function is a statement but there are many statements that aren't functions.
The if statement is one of those that isn't a function. the for and while are also great examples. All of them are part of the Compound Statements group.
In Python, functions have to return a value. Even if you do not use a return statement the function will return None. there are some statements that do not return any value like if, for, while, etc. how can you check if a statement is a function or not? you can use the type function which will return class 'function' for functions. You can try to ask for the type of if but you will get a SyntaxError.
In python 2 the print statement was part of the Simple statements like import, global, break, and others. But after PEP 3105 a new print function replaced the print Simple Statement. In PEP 3105 you can read why the community decided to change the python statement, which may make it clearer why some of the statements aren't functions.
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"