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.

Answered by Stefania G. Python tutor

1925 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

How could I write a program to convert Km/h to m/s in python


Demonstrate a recursive solution to calculate the factorial of a number


Create a program that generates prime numbers between two integer boundareis


Firstly, my question is not Python related, but maths A-level related. I don't know how to change that in my profile but I would like to only tutor maths GCSE and potentially A-level. My question: Integrate sin(x)^3 over x=0 and x=pi/2.


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo
Cookie Preferences