Skip to main content

Define a function that computes the length of a given list or string| Python for beginner

 Python is a popular programming language that is widely used by developers and data scientists. One of the fundamental concepts in Python is functions, which are self-contained blocks of code that perform a specific task. In this blog post, we will discuss how to define a function that computes the length of a given list or string using Python.



What is a Function?

A function is a block of code that performs a specific task. Functions are self-contained and can be called from anywhere in a program. Functions are an essential part of Python and are used extensively in programming.


Defining a Function in Python

In Python, functions are defined using the 'def' keyword, followed by the name of the function, and a set of parentheses. The body of the function is indented and contains the code that performs the task.


Here is an example of a function that computes the length of a given list:

def compute_length(list):

    length = 0

    for item in list:

        length += 1

    return length


In this function, the 'for' loop iterates over each item in the list and increments the 'length' variable. Finally, the function returns the length of the list.

Similarly, we can define a function that computes the length of a given string:
def compute_length(str):
    length = 0
    for char in str:
        length += 1
    return length

In this function, the 'for' loop iterates over each character in the string and increments the 'length' variable. Finally, the function returns the length of the string.

Using the Function

Once the function is defined, we can call it from anywhere in the program. Here is an example of how to use the 'compute_length' function:
my_list = [1, 2, 3, 4, 5]
print("The length of the list is:", compute_length(my_list))

my_str = "Hello World"
print("The length of the string is:", compute_length(my_str))

In this example, we first define a list 'my_list' and a string 'my_str'. We then call the 'compute_length' function, passing the list and string as arguments. The function returns the length of the list and string, which we print to the console.

Conclusion
Functions are a fundamental concept in Python, and are essential for writing clean, organized, and reusable code. In this blog post, we discussed how to define a function that computes the length of a given list or string using Python. By using functions, we can write more efficient and maintainable code, making Python a powerful language for developers and data scientists alike. 

Complete code - 
def len(s):
    count=0
    for i in s:
        if i!= ' ':
            count+=1
    print("The total length of the string is : ", count)

Comments

Popular posts from this blog

S.Y.I.T Sem 3 Python Practical | Python| Python practical 1a| Print name and age +100 | python for beginner

 Hello reader's , This blog is for those students who are studying in second year of information technology. This blog is for practical of python  subject which is in sem 3. In this blog you are going to see that how you can ask user to enter their name and age. Print out a message addressed to them that tells them the year that they will turn 100 year old. For performing this practical you must have install python on your laptop or pc. to download python  - https://www.python.org/downloads/  python website  go to the link and download python and you need a IDE which supports python,  i am using VS Code, you can use python IDE too.  vs code screen After the successful installation open ide and type the code or simply copy the code from given link in below and run it to see the output. code -  name = input("Enter your name: ") age = int(input("Enter your age: ")) year = str((2021 - age)+100) print(name + "will be 100 years old in the year"+year)

Simple calculator using python| Python | Calculator | python for beginner

 Hello reader's, Today you are going to see that how can you develop your own calculator using python . simple calculator using python To perform this activity you must have installed python on your system.   To install python perform the following steps :- 1) Go to - https://www.python.org/downloads/  2) click "Download" . 3) Download the any latest python version from the given website . 4) When download complete just open your file explorer and double click on downloaded python file and install the python in which directory you want.  5) After the successful installation just double click on python icon on desktop to open python IDE.  python logo  After the successful installation of python you need a IDE which support python. I am using VS code(Visual studio code), you can use pycharm or python ide too. how to install VS Code? To install VS code(Visual Studio code) follow these steps :-  1) Go to - https://code.visualstudio.com/download  2) S...

Print calendar using python | Python| Calendar using Python |python for beginner

Hello readers, Today, we are going to see that how can we print the calendar using python.   Print calendar using python  In this blog you are going to see that how can you print calendar using python programming language. To perform this activity you must know the basics of python because we are going to use python programming language.   To perform this activity you must have to install python on your laptop or desktop. If you don't know that how to install python then follow these steps.    To install python perform the following steps :- 1) Go to - https://www.python.org/downloads/  2) click "Download" . 3) Download the any latest python version from the given website . 4) When download complete just open your file explorer and double click on downloaded python file and install the python in which directory you want.  5) After the successful installation just double click on python icon on desktop to open python IDE.  python logo  Af...