0% found this document useful (0 votes)
9 views

12 Python Programs

Uploaded by

haashir374
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

12 Python Programs

Uploaded by

haashir374
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Aim:

To find the maximum of a list of numbers.

Algorithm:

• Start the program.

• Define a function max_of_list that takes a list as input.

• Initialize a variable max_num with the first element of the list.

• Loop through the list and compare each element with max_num.

• If an element is greater than max_num, update max_num.

• Return max_num as the maximum value in the list.

Code:

def max_of_list(numbers):
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
return max_num

# Example usage
print(max_of_list([4, 7, 1, 12, 9]))

Sample Output:

12

Viva Questions:

• What is the purpose of this program?

• How does the loop help in finding the maximum number?

• Can this program handle an empty list? Why or why not?

• What would you modify to find the minimum value instead?


• How does this approach compare to using Python’s built-in max() function?

Result:

The program successfully finds the maximum number in a list.

You might also like