We can pass a group of elements like numbers or strings into a list and then we can pass this list to the function to process those elements.
Example 1 : Passing list of Integers to a Function
def funcalc(lst):
"""function to calculate sum and average of list of numbers"""
n = len(lst)
total = 0
for i in lst:
total = total + i
avg = total / n
return total, avg
# reading the user input
print('Enter the list of numbers separated by space:')
lst = [int (j) for j in input().split()]
# calling the function
x, y = funcalc(lst)
print('The total of numbers is: ', x)
print('The average of numbers is: ', y)
#Input:
Enter the list of numbers separated by space:
2 3 4 5
#Output:
The total of numbers is: 14
The average of numbers is: 3.5
# ------------------------------------------------------------------------------------------------------------------------ #
Example 1 : Passing list of Strings to a Function
def dispstrings(lst):
"""function to display the list of strings"""
print("The strings you entered are: ")
for i in lst:
print(i)
# reading the user input
print('Enter the list of strings seperated by comma:')
lst = [j for j in input().split(',')]
# calling the function
dispstrings(lst)
#Input:
Enter the list of strings seperated by comma:
Sun,Mon,Wed,Fri
#Output:
The strings you entered are:
Sun
Mon
Wed
Fri
#--------------------------------------------------------------Thanks--------------------------------------------------------------#
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.