0

I tried searching this up before asking it here, but from what I have searched it does not seem to be asked in the form that I am thinking.

For a simple example, imagine a situation where I have a list of integers [1, 2, 3, 4, 5] and I want to iterate through the list and save the largest number that is divisible by 2. (And yes, if the list was not ordered this code wouldn't work, but for the sake of example imagine all the lists are in numbered order)

nums = [1, 2, 3, 4, 5]

for number in nums:
    if number % 2 == 0:
        max_num = number

print(str(max_num))

In this example, I would be able to print max_num without issue. However, if the list nums was [1, 3, 5] for example, max_num would never be set, and I would be referencing a variable I never instantiated.

My question is how would I properly set the variable prior to the loop? To state it otherwise, would you ever need to describe the data type of a variable prior to setting it, akin to Java and C++? Is it even necessary?

What I have been doing recently is something like this:

nums = [1, 2, 3, 4, 5]
max_num = None      # <- added this

for number in nums:
    if number % 2 == 0:
        max_num = number

if max_num is not None:     # <- added if statement
    print(str(max_num))

But I feel like there would be a better way of doing it. I also had the idea to try this:

nums = [1, 2, 3, 4, 5]
max_num = int          # <- this was changed

for number in nums:
    if number % 2 == 0:
        max_num = number

print(str(max_num))

The issue with this way it it makes max_num type(int), which I don't believe is correct either.

Any feedback would be greatly appreciated.

EDIT: I am not specifically looking for a solution to the example I have, but instead a pythonic way of setting a variable.

Mitchnoff
  • 495
  • 2
  • 7
  • Your two solutions are equally good for the purposes they serve. They may be other solutions available to you. I think you need to think what each case means to you: 1. There is a `max_num` and you can print it. 2. There is no number available to print. – quamrana Aug 11 '21 at 14:59
  • `max(number for number in nums if number % 2 == 0)` would throw a `ValueError` in the absence of even numbers, unless you added a value as the `default` keyword argument. – jonrsharpe Aug 11 '21 at 15:05
  • I get the impression the OP isn't specifically looking for a "max even number" solution (if so, stepping through the list backwards would be more efficient) but is rather looking for a Pythonic manner to detect whether the desired result was found. – sj95126 Aug 11 '21 at 15:08
  • Do not do `max_num = int`. That is not a declaration even if it looks like one. It makes `max_num` a synonym of `int`, so that `max_num(math.pi)` will return `3`. That sort of thing will come back to bite you. Do `max_num = None` as you have been doing. – BoarGules Aug 11 '21 at 15:13
  • @sj95126 is correct, I am not looking for an answer to the specific example I put in the question, the example was simply to help me get my point across with finding a pythonic way to see if a variable was ever set prior to being accessed. – Mitchnoff Aug 11 '21 at 15:46

1 Answers1

0

Your first example exactly does what you need. It does assign a default value None to max_num.

You don't need to specifically check if max_num is not None. If at all the list contains any value that is divisible by 2, it will be assigned to max_num. If not, max_num will be None ( the default value) which means - No number divisible by 2 exists in the list.

nums = [1, 3, 5]
max_num = None      # <- added this

for number in nums:
    if number % 2 == 0:
        max_num = number

print(max_num)

The above code, if

nums = [1, 3, 4, 5] - Returns 4 -> Max. number divisible by 2 exists
nums = [1,3,5] - Returns None -> No number divisible by 2 exists.
Ram
  • 4,724
  • 2
  • 14
  • 22
  • Say I need to use the number in a function ( do_sth(max_num) ). In this case, max_num needs to be an integer, and not None. would the best way to do this be a simple if not None: statement? – Mitchnoff Aug 11 '21 at 15:53
  • You can also look into type hinting in python. – Zev Aug 11 '21 at 16:18
  • @Zev that fits really well with my issue, I believe this answers my problem. – Mitchnoff Aug 11 '21 at 18:01