Home > Back-end >  Print Values from a List
Print Values from a List

Time:10-03

I'm exploring Python as late as now, so please bear with me if you find this question relatively easy. Basically, what I want to display or print are the maximum and minimum values in the array/list, but alas, I can't seem to get the values from it. Thanks in advance for your help guys.

def f(x):
  y = x*x*x - 4*x   1
  return y
  
# assign the f(x) value to variables a,b,c,d and pass them to a list or array called max_min(data)

x = -2.0
print("f(", x ,") = ", f(x))
a = f(x)

x = -0.75
print("f(", x, ") = ", f(x))
b = f(x)

x = 0.5
print("f(", x, ") = ", f(x))
c = f(x)

x = 2.0
print("f(", x, ") = ", f(x))
d = f(x)

def max_min(data):              # initializing max_min(data) list
  max_val = data[0]             # initializing the position of the variable in the array
  min_val = data[0]             # initializing the position of the variable in the array
  for num in data:              
    if num > max_val:           # testing the initial value of num (0 by default)
      max_val = num
    elif num < min_val:
        min_val = num
  return max_val, min_val

print(max_min([a, b, c, d]))    # 
print("----------------------------------------------")
print("The maximum value in the list = ")
print("The minimum value in the list = ")

CodePudding user response:

def f(x):
  y = x*x*x - 4*x   1
  return y
  
# assign the f(x) value to variables a,b,c,d and pass them to a list or array called max_min(data)

x = -2.0
print("f(", x ,") = ", f(x))
a = f(x)

x = -0.75
print("f(", x, ") = ", f(x))
b = f(x)

x = 0.5
print("f(", x, ") = ", f(x))
c = f(x)

x = 2.0
print("f(", x, ") = ", f(x))
d = f(x)

def max_min(data):              # initializing max_min(data) list
  max_val = data[0]             # initializing the position of the variable in the array
  min_val = data[0]             # initializing the position of the variable in the array
  for num in data:              
    if num > max_val:           # testing the initial value of num (0 by default)
      max_val = num
    elif num < min_val:
        min_val = num
  return max_val, min_val

a, b, c, d = 10, 20, 30, 5

maxmin = max_min([a, b, c, d])
print(maxmin)    # 
print("----------------------------------------------")
print("The maximum value in the list = "   str(maxmin[0]))
print("The minimum value in the list = "   str(maxmin[1]))

CodePudding user response:

Like this:

print(max_min([a, b, c, d]))    # 
print("----------------------------------------------")
max_num, min_num = max_min([a,b,c,d])
print(max_num)
print(min_num)

The reason for this is because you are returning two values from the same function, it will become a tuple. To easily print the values of the tuple separately, you just assign the values to two separate variables.

  • Related