Functions (def) in python | Sole Concept Lab
Python For data scienceTutorials

Functions (def) in python Build a function that counts the words in a short story

Introduction

So far you have seen some of the important concepts of python. This post will take you a little further and show you what you can achieve with very simple python.

What I aim to walk you through in this post are functions. This is, in particular, usefulĀ in any programming language and are extremely easy to use in python.

The aim of this post is to show you how you can define a function followed by giving you an example of how you can use them.

The bonus here is that you will create one simple program! Count the words in a text file
Let’s get started.

Function Defenition

Coding functions in python are extremely simple. The outline to write a function is as shown below. We will cover the very basics of the function here and I will show you how to use them. I will make a quick post later to introduce advanced concepts.

1
2
3
4
5
6
7
8
9
def name_of_the_function(arguments):

'''

Make sure to leave 4 spaces

Your code goes here

'''

Let’s break it down. Firstly a function can take in one or more arguments. It can also have no arguments!

Basically, the function performs operations on the arguments and may or may not return the output. Not too complicated. An example will highlight what I mean.

Remember the problem of identifying odd and even numbers from a list. It’s straightforward but I recommend you read the article on conditional statements and iterators.

I am going to generate random numbers again for this task. Take a look at the first 20 numbers in the picture below.

Image showing random number generated for this task

Image Showing the random numbers generated for this task

Now the task you have is pretty simple. Again you would be creating three lists even, odd, and zeros and updating them with the right numbers. The only difference is here you would be doing it in a function.

The argument to this function is the list of random numbers itself. Nothing to worry! Its simple super simple I would say. Have a look

 

1
2
3
4
5
6
7
8
9
10
11
12
even = [] # a list to store all the even numbers
odd = [] # list to store odd numbers
zeros =[]

def odd_even_zeros(numbers):
for i in numbers:
if i!=0 and i%2 == 0: #check two conditions
even.append(i)
elif i%2 !=0: # Check for odd
odd.append(i)
else : #else it is apparent it should be zeros
zeros.append(i)

Let me take a moment to explain whats going on. Firstly I named the function even_odd_zeros. This function takes an argument numbers which is essentially a list. The rest of the story is known to you. If not go back and catch up. The output is…………. (fill up the blanks here) leave it in the comments.

One very important thing to notice… this function does not have a return value.

Bonus

Now the two bonus problems that I promised you earlier. I will first define the problem statement and go through each of the solutions one by one.

Problem 1: Here you are given a text file and you would like to know the number of words that it contains. How would you go about doing so!

I will definitely write a function to do so. The argument of the function would be the path to my data in this case I am storing the data in a folder called data. The function would then return the number of words.

What exactly this function is doing is that it will open the file read the contents and store it in a variable, count the number of words and return the count. Let’s see what it looks like.

1
2
3
4
5
6
7
8
counter =0
def word_counter(path):
with open(path,'r+') as f: #Open the file
data = f.read().split() #Read the data and split white spaces
for i in data: # I will not comment this
if isinstance(i,str):
counter_words+=1
return counter

I have spoken about functions this far but I more than ignored telling you how to use them. I will tell you with the above example. See this is what you have to do just call the function name and provide it with the proper arguments!!! It’s that easy.

Let me now walk you through the code firstly we set a counter named counter to count the number of words in the file. The data I am using for this case is 100 WEST BY 53 NORTH.

Now coming to the function. Firstly the function opens the file from a path in read mode. Then reads the data and splits it into white spaces.

Then you iterate through the data and update the counter it’s that simple. I will soon post all the code on GitHub and share the link with all of you. To run the function what you have to do is just call the function in a new cell with the path to the file. That’s it!

So, in summary, I introduced you the concepts of function. Illustrated the function with the help of even-odd example covered in the previous post.

I then revealed the bonus that I had promised you. This function enabled you to count the number of words given a file (path to the file).

By now you would have realized how a simple function can do amazing things. Next step is a small exercise followed by other function notations in python.

Happy Learning !

 

Sharing is caring!

Leave a Comment