FAQ: Introduction to Functions - Keyword Arguments

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!

1 Like

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

13 posts were merged into an existing topic: Do all keyword arguments have to written after all the positional arguments?

2 posts were split to a new topic: Can keyword arguments be used positionally?

2 posts were split to a new topic: What is the point of using a keyword?

5 posts were split to a new topic: Why doesn’t my code work?

2 posts were split to a new topic: What is wrong with my code?

4 posts were split to a new topic: Do we need to use str() on row_count?

2 posts were split to a new topic: What is concatenation?

Why can’t I print integers? Why must I cast them as strings before printing them?

Integers can be printed without any problem. If we wish to interpolate numeric variables in a string, then we will need to cast them to str…

mol = 42

print (mol)    #  42

print ("The meaning of life is, " + str(mol) + ".")

#  The meaning of life is, 42.
1 Like

That’s twice today you’ve helped mtf, thanks again. :slight_smile: I think that I’m used to mixing numerics, in the same statement, with strings like you can (I believe) in JavaScript.

RIP Adams.

1 Like
  1. Change the print statement in the function to print “Creating a spreadsheet called title with row_count rows”, where title and row_count are replaced with their respective values.

Remember, to concatenate a number to a string object, you’ll first have to cast row_count to a string using str() . Otherwise, you’ll get a TypeError .

There’s a big problem with that, it tells me “Remember” but it was never brought upon any of the previous topics on how to interpolate ints in a string…

1 Like

Where did all of this come from? Did I miss something? All the sudden there’s a bunch of +'s, and a “str” I’ve never seen before.

This part messed me up
("creating a spreadsheet called “+title+” with "str(row_count)+“rows.”)

Where did that come from??? I had to click view solution cause I just couldn’t get it and then it hits me with this stuff I’ve never seen and could never have guessed.

I am so lost…

1 Like

It would have come up in the unit on strings and string formatting, and str() may be introduced in the built-in functions unit. Since concatenation is a rather simple concept there is probably not much time spent on the narrative and not many examples.

The thing to keep in mind is we are building a string from mixed data. All that data needs to be cast to the str type in order for the concatenation to be successful (with raising an exception). That is why we recast numbers (int, float) to string objects in this process. The object itself is not altered, only its representation within the string output.

You may feel blindsided at times in these courses but the onus is on us to keep a cool head, go back through the lessons (and our notes), do a quick search of the documentation to look up functions that are new and unfamiliar to us so we learn what they offer.

Is it possible to have more than one default argument in the function definition?

Yes, so long as we don’t go overboard. The simpler the function and the fewer the parameters the easier to debug and maintain. Better to have three simple functions than one huge complicated one. Defaults should be able to apply in all situations where positional arguments are not present.

Thanks! But why not have default arguments for all the parameters? It seems like a good safeguard, especially considering missing parameters cause so much problems in languages like C.

That is where splat, the spread operator comes in…

>>> def foo(*args):
	if len(args) < 1: raise ValueError
	for i in args:
		print (i)

		
>>> foo(1,2,3,4,5,6,7,8,9)
1
2
3
4
5
6
7
8
9
>>> 

So raising this error and having default arguments just depends on what you want to accomplish?