Class 12: Passing List to the function

Example 1:
Python script to pass the entire list to a function and display all the elements of a list.
Solution:

#function definition
def list_func1(l):
    for i in l:
        print(i)    

#funtion calling
l=['a','b','c','d','e']
list_func1(l)
# Output

a
b
c
d
e
>>> 

Example 2:
Python script pass a list to the function and calculate sum of all its elements.
Solution:

#function definition
def list_func1(l):
    s=0
    for i in l:
        s=s+i
    print('sum of elements= ',s)     

#funtion calling
l=[23,34,56,12]
list_func1(l)
# Output

sum of elements=  125
>>> 

Example 3:
Python script to pass a list to the function and calculate product of all its elements.
Solution:

#function definition
def list_func1(l):
    p=1
    for i in l:
        p=p*i
    print('product of elements= ',p)     

#funtion calling
l=[1,2,3,4,5]
list_func1(l)
# Output

product of elements=  120
>>> 

Example 4:
Python script to pass a list to the function and display the largest element.
Solution:

#function definition
def list_func1(l):
    p=l[0]
    for i in l:
        if(i>p):
            p=i
    print('largest of elements= ',p)     

#funtion calling
l=[199,34,56,789,234]
list_func1(l)
# Output

largest of elements=  789
>>> 

Example 5:
Python script to pass a list to the function and display the lowest element.
Solution:

#function definition
def list_func1(l):
    p=l[0]
    for i in l:
        if(i<p):
            p=i
    print('lowest of elements= ',p)     

#funtion calling
l=[199,34,56,789,234]
list_func1(l)
#Output

lowest of elements=  34
>>>