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

Related Python Mentoring answers

All answers ▸

Explain the difference between local and global variables


Ask the user for a number and output the smallest divisor, bigger than one, for the inputted number. Output "Prime" if the number is a prime number.


Give an example of a FOR loop


Write a python function that takes a string as parameter and returns the character in the string with the most occurrences, along with the number of times this character occurs