How do I define a function in Python?

There are two keyword to define a function in Python: "def" and "return". The structure is always the same: def function_name(input1, input2 = 2): [INDENT] "main body" return output Remember that in Python using the ":" at the end of the definition line and indenting the following lines is essential. Every output is placed after the keyword return. The inputs can be mandatory or optional. Every optional input need to be given a default value, like this: optional_input = Default_value. Finally, it is good practice to put all the mandatory inputs first, and then the optional ones.

SG
Answered by Stefania G. Python tutor

2420 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Create a python code to sum the number from 1 to 10.


Explain how you would write a python program that takes a rectangle and a point in a 2D space as command-line arguments and checks if they intersect.


With the help of Pandas and Numpy library create a DataFrame with the columns "Name", "Surname", "Age" and "Gender", create as many rows as you want. Print out the result.


How do For Loops work?


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

MyTutor is part of the IXL family of brands:

© 2026 by IXL Learning