fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • min max abs

    Python provides a big list of built-in functions that make performing many operations way easier.
    print() and input() are great examples of such built-in functions. When dealing with computations, in many cases it’s required to compute min or max values among several values. To help with those operations python provides built-in functions min(), max(), and abs() to compute the minimum, maximum, and the absolute value correspondingly.
    # max() examples
    print(max(10, 17, 12))          # 17
    a = max(0, -8)                  # 0
    
    # min() examples
    print(min(11, 10, 2, -7, 0))    # -7
    print(min(3, a, -1))            # -1
    
    # abs() examples
    print(abs(-10))                 # 10
    print(abs(10))                  # 10
    x = -2
    x = abs(x)
    print(x)                        # 2
    Notice that abs() takes only a single value and computes the absolute value for only 1 item, while min() and max() operate with an arbitrary number of values.
    Try providing min or max a single value. What will happen? Why?

    Challenge

    Given 4 floating-point numbers in the input, compute their minimum and maximum and print the values in the output side by side. The next 4 lines should contain the absolute values for all the inputted numbers.
    Input
    Output
    0 3.3 4 -2.3
    -2.3 4 0 3.3 4 2.3
     
    To check your solution you need to sign in
    Sign in to continue