6

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?

0

11 Answers 11

16

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
Sign up to request clarification or add additional context in comments.

7 Comments

What if a number was -2 in the list?
To add some explanation to the loop solution: To get the biggest number, you iterate through the list and remember the biggest number you have encountered so far. That way when you reach the end, you will have the biggest number of them all remembered.
@alex You can set max = nums[0]; also max is not the best variable name for this.
Granted, the name max is ill-chosen. -1 an initial value fails, if all numbers in the list are < -1. I edited the post accordingly.
(int) > None will raise a TypeError.
|
10

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.

6 Comments

but there is no loop - at least not explicite
@TheodrosZelleke for i in range(len(list)): # for loop iterates through it. That's an explicit way of repeating something for the length of the list. This will work for indices; if you want to do something x amount of times, the range() 'end' argument should be x + 1. So to do something x amount of times where x is the length of the list, do: for i in range(len(list) + 1):.
@F3AR3DLEGEND, thanks for the clarification -- however, my comment was meant as a "kind of criticism" not a question.
for i in nums: return max(nums) :p
@F3AR3DLEGEND list is a builtin function, best not to use it as a variable name. and you're better off doing for i, _ in enumerate(seq) rather than range(len(seq))
|
9
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

1 Comment

For bonus points, make sure nums is not empty before assigning big = nums[0], and if so, raise an exception (ValueError would be a good choice).
2

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

Comments

1

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

>>> m = max(nums)

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

1 Comment

OP explicitly stated (s)he wanted a loop.
1

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

Comments

0

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.

1 Comment

I'm not sure where the max_int_in_list goes though. It needs to have exactly 1 parameter. -- are you answering this question, or asking a new, related question? Stackoverflow is not a discussion forum, it's a question and answer site, so if you need a follow-on question answered, you should delete this answer, and ask here, possibly linking back to this question for reference. See also: How to Ask.
0

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))

Comments

0
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.

1 Comment

Welcome to StackOverflow. While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
0

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])

Comments

-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)

1 Comment

highest will be zero if every values are negative. Then answer will be wrong

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.