user1998529
user1998529

Reputation: 61

How to find the maximum number in a list using a loop?

So I have this list and variables:

nums = [14, 8, 9, 16, 3, 11, 5]

big = nums[0]

spot = 0

I'm confused about how to actually do it. I want to use this exercise to give me a starter. How do I do that on Python?

Upvotes: 8

Views: 127318

Answers (11)

Harun ERGUL
Harun ERGUL

Reputation: 5942

Python already has built in function for this kind of requirement.

list = [3,8,2,9]
max_number = max(list)
print (max_number) # it will print 9 as big number

however if you find the max number with the classic vay you can use loops.

list = [3,8,2,9]

current_max_number = list[0]
for number in list:
    if number>current_max_number:
        current_max_number = number

print (current_max_number) #it will display 9 as big number

Upvotes: 2

helmbert
helmbert

Reputation: 37994

Usually, you could just use

max(nums)

If you explicitly want to use a loop, try:

max_value = None
for n in nums:
    if max_value is None or n > max_value: max_value = n

Upvotes: 16

user17760387
user17760387

Reputation:

I would add this as a reference too. You can use the sort and then print the last number.

nums = [14, 8, 9, 16, 3, 11, 5]

nums.sort()
print("Highest number is: ", nums[-1])

Upvotes: 0

olivia zhou
olivia zhou

Reputation: 11

student_scores[1,2,3,4,5,6,7,8,9]

max=student_scores[0]
for n in range(0,len(student_scores)):
  if student_scores[n]>=max:
    max=student_scores[n]
print(max)
# using for loop to go through all items in the list and assign the biggest value to a variable, which was defined as max.

min=student_scores[0]
for n in range(0,len(student_scores)):
  if student_scores[n]<=min:
    min=student_scores[n]
print(min)
# using for loop to go through all items in the list and assign the smallest value to a variable, which was defined as min.

Note: the above code is to pick up the max and min by using for loop, which can be commonly used in other programming languages as well. However, the max() and min() functions are the easiest way to use in Python to get the same results.

Upvotes: 0

Patrick Atwater
Patrick Atwater

Reputation: 69

To print the Index of the largest number in a list.

numbers = [1,2,3,4,5,6,9]

N = 0
for num in range(len(numbers)) :
  if numbers[num] > N :
    N = numbers[num]
print(numbers.index(N))

Upvotes: 0

Jayna
Jayna

Reputation: 1

For the Max in List Code HS I've managed to get most of the auto grader to work for me using this code:

list = [-3,-8,-2,0]

current_max_number = list[0]
for number in list:
    if number>current_max_number:
        current_max_number = number

print current_max_number

def max_int_in_list():
    print "Here"

I'm not sure where the max_int_in_list goes though. It needs to have exactly 1 parameter.

Upvotes: 0

ERIC
ERIC

Reputation: 1

scores = [12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27,
          28, 29, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 31, 31, 37,
          56, 75, 23, 565]

# initialize highest to zero
highest = 0

for mark in scores:
    if highest < mark:
        highest = mark

print(mark)

Upvotes: -1

Emanuele Paolini
Emanuele Paolini

Reputation: 10162

nums = [14, 8, 9, 16, 3, 11, 5]

big = None

spot = None

for i, v in enumerate(nums):
    if big is None or v > big:
         big = v
         spot = i

Upvotes: 9

Rushy Panchal
Rushy Panchal

Reputation: 17532

To address your second question, you can use a for loop:

for i in range(len(list)):
    # do whatever

You should note that range() can have 3 arguments: start, end, and step. Start is what number to start with (if not supplied, it is 0); start is inclusive.. End is where to end at (this has to be give); end is exclusive: if you do range(100), it will give you 0-99. Step is also optional, it means what interval to use. If step is not provided, it will be 1. For example:

>>> x = range(10, 100, 5) # start at 10, end at 101, and use an interval of 5
>>> x
[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95] # note that it does not hit 100

Since end is exclusive, to include 100, we could do:

>>> x = range(10, 101, 5) # start at 10, end at 101, and use an interval of 5
>>> x
[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100] # note that it does hit 100

Upvotes: 1

Andrea
Andrea

Reputation: 3717

Why not simply using the built-in max() function:

>>> m = max(nums)

By the way, some answers to similar questions might be useful:

Upvotes: 1

alex
alex

Reputation: 490233

Here you go...

nums = [14, 8, 9, 16, 3, 11, 5]

big = max(nums)
spot = nums.index(big)

This would be the Pythonic way of achieving this. If you want to use a loop, then loop with the current max value and check if each element is larger, and if so, assign to the current max.

Upvotes: 10

Related Questions