How can I… #1 – Find a max value

So, one of the things I wanted to do was to help others with Python as I was learning… and I came across the following reddit in the learnpython subreddit…

If you follow the link, you can see my response… but basically someone was trying to go through an array of numbers (called a list in python) and come up with the max value (I know you can use the max method provided – but I think the exercise was to see how you’d write it)…

My answer, was the following:

def max_number(nums):
    largest = 0
    for num in nums:
        largest = num if num > largest else largest

    return largest

Which for me, was neat as it was one of the first times I used boolean expression from javascript in python (in javascript, it would have been largest = (num > largest) ? num : largest;) but even though its a simple exercise, I felt good that I came up with the pythonic solution relatively easy!

More to come… Until next time…

Never B Flat, Sometimes B Sharp, Always B Natural