/
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. 29.03.2023 at 10:57 am / Moving Away from Todoist - to Taskwarrior, SSH & Dropbox - Part 2
  2. 04.01.2023 at 10:52 pm / Newton's Commonplace Beginnings
  3. 11.06.2022 at 09:09 pm / 大蛇に嫁いだ娘 - The Girl Married to the Giant Serpent
  4. 11.02.2021 at 01:13 am / 46 Simple Python Exercises/#02
  5. 13.08.2020 at 10:00 am / Consenting Pythonic Adults
  6. 07.10.2019 at 11:22 am / 3D & 2D - The Demarcation
  7. 06.09.2015 at 12:00 am / Big Illegal Guns
  8. 07.07.2014 at 12:00 am / If I Cannot Strengthen Our Bench
  9. 28.11.2013 at 12:00 am / ケチ - Scroogean Niggardliness
  10. 08.11.2013 at 12:00 am / Red Riding Hood's Examination-in-Chief
© Wan Zafran. See disclaimer.