9.2. Function Return

9.2.1. Return Keyword

  • return keyword indicates outcome of the function

  • Code after return will not execute

def hello():
    return 'ehlo world'

print(hello())
# 'ehlo world'
def hello():
    return 'ehlo world'
    print('This will not be executed')

print(hello())
# 'ehlo world'

9.2.2. Returning Simple Types

def my_function():
    return 42
def my_function():
    return 13.37
def my_function():
    return 'Mark Watney'
def my_function():
    return True

9.2.3. Return None

  • Python will return None if no explicit return is specified

def my_function():
    return None
def my_function():
    print('ehlo world')
def my_function():
    pass
def my_function():
    """My function"""

9.2.4. Return Sequences

def my_function():
    return 42, 13.37, 'Mark Watney'
def my_function():
    return (42, 13.37, 'Mark Watney')
def my_function():
    return [42, 13.37, 'Mark Watney']
def my_function():
    return {42, 13.37, 'Mark Watney'}

9.2.5. Return Mapping

def my_function():
    return {'first_name': 'Mark', 'last_name': 'Watney'}

9.2.6. Returning nested types

def my_function():
    return [
        ('Mark', 'Watney'),
        {'Jan Twardowski', 'Melissa Lewis'},
        {'astro': 'Иванович', 'agency': {'name': 'Roscosmos'}},
        {'astro': 'Jiménez', 'missions': ('Mercury', 'Gemini', 'Apollo')},
    ]

9.2.7. Assignments

9.2.7.1. Return Numbers

English
  1. Define function add without parameters

  2. Function should return sum of 42 and 13.37

  3. Call function and intercept returned value

  4. Print value

Polish
  1. Zdefiniuj funkcję add bez parametrów

  2. Funkcja powinna zwracać sumę 42 and 13.37

  3. Wywołaj funkcję i przechwyć zwracaną wartość

  4. Wyświetl wartość