Python Tutorial
About Lesson

in this Python Functions lesson we want to learn about Create Function Example in Python, also for python function we can tell that it is a block of codes. when you want to do a particular task that you’ve defined in a function, you call the function responsible for it. If you need to perform that task multiple times throughout your program, you don’t need to type all the code for the same task again and again, you can just call the function dedicated to handling that task, and the call tells Python to run the code inside the function. You’ll find that using functions makes your programs easier to write and read.

 

Now let’s create a simple python function example, you can use def keyword for creating functions in python.

 

In the above code we have created a simple function or method in python, after creating of the python function, if you want to use from this function you need to call that. 

 

For example you are in a situation that you need this code again now you don’t need to write this code again and you don’t need define this function again, you can just call this function as much as you want.

 

 

 

Passing Information to Function

Sometimes you need to pass some information in the function, now for that we can call function parameters. so we can say that a piece of information that the function needs to do its job is called function parameters. in this example we have just created a simple function and we have passed username as parameter in the function, now when you call your function you need to give a username as parameter, if you don’t do this you will receive error.

 

 

 

Run the code and this is the result.

Python Functions - Create Function Example
Python Functions – Create Function Example

 

 

 

Also you can pass multiple parameters to a function, in this example we are going to pass two parameters in the function, now when you call your function you need to pass two parameters.

 

 

 

Default Parameters.

Some times you need to add default parameters in Python functions, in this example we want to add two parameters in the function, and the second parameter has a default value, now when you call your function you can just pass one value to the function, and it will take the second parameter as default parameter. 

 

 

 

Returning a Value 

A function doesn’t always have to display its output directly. Instead, it can process some data and then return a value or set of values. The value the function returns is called a return value. The return statement takes a value from inside a function and sends it back to the line that is called by the function. in this example we want to create a function that takes two parameters, after that we want to add these two parameters together and return that.

 

 

 

Run the code and this is the result.

Python Functions - Create Function Example
Python Functions – Create Function Example

 

 

 

Returning a Dictionary in Function 

A function can return any kind of value, we have learned that how you can return a simple string, but some times you need to return a dictionary, now let’s create the example.

 

 

 

Passing a List to Python Function

Sometimes you need to pass a list to the function, so in this example we are passing a list in to the python function.

 

 

 

Run the code and this is the result.

Python Function Example
Python Function Example

 

 

 

 

Passing Arbitrary Numbers of Arguments in Python Functions

Sometimes you won’t know ahead of time how many arguments a function needs to accept. fortunately, Python allows a function to collect an arbitrary number of arguments from the calling statement. For example, consider a function that print numbers. It needs to accept number , but you can’t know ahead of time how many numbers will be added for printing. the function in the following example has one parameter, *numbers, but this parameter collects as many arguments as the calling  line provides. you can use asterisks for doing this.

 

The asterisk in the parameter name *numbers tells Python to make an empty tuple called numbers and pack whatever values it receives into this tuple. The print() call in the function body produces output showing that Python can handle a function call with one value and a call with different values. It treats the different calls similarly. Note that Python packs the arguments into a tuple, even if the function receives only one value.

 

 

 

Now you can use for loop and iterate on the tuple.

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found