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

  • No execution after return

    A function might have multiple return statements. It can return one value in an if statement and another value in an else statement. It’s important to keep in mind that the function stops its execution as soon as it returns a value. As soon as the function reaches a return statement, the program continues its execution from the place where the function was called:
    def process_only_even(n):
    	if n % 2 == 1:
    		return 'This was an odd number!'
    	print('Very interesting number ...', n)
    	n += 18
    	print('Adding 18 will result in:', n)
    	return n
    print(process_only_even(5))    
    # This was an odd number!
    
    print(process_only_even(6))
    # Very interesting number ... 6
    # Adding 18 will result in: 24
    # 24
    So, the function returns only once, and the program continues its execution from the point where the function was called.
    This is especially handy if you can return a value in a loop, and therefore, both the loop and the function will stop at that point and the program will get back to where the function was called from. We can avoid using break in loops like that.

    Challenge

    We will call a number “special” if it’s even and the sum of its last two digits is 7.
    Implement a function is_special(n) that would return True if n is special and False otherwise. If the number is not “special”, the function should also print Not special in the output before returning.
    The input contains a single integer n (100 ≤ n ≤ ).
    The program should print Yes if n is special and No otherwise. In the case of printing No, the program should also print Not special before the No.
    Input
    Output
    116
    Yes
    117
    Not special No
     
    To check your solution you need to sign in
    Sign in to continue