input: Write a function to optimize the Python program that finds the maximum number in a list. The function should iterate through the list and keep track of the largest number encountered. If the list is empty, the function should return None.
  assert max_num([9, 2, 5, 6, 4, 1])==9
  assert max_num([1, 3, 4, 5, 2])==5
  assert max_num([7])==7
  assert max_num([])==None
  assert max_num([-3, -5, -1, -2])==-1
output: def max_num(lst):
      if not lst:
          return None
      max_val = lst[0]
      for num in lst:
          if num > max_val:
              max_val = num
      return max_val