Given a Linked List Containing Brand Names of Biscuits Sold by a Retail Store Write a Python Function Which Finds And Returns The Total Number of Biscuit Brands.

Hi in this article you will learn a Python program to get the count of the number of biscuits brands  present in the retail store.

Let's try to write a Python function to solve this problem.

 

Given a Linked List Containing Brand Names of Biscuits Sold by a Retail Store Write a Python Function Which Finds And Returns The Total Number of Biscuit Brands.

 

 


Given a Linked List Containing Brand Names of Biscuits Sold by a Retail Store Write a Python Function Which Finds And Returns The Total Number of Biscuit Brands.

First let's define a function named count_brands() and pass the input argument as a list of biscuit brands to it.  Take an integer variable named as total and set the variable to zero,  the total variable will be used for counting the number of biscuit brands. Use the for loop to iterate all the items present in the biscuit list.


Inside the for loop keep incrementing the total variable for each iteration once the for loop ends return the total variable.

def count_brands(biscuits_list):
   total = 0
   for _ in biscuits_list:
       total = total + 1
   return total





In the main program take a variable name biscuits and set it to a list of biscuit brands,  the list will store the list of  biscuit brands present in India as shown below.

Take another integer variable named count to store the return value of the function and  then call the function count_brands() to count the number of brands then using the print statement print the count.


biscuits = ["Sunfeast", "Britannia", "Cadbury", "Patanjali", "Marie", "Parle", "Anmol"]
count = count_brands(biscuits)
print("Count of Number of Biscuits Brands are : ", count)






Complete Python Program
===========================

def count_brands(biscuits_list):
    total = 0
    for _ in biscuits_list:
        total = total + 1
    return total

 

biscuits = ["Sunfeast", "Britannia", "Cadbury", "Patanjali", "Marie", "Parle", "Anmol"]
count = count_brands(biscuits)
print("Count of Number of Biscuits Brands are : ", count)






Output
=============

Count of Number of Biscuits Brands are :  7



Optional Learning
====================

 

Given a Linked List Containing Brand Names of Biscuits Sold by a Retail Store Write a Python Function Which Finds And Returns The Total Number of Biscuit Brands.

 

 

The above Python program can also be modified to accept the list of brands from the user as well.  For this you have to take an empty list and ask the user to enter the biscuit brands as input using the input function call. As shown below

Complete Python Program
===========================
 

def count_brands(biscuits_list):
    total = 0
    for _ in biscuits_list:
        total = total + 1
    return total


length = int(input("Enter Number of Brands : "))
biscuits = []
for _ in range(length):
    temp = input("Enter Brand Name : ")
    biscuits.append(temp)

count = count_brands(biscuits)
print("Count of Number of Biscuits Brands are : ", count)





Output 1
==============

Enter Number of Brands : 3
Enter Brand Name : marie
Enter Brand Name : anmol
Enter Brand Name : sunfeast
Count of Number of Biscuits Brands are :  3






Conclusion
=================

Execute the above Python program by modifying the declared list of biscuit brands,  try reducing or increasing the biscuit brands by adding the biscuit brands to  the list.

Comment it down below if you have any queries regarding the above python program


Post a Comment

0 Comments