/
11.02.2021 at 12:56 am
Cuttings

46 Simple Python Exercises/#01

Define a function max() that takes two numbers as arguments and returns the largest of them.

Problem

Define a function max() that takes two numbers as arguments and returns the largest of them.

Use the if-then-else construct available in Python. (It is true that Python has the max() function built in, but writing it yourself is nevertheless a good exercise.)

Solution

    def new_max(a, b):

    assert b != a, "Values are the same."
    if a > b:
        return a
    elif b > a:
        return b

    print(new_max(46, 46.5))

Filed under:
#
#
Words: 98 words approx.
Time to read: 0.39 mins (at 250 wpm)
Keywords:
, , , , , , , , ,

Potentially related: 46 Simple Python Exercises/#02

Other suggested posts

  1. 01.04.2023 at 11:27 am / Search Notes Fast: With Taskwarrior, jq & ripgrep
  2. 20.06.2022 at 01:56 pm / Cultists of Science
  3. 19.02.2020 at 10:25 am / STEM, STEAM and Art
  4. 23.08.2018 at 01:33 am / Darwin's Details
  5. 08.12.2017 at 12:00 am / The Song of Java Braces
  6. 12.07.2016 at 12:00 am / Refunctin' Blocks
  7. 20.06.2015 at 12:00 am / Mike Tyson on (His) Lawyers
  8. 26.12.2013 at 12:00 am / Teal Stares
  9. 29.11.2013 at 12:00 am / ガラケー Garakei Evolution
  10. 11.11.2013 at 12:00 am / 下戸 - Lower House
© Wan Zafran. See disclaimer.