Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Functions and Looping Create a Function

how do I create a square function?

how do I create a square function?

squaring.py

1 Answer

Steven Parker
Steven Parker
231,248 Points

It's not clear if your question is about creating a function or calculating a square. I'll try describing each.

To define a function in python, use the "def" keyword, the function name, parentheses with any parameter name(s) inside them, and a colon. On the following lines you put the code for the function to perform, indented. If the function is intended to return a value, a "return" statement should be included on the last line. You may want to take another look at the examples shown in the video.

To create a square you can either multiply a number by itself, or use the exponent operation (with the symbol "**") with a second operand of 2.