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

Android 13 features| Android 13 updates| Material You design| language Customizable| Android themes Privacy and security controls in Android 13| App Hibernation in Android 13| Adaptive Charging in Android 13| Android 13 release date| Android Beta Program| Latest Android version

 Android 13: What's New and Exciting? Android 13, codenamed Tiramisu, is the latest version of Google's mobile operating system that is expected to launch in late 2023. Android 13 promises to bring a host of new features and improvements to enhance the user experience, security, and performance of Android devices. In this blog post, we will take a look at some of the most anticipated features of Android 13 and how they will benefit users and developers alike. One of the biggest changes in Android 13 is the introduction of Material You, a new design language that adapts to the user's preferences, mood, and context. Material You allows users to customize their device's theme, colors, fonts, icons, and shapes according to their personal style and taste. Material You also changes dynamically based on the time of day, the wallpaper, the app, and the device mode. For example, if the user switches to dark mode, the system will automatically adjust the colors and brightness to ...

Cafe management software| Restaurant management system| Inventory management for cafes| Cafe billing system

 Cafes have become a popular spot for people to hang out, have meetings or just grab a quick bite. With the increasing number of cafes in cities, managing them efficiently has become a crucial task. This is where a cafe management system comes into play. In this blog post, we will discuss what a cafe management system is, its features and benefits. A cafe management system is a software that helps cafe owners to manage various aspects of their cafe, such as billing, inventory, customer management, employee management and sales tracking. The system automates most of the mundane tasks, freeing up the owner's time to focus on more important aspects of running the cafe. Features of a Cafe Management System: Billing: The cafe management system should have a billing feature that allows the cafe owner to create bills quickly and accurately. It should also have the capability to generate invoices and receipts. Inventory Management: The system should be able to manage the cafe's invento...

Automation and robotics| automation, robotics, artificial intelligence, machine learning, industrial revolution, manufacturing, productivity, efficiency, human-robot collaboration, automation technology, future of work, job displacement, innovation

 Automation and robotics are two technologies that have revolutionized the manufacturing industry. They have enabled manufacturers to produce goods more efficiently and with higher quality than ever before. Automation refers to the use of technology to perform tasks without human intervention, while robotics involves the use of machines to perform tasks that would otherwise require human labor. This blog post will discuss the history, benefits, and challenges of automation and robotics in the manufacturing industry. History of Automation and Robotics The history of automation and robotics can be traced back to the early 20th century when Henry Ford introduced the assembly line. The assembly line allowed workers to specialize in specific tasks and increased productivity. The introduction of programmable logic controllers (PLCs) in the 1970s further advanced automation by allowing machines to be controlled by computers. The first robots were developed in the 1950s and were primarily ...