Write a python program to print a simple list using functions

1. Introduction

In this blog post, we will develop a Python program to print the elements of a simple list using functions. This is a basic yet important task in Python, demonstrating how functions can be used to modularize and manage code effectively, especially when dealing with collections like lists.

2. Program Steps

1. Define a function to print the elements of a list.

2. Create a list with some elements.

3. Call the function and pass the list to it.

3. Code Program


def print_list_elements(input_list):
    # Iterate through the list and print each element
    for element in input_list:
        print(element)

# Step 2: Create a list
example_list = [1, 'Python', 3.14, 'Hello']

# Step 3: Call the function with the list
print_list_elements(example_list)

Output:

The output will be each element of the list printed on a separate line:
1
Python
3.14
Hello

Explanation:

1. The function print_list_elements is defined to take a list as an argument. It uses a for loop to iterate over each element in the list.

2. example_list is created with a mix of integers, strings, and floats. This list is used to demonstrate the function's ability to handle different data types.

3. The function is called with example_list as its argument. Inside the function, each element of the list is printed on a separate line by the print function.

This simple program illustrates how functions in Python can be used to perform operations on list elements, enhancing code readability and reusability.


Comments