FAQ: Introduction to Functions - Keyword Arguments

Hi all,
Somehow my code does not work, I tried to get it working like this:

Define create_spreadsheet():

def create_spreadsheet(title+row_count):
print("Creating a spreadsheet called "+title)

Call create_spreadsheet() below with the required arguments:


row_count = 1000

that`s my Code

and here is the error message that i´m recieving

File “script.py”, line 2
def create_spreadsheet(title+row_count):
SyntaxError: invalid syntax

i could click on “Solution” but the i would see all of the code in the exercise, wich contradicts the purpose of learning by doing.

I hope somebody can help me.
Thank`s in advance.

We cannot write operator expressions in the parameter. It should be a comma separated list. Any keyword arguments should appear last in the list.

def create_spreadsheet(title, row_count=1000):
1 Like

I don’t quite understand why do we need to have the string command for the row_count below?

def create_spreadsheet(title, row_count = 1000):
print(“Creating a spreadsheet called " + title + " with " + str(row_count) + " rows”)

Because row_count is an integer and we cannot concatenate an integer (or a float) to a string. Using the str() constructor casts the number to a string type so it can be concatenated.

Is there a difference between using str() and passing it to print() as multiple arguments?

def create_spreadsheet(title, row_count=1000):
print(“Creating a spreadsheet called " + title + " with " + str(row_count) + " rows”)

def create_spreadsheet(title, row_count=1000):
print("Creating a spreadsheet called “+title+” with “, row_count, " rows”)

Is there a practical difference between the two?

Well, they both print what you want, but I think that keeping in mind that you are either printing strings (first example) or printing a tuple (second example) helps focus on what you are trying to do.

There is a host of string formatting machinery available to you if you’re interested in nice-looking output. On the other hand, printing tuples consisting of objects separated by commas is quick and easy, especially for developing and debugging.

Hi Mtf,
First timer here. I don’t know why I couldn’t seem to post here - still trying to get used to the forum :slight_smile:
Just a simple question, so it says ‘Once you give an argument a default value (making it a keyword argument), no arguments that follow can be used positionally.’ Could I interpret it as keyword/default value argument is not positioned in the same sequence as the print statements? To be more specific, as grocery_store comes ahead of special_item in print statements, then we have to use special_item before grocery_store in the parenthesis after def greet_customer that’s what the web says as well.
However, in the practice, it seems that row_count is defaulted/keyworded and comes after title , which is the same sequence as what it is in the print statement. Does it not seem to be to the contrary?

Would be good to know why.

The order in which we print items is arbitrary (as in doesn’t matter). However, the order in which we position the arguments in a function call definitely matters.

The statement in your post describes where we can position default parameters in the function’s parameter list. They cannot be followed by any other positional arguments.

 func(arg1, arg2)        # a call with two positional arguments

 def func(a, b, c=0):    # a signature line with a third optional parameter

The default parameters must always be last.

Thanks so much for your prompt response!

I see. Just to confirm, as the order of how we input the parameters in the print statements doesn’t matter, I assume the order in the function parenthesis has nothing to do with that and the no.1 rule is that default must be left as the last, not followed by any positional argument.

1 Like

The order of the arguments is important so that we are passing the values to the correct parameter.

a => receives arg1
b => receives arg2

Those two arguments must be present in the function call or an exception will be raised. We do not need to third argument since it has a default value already set. Should we include a third argument, say, arg3, then

c => arg3

Now if we wish to print these values in the function, we can choose any order…

print (c, b, a)

would be just fine. The print function doesn’t have any positional arguments, and can accept any number of comma separated expressions, or any manner of formatting. That function does nothing but convey the expressions to the standard output (usually the terminal).