Can all the parameters in a function be keyword arguments?
>>> def second_degree_equation(a=1, b=1, c=1): return lambda x: a * x ** 2 + b * x + c >>> f = second_degree_equation() >>> f(2) 7 >>>
The function above returns a function with a, b and c as constants. In Python a
lambda is an anonymous function. We could have written it like so,
>>> def second_degree_equation(a=1, b=1, c=1): def func(x): return a * x ** 2 + b * x + c return func >>> f = second_degree_equation() >>> f(3) 13 >>>
We learn in around G. 9 or G. 10 about quadratic equations. They can take various forms, such as one, two, or more terms with degree 2. The one above is the general form of an equation with one second degree term.
y = Ax^2 + Bx + C
where A, B and C are constants, and
x is the variable.
Consider Pythagorean Triangle…
a^2 + b^2 = c^2
This gives us the relation of the base and side of a right triangle to its hypotenuse.
A very similar equation describes the relation of the x and y axes to the radius of a circle.
x^2 + y^2 = r^2
The concept of a function returning a function will be coming up in due course so don’t stress over it right now if you don’t quite understand. The key here is that we have shown that we can use all keyword arguments.
im a little bit confused as to why the title in this code has the + marks beside it within quotation marks.
maybe i need to go back and review some of this stuff
print(“Creating a spreadsheet called “+title+” with “+str(row_count)+” rows.”)
Try breaking that statement down into its indiivudal parts to see what goes where so you can work out what is being done and have a little look back at string concatenation.
ill do that now, thanks
I am having a hard time with this lesson on Keyword Arguments. With the last question:
title set to
row_count set to
I’m not sure what I’m doing wrong.
Looks like the defaults are possibly something else? From the wording it looks like we are expected to supply the arguments in the call…
Is the row_count default supposed to be set to 1000?
It says 10 in the instructions, I’m not sure where 1000 came from
As the default.
def create_spreadsheet(title, row_count=1000):
Ohhh, earlier in the assignment I did set it to 1000. I must have deleted it when I changed it to 10. I see. Just wondering why I would want to change it to 10 when I call it rather than just change the default? is it for convenience?
Thank you for your help
Yes. If we want the default size we only need to supply the title for the new worksheet.
How come these lines of code in keyword arguments, sorry had to copy and paste:
def create_spreadsheet(title, row_count = 1000):
print("Creating a spreadsheet called “+title+” with " + str(row_count) + " rows ")
Call create_spreadsheet() below with the required arguments:
Running it gives me this, and spits it out twice instead of once:
Creating a spreadsheet called Downloads with 1000 rows
Creating a spreadsheet called Applications with 10 rows
Question is why does it run it twice when from my understanding of the code it really should just do it once like:
Creating a spreadsheet called Downloads with 10 rows
Am I wrong on assuming that or is it somewhere in the coding calling on it twice
me again is (“downloads”) calling on the first line
and (“Applications”, 10) calling the second line resulting in both lines printed out,
Please check the following FAQ which covers amongst other things how to format code on the forums (Python without indentation is hard to understand so please use formatted code in the future).
Your function is run every time you call it
function(arguments). So your assumption about using it on both lines is correct, for this function you would receive output every time you called it.
that’s code academy code, I copy and pasted from their own view solution, but thanks for the FAQ
(Newbie here) In previous exercises, we used spaces in-between operants, like:
kind_of_food = "cakes" print("My favourite kind of food is " + cakes)
whereas now, in the exercises, it turned to (spaces between the string, + and cakes):
kind_of_food = "cakes" print("My favourite kind of food is "+cakes)
Does it make any difference? Is one used more often than the other? Is the reason readability or memory storage, or maybe something completely different?
It’s a suggestion rather than a requirement but spaces around operators is the norm, it doesn’t affect the code execution so it’s purely for readability. In your first example I can clearly separate the string, the operator and the identifier whereas the second one takes me a moment longer to work out what’s going on (maybe that’s just me but spaces are suggested).
Occasionally you might find things easier without spaces; as an example pep-8 notes the option to group certain mathematical operators by priority- pep8- Other Recommendations.
Since we’re discussing keyword arguments here the suggestion is not to use spaces for them-
Example from https://www.python.org/dev/peps/pep-0008
def complex(real, imag=0.0): # not for default arguments return magic(r=real, i=imag) # or keyword arguments
It is now clear, thank you