fbpx

In this article, we will master the fundamentals of creating Python Function with an example. Let’s take a python function program. Here is the problem statement:

Problem Statement for python function program:

Build a dictionary from below paragraph, with each words as key and Frequency of occurrence of that word as value .Use the below sample paragraph to use .

Paragraph=”””Gift not free is not a gift . Nothing is free in this world . World if full of gifts .“””


Actual Code:

def wordCount(a):
# Initiate a blank dictionary
    wdict={}
#we need to all words in the Paragraph as a list using a.split() and then need to 
iterate over the each list items or words
    for i in a.split() :
#take each word and upper case it and check whether it's not there in the Dictionary we initiated and if not then assign a value of 1 
        if i.upper() not in wdict.keys():
            wdict[i.upper()]=1
#If it's there in the dictionary  we initiated then just add 1 in the value . Consider using upper otherwise it will consider gift and Gift as different words
        else:
            wdict[i.upper()]=wdict[i.upper()]+1
# return the dictionary once the loop ends. 
    return wdict        
        
Paragraph="""Gift not free is not a gift . Nothing is free in this world . World if full of gifts ."""
wordCount(Paragraph)

If you have any doubt please comment below or Contact us and we will be happy to help .

To learn Python check out this .

Check out our courses :here

By tecksed

Leave a Reply

Your email address will not be published. Required fields are marked *

×