Skip to content Skip to sidebar Skip to footer

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

Solution 1:

A statement is a syntax construct. A function is an object. There's statements to create functions, like def:

defSpam(): 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.

Solution 2:

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_foundprint("hello") # This is a statement that calls the print functionclassUser(BaseClass): # This statement begins a class definitionfor player in players: # This statement begins a for-loopdefget_most_recent(language): # This statement begins a function definitionreturn total_count # This statement says that a function should return a valueimport 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"

Post a Comment for "What Is The Difference Between A Statement And A Function In Python?"