What is the best way to master "writing a function" in python?
Hello Tutor,
What is the best way to master "writing a function" in python?
Please also provide any example or any helpful url links.
Thanks
I assume you know what are python functions. if yes please proceed to step 2.
If not, let me give you a brief introduction to python functions.
Function: Function is a block of reusable code, used to perform repetitive tasks.
python functions are three types:
- Built-in Functions
-
User-defined Functions
-
Anonymous Functions
Built-in Functions: In python, many ready-to-use functions are available which are called built-in functions.
Example: print(), range() etc.,
User-defined Functions: Functions defined by the user to accomplish a certain task are called user-defined functions. It can have parameters and return values based on the requirement.
Syntax:
def function_name():
/*----code goes here ---*/
Example:
def add(): output: 5
a=2
b=3
print(a+b)
Anonymous Functions: Anonymous functions are also known as lambda functions. These functions are defined without a name and unlike using def we use lambda to define these functions. It can have any number of arguments but can have only one expression.
Example:
cube = lambda x: x*x*x output: 27
print(cube(3))
Step by step
Solved in 2 steps