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

  • Functions with return value

    Printing in the output is great, but most of the useful functions that we have used before like max() or math.sqrt() do not print a value. They return a result. max() returns the maximum among all the values passed to it. math.sqrt() returns the square root of a number which we can use afterward in our program.
    To return a value from a function, we can use the return command:
    def celsius2fahrenheit(degrees):
    	return 9 / 5 * degrees + 32
    
    print(celsius2fahrenheit(10))                            # 50.0
    print(celsius2fahrenheit(10) + celsius2fahrenheit(20))   # 118.0
    We can have functions with more operations in their body before returning:
    def product(numbers):
    	res = 1
    	for n in numbers:
    		res *= n
    	return res
    
    print(product([4, 5, 6]))   # 120
    print(product([-1, 0, 5]))  # 0

    Challenge

    You are asked to write a function called vote, which would return the element which appears the most frequent among the 3 numbers passed to it. If all of them are different, the function should return the first one.
    The input contains 3 numbers.
    The program should print a single number - the most frequent one returned by the function vote.
    Input
    Output
    0 1 0
    0
    1 2 3
    1
     
    To check your solution you need to sign in
    Sign in to continue