Hello, and welcome to the last part of the series “Introduction to Python Programming.” If you have gone through the series chronologically, you are now on your way to becoming a Python Ninja.
If you have not gone through the previous episode, kindly find the links below.
Introduction to Python Programming - part one
Introduction to Python programming - part two
introduction to Python programming - part three
introduction to Python programming - part four
A function only executes when called. You can supply parameters or data, to a function. As a result, a function may return data.
The Python def keyword is used to define functions:
def my_function(): print("Hello World")
def my_function(): print("Hello World") my_function()
Information can be passed into functions as arguments. Add as many arguments as you like; just be sure to space them out with a comma.
The function in the next example only takes one argument, "Myname". The function receives a name as a parameter when it is called, and uses it to print the "Hello + name":
def my_function(Myname): print("Hello", + Myname) my_function("Emmanuel") my_function("Stefan")
Both parameters and arguments can be used to describe data that is passed into a function.
A parameter is listed inside the parentheses, while an argument is the value sent to the function when it is called.
By default, the correct number of parameters must be used to invoke a function. In other words, you must call your function with 2 arguments, not more or less, if it needs 2 arguments.
#A function expects two arguments and gets two arguments def my_function(FirstName, LastName): print(Firstname + " " + Lastname) my_function("Emmanuel", "Akinnimi")
An error will occur if you attempt to call the function with just one or three arguments:
def my_function(FitstName, LastName): print(Firstname + " " + Lastname) my_function("Emmanuel")
If you don't know the number of arguments your function will receive, add a * before the parameter name.
The function will then be provided with a tuple of parameters and will have access to the parts as required:
def my_function(*colour): print("The colour of the ball is " + colour) my_function("red", "Green", "white")
You can send parameters with the key = value syntax as well. The arguments' chronological arrangement is irrelevant.
def my_function(food1, food2, food3): print("I love eating" + food2) my_function(food1 = "rice", food2 = "beans", food3 = "noodles")
If you are unclear about the number of keyword arguments your function will accept, place two asterisks before the parameter name in the function definition: **.
In doing so, the function will be able to effectively access the objects after receiving a dictionary of arguments:
def my_function(**city): print(city["city2"]+ " is located in europe") my_function(city1 = "Lagos", city2 = "Paris", city3 = "California")
Default Parameter Value
Without arguments, the function will use its default value.
def my_function(country = "Belgium"): print("I am from " + country) my_function("Nigeria") my_function("India") my_function() my_function("South korea")
You can supply any data type as an argument in Python (list, tuple, dictionary, strings, numbers, etc.).
If a Tuple is given as an argument, for example, it will still be a Tuple when it reaches the function:
def my_function(Country): for a in Country: print(a) cities = ["New York", "London", "Cairo", "Algiers"] my_function(cities)
To make sure your function return a value or data, you will use the return keyword.
def my_function(a): return 10 * a print(my_function(200)) print(my_function(50)) print(my_function(98))
If your function won't contain any data, you can use the pass keyword to bypass the error Python will throw for an empty function.
def myfunction(): pass
Congratulations to everyone for completing this series with me. We have discussed so many things in Python, and you have written a lot of code.