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

  • Default arguments

    Some functions in Python take a long list of arguments, and in most cases, it’s convenient to have default values for those arguments. For example, the default value for the end parameter of the print() function is \n - a new line. But if someone wants to print another symbol in the end, they can provide it with end='*', for instance. We can define default arguments in our functions as well:
    def work(hours, from_home=True):
    	if from_home:
    		print('Home sweet home...')
    	else:
    		print("Let's meet some people!")
    	return hours - 2
    
    work(8)
    work(8, True)
    work(8, False)
    In this example, we have to provide the number of hours to the work() function, but we can skip the from_home parameter. If we don’t provide it, it will be set to True as we have defined the function as def work(hours, from_home=True).
     

    Challenge

    Fill in the function to produce the correct result. The function should compute the distance between two points and . In some cases, the second coordinate is omitted, and therefore, the distance should be computed from the origin of the coordinate system - .
    The function should accept 4 arguments and return the distance between those.
    As a reminder, the distance between two points can be calculated with:
    Input
    Output
    0 1
    1
    1 1 1 2
    1
     
    To check your solution you need to sign in
    Sign in to continue