w3resource
w3resource logo
Python Exercises

Python: Find the maximum and minimum numbers from a sequence of numbers

Python Basic: Exercise-148 with Solution

Write a Python function to find the maximum and minimum numbers from a sequence of numbers.
Note: Do not use built-in functions.

Sample Solution :-

Python Code :

def max_min(data):
  l = data[0]
  s = data[0]
  for num in data:
    if num> l:
      l = num
    elif num< s:
        s = num
  return l, s

print(max_min([0, 10, 15, 40, -5, 42, 17, 28, 75]))

Sample Output:

(75, -5)

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor :

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python function to check whether a number is divisible by another number. Accept two integers values form the user.
Next: Write a Python function that takes a positive integer and returns the sum of the cube of all the positive integers smaller than the specified number.