FAQ: Introduction to Functions - Keyword Arguments


#1

This community-built FAQ covers the “Keyword Arguments” exercise from the lesson “Introduction to Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Keyword Arguments

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


split this topic #2

A post was split to a new topic: Why doesn’t the following code work;?


#3

In section 7 the following statement appears “When a function has a default value for one of its parameters, we can no longer call it positionally, and only by keyword.”

I can’t replicate the above statement. If a function has a default value and I call it using a positional argument it works fine. Am I misunderstanding what the above quote is trying to explain?


#4

It could be that if you define a specific phrase, in this case the function was defined as saying "Engrossing Grocer’s " it will automatically call that phrase no matter what, so you don’t need to use a positional argument for it. Whereas, with the special item it didn’t have a specific phrase, so it would need to be called and a phrase or keyword would need to be introduced. I hope this helps.


#5

I have a similar question. In the quiz, a function is defined as follows:
def update(new_value = 10):
old_value = new_value

It asks how to call the function with new_value of 20.

The correct answer is
update(20)

So does this work because there is only one parameter for the function, so that the keyword new_value doesn’t need to be explicitly spelled out when calling the function? If there were more parameters, each with a default value, or some has default values and others don’t, how would it work? Would the keyword arguments need to be spelled out in those cases?

thanks


#6

new_value is only a local variable, not accessible outside of the function. It is preset to 10, but takes on any value we pass in. If no value is passed in, then new_value becomes 10.

Multiple keywords are possible, but the position is critical.

>>> def foo(a = 1, b = 2, c = 3):
    return a * 4, b * 3, c * 2

>>> foo()
(4, 6, 6)
>>> foo(4)
(16, 6, 6)
>>> foo(4, 5)
(16, 15, 6)
>>> foo(4, 5, 6)
(16, 15, 12)
>>> 

#7

Yea I’m still stuck on that statement in part 7 “When a function has a default value for one of its parameters, we can no longer call it positionally, and only by keyword.”

I intended to break it to check this, and I’m able to pass arguments positionally that have default values and receive the correct output

Example from the lesson:

Define create_spreadsheet():

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

Call create_spreadsheet() below with the required arguments:

create_spreadsheet(“Downloads”)
create_spreadsheet(“Applications”, 10) #<----- notice here that I did not explicitly call row_count.


#8

Hey ! Is it possible to put a default argument before a non default argument when defining a function. If yes how can we call it ?


#9

Consider,

>>> def remove_x(word, x='i'):
	return ''.join(word.split(x))

>>> remove_x('mississippi')
'msssspp'
>>> def remove_x(x='i', word):
	return ''.join(word.split(x))
SyntaxError: non-default argument follows default argument
>>> 

#10

Hello all, first timer here.

I was wondering what the point of keywording something in the parameter is ? In the example of the exercise that have been mentioned in this thread:

def greet_customer(special_item, grocery_store=“Engrossing Grocer’s”):
print("Welcome to "+ grocery_store + “.”)
print("Our special is " + special_item + “.”)
print(“Have fun shopping!”)

Engrossing Grocer’s has now been keyworded into the parameter’s positional arguements.

It seems to me that this is extra work and redundant coding, since keywording something to a parameter is pretty much the same as me doing this :

def greet_customer(special_item):
print(“Welcome to Engrossing Grocer’s.”)
print("Our special is " + special_item + “.”)
print(“Have fun shopping!”)

now isn’t that pretty much the same as me keywording it into the parameter ? but with less code ?
or does keywording something as in the example have broader uses ?

also, on a side note. How do I store a functions output as a variable ?

ex:

def somernumbers(number, x, y)
print(number + x*y)

somenumbers(5, 3, 2)

now that prints the number to the console, but
how do I go about storing that number/function as a variable ? I’ve tried all sorts of things, but with no luck.

Thank you all so much in advance.


#11

It is not the same. That example has hard coded a literal into the function body, and does not allow for a second positional argument. By keywording a default value, we may still replace that with a literal argument in subsequent calls.

>>> def add_to_list(temp_list=[], item='temp_item'):
    temp_list.append(item)
    return temp_list

>>> my_list = add_to_list()
>>> my_list
['temp_item']
>>> my_list = add_to_list(['existing item'])
>>> my_list
['existing item', 'temp_item']
>>> my_list = add_to_list(['existing item'], 'new item')
>>> my_list
['existing item', 'new item']
>>> 

#12

What do you mean by a “special item” ? The sentence which emommsen was referring to, still seems to be wrong. Calling a function positionally works absolutely fine, even though it has a default parameter (which accepts any other argument anyway).


#13

I feel like I am at my wits end on the section 7, when it asks me to call create_spreadsheet with a title “downloads” it doesn’t accept my answer, but when I check the solution the answer is exactly what I put in! Please see below:

https://www.codecademy.com/paths/computer-science/tracks/cspath-intro/modules/cspath-python-functions/lessons/intro-to-functions/exercises/keyword-arguments


#14

Did you write,

create_spreadsheet("downloads")

or,

create_spreadsheet("Downloads")

?


#15

I used upper case (“Downloads”) and it hasn’t accepted it


#16

MIght be the lesson timed out. Tried a refresh and Save again.


#17

Thank you sorted now


#18

The syntax error code helped me to understand what they were trying to say in the lesson. Thank you!


#19

So does this imply that default values must always be at the end of a list of arguments? We can’t have it before any non-defaults?


#20

More than imply; it’s the rule.