PYTHON PROGRAMMING CLASS (THE BASICS) || DAY 1 - PYTHON FUNCTIONS | WEEK 4

in Steem4Nigeria2 years ago

python day 1 week 4 banner.PNG


Good day everyone!
Hope you're doing great!! Welcome back to my Python programming class. Today is the first day of the Python Programming class week 4. If you have followed my week 1, 2, & 3 till the end, you must have acquired some basic Python programming skills that can help you start your programming career. However, if you did not follow my week 1, 2, & 3 classes, please visit my blog now @anyiglobal to see all my week 1, 2, & 3 classes.

I brought out this idea of teaching python on steemit because of the need for python programming skills in the world of technology today. This is the new trend in tech now because python can be used for various purposes in many applications ranging from web development, automation, artificial intelligence, software testing to many others. I decided to start this programming class on steemit communities because the communities comprise mainly of people who are still eager to learn new skills. Developers and non-developers alike can use this python.

Note: The English language used in this class is literally understandable by a layman. So let's begin...


PYTHON FUNCTIONS

As the name implies, "Function", to a layman understanding is a fragment of code that performs a specific task. A function is synonymous to subprogram, routine, subroutine, e.t.c.

According to English dictionary:

A function is a routine that receives zero or more arguments and may return a result.

In the Python programming language, a "function" is a block of statements that only runs when it is called by name and invoked whenever required. We may pass parameters or arguments to a function, process them, and return a value as a result. We have two aspects of function. They include;

  • Functions definition, and
  • Function call/invocation

Function Definition
Function definition is the process of creating the collection of statements that carry out the functionality required from the function. This is always defined in the first function statement.

Function Syntax:
def function_name(parameter1, parameter2,..):
    function docstring
    body of the function
    return expression

Note that:

  • The def keyword in the function syntax above simply denotes the definition of the function.
  • A user-defined name(function_name) used to identify the function which comes after the def keyword.
  • Variables called the parameters which is being processed when it is called.
  • The return expression returns the result of the function's execution.

Function call/Invocation:
A function call executes each statement in the function declaration. Let me explain further; a function call is a method of calling a function to execute its functionality or a block of code. We know that "function" is an action word in the English language, so it must be written to perform a specific task. Let's see the syntax of function call below...

Function call Syntax:
function_name(argument1, argument2,...)

Note that:

  • Arguments are set of values passed into a function when it is called. In the function call syntax above, argument1 and argument2 are set of values passed into the function when it is called.

CREATING A FUNCTION
Let's create a function in the example below:

def function_name():
  print("Hello! this is a Function")

Hope you know that without calling this function, it won't execute the print statement in the function block. So therefore, in order to execute the function above, we need to call the function as indicated below...

CALLING A FUNCTION
To call a function, use the function name followed by parenthesis.

For Example;

def function_name():
  print("Hello! this is a Function")

function_name()

Output
Hello! this is a Function

python function.PNG
Python Function

As we can see from the screenshot above, the print statement in the function block executes as soon as we called the function by it's name. You can copy the code and try it out in your text editor to see how it works. Read this article here to learn more about Python Function.


PARAMETERS
As earlier discussed, parameters are passed into the function during the function definition. It holds the value to be used in the function. It is specified in the function definition within a parenthesis "()" after the function_name. In the parenthesis, you can add as many parameters as you want by just separating them with commas.

Let's see from the example below...

For Example;
Let's write a function that prints the username of some steemit users and calls the function by passing an argument to the function.

def name(username):
  print("My username is ", username)
  return username

username("Anyiglobal")
username("Reminiscence01")
username("Ngoenyi")

Output
Anyiglobal
Reminiscence01
Ngoenyi

From the function above, we called the function three times to print some steemit usernames to the screen. Let's the screenshot below...

python function call.PNG
Python Parameter


DEFAULT PARAMETER VALUE
We can assign a default value to a parameter during the function definition. Hence, when the function is called without passing an argument, it makes use of the default parameter. Let's use the same example above to illustrate what we are saying below...

def name(username="Simonnwigwe"):
  print("My username is ", username)

name("Anyiglobal")
name("Reminiscence01")
name()
name("Ngoenyi")

Output
Anyiglobal
Reminiscence01
Simonnwigwe
Ngoenyi

From the function above, we did not pass an argument in one of the function call, but because we specified a default parameter in the function definition, it makes use of it as an argument of the function during execution. Let's see the screenshot below...

python default parameter.PNG
Python default parameter

Read this article here to learn more about Python Function.


ADDING TWO NUMBERS IN FUNCTION
We can as well use function to add numbers in Python. Let's see how to do this in the example below...

For Example;

def add(a, b)
  sum = a + b
  return sum

print(add(2, 8))
print(add(42, 50))
print(add(15, 73))

Output
10
92
88

From the function above, we passed two parameters, return their sum, and finally call the function three times by passing arguments in the function name parenthesis. Let's see the screenshot of the above example below...

python function addition.PNG
Addition two numbers with function

NB: Please Copy the code and try it out in your text editor.


PASSING LIST AS A PARAMETER
We can also pass a list as parameter in a function. A function can accept any parameter type, including strings, numbers, lists, dictionaries, etc., and will handle it as the same data type inside the function. Let's see an example below...

For Example
Let's pass a list of fruit as a parameter to the function.

def my_function(fruit):
  for x in fruit:
   print(x)

fruits = ["Apple", "Orange", "Mango", "Cherry"]
print(my_function(fruits))

Output
Apple
Orange
Mango
Cherry
None

python function list parameter.PNG
Python list parameter

If you observed in the we passed a list of fruits as the argument of the function. Also note that if you pass a list as a parameter, the function will still receive a list. I know the explanation here might be confusing to you, but don't worry I provide you with a material to read in order to have a clearer understanding on Python function. Read this article here to learn more about Python Function. Please if there's any code that is not clear to you, feel free to reach me in the comment section.


PYTHON RECURSION
Recursion is a function that calls itself.

image.png
source

The tree above shows a recursive function. A typical example of a mathematical problem that is best solved with recursive function is Factorial. For example, in mathematics, the factorial of 5 is denoted as 5! i.e. 5 x 4 x 3 x 2 x 1 = 120.

For Example;
Let's write a recursive function to find the factorial of 5.

def factorial(x):
  if x == 1:
    return 1
  else:
    return (x * factorial(x-1))

num = 5
print("The factorial of ", num, "is", factorial(num))

Output
The factorial of 5 is 120

From the code above, we saw how we recurse through the function and the function calls itself many times until the problem is solved. Let's see the screenshot to the code function above below...

python recursive function.PNG
Python recursion

NB: If you don't actually understand this Python recursion, please make use of this material to learn more about that.


Conclusion

Functions are an integral part of the Python programming language. Our Python function breaks our code into smaller chunks for efficiency and readability of our code. A function is composed of two parts: function definition and function call. In this class, we discussed how we define functions before using them in our programs. We also discussed how we call our defined function to execute its statement. The function definition also has a return statement that returns a value in the function block. Finally, we discussed how we can recursively make a function call itself. Please as a student, do well to practice along with the teacher, and also make sure you read other materials from other websites to have an in-depth understanding of what I teach you here. In our next class, we will look at "Python Arrays". Thanks for being part of this class, I appreciate your coming!!



This is the end of this particular class. I believe that if you followed this class till the end, you must have grabbed one or more information from the class. Please make sure to practice along with your laptop and text editor to grab every bit of the information passed.

Please do well to follow this blog, and also don't forget to resteem this post so that it can reach larger number of steemians who wants to learn this skill.


Am glad you participated in this class! I believe you have learnt something new today.

I am @anyiglobal, a Computer Scientist, Software Engineer and a Blogger!



THANKS TO THE COMMUNITY ADMINS FOR THE SMOOTH RUNNING OF THIS COMMUNITY
Cc: @steem4nigeria @reminiscence01 @ngoenyi

Sort:  
Loading...
 2 years ago 

Congratulations, your post has been supported by @steem4nigeria. This is the official community account of Nigerians on Steemit. You can reach us here on our community account.

Manual Curator : @Reminiscence01

Subscribe and Join Steem4Nigeria Telegram
Discord Facebook Twitter

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.030
BTC 71289.49
ETH 3819.48
USDT 1.00
SBD 3.44