beginner
Find Maximum Value
Find the largest element in a Python list.
Learn comparison algorithms by finding max values manually.
📚 Concepts & Theory
Finding Max
max_val = nums[0]
for n in nums:
if n > max_val:
max_val = n 🎯 Your Challenge
Write a function find_max that returns the maximum value.
📝 Starter Code
Python
def find_max(numbers):
pass
print(find_max([3, 7, 2, 9]))
Solution
Python
def find_max(numbers):
maximum = numbers[0]
for num in numbers:
if num > maximum:
maximum = num
return maximum
print(find_max([3, 7, 2, 9]))
Explanation
Compare each element with current max.
❓ Frequently Asked Questions
Use max() or iterate