Python

Python Menu

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.

A block is an area of code of written in the format of:

block_head:
    1st block line
    2nd block line
    ...

Where a block line is more Python code (even another block), and the block head is of the following format: block_keyword block_name(argument1,argument2, ...) Block keywords are "if", "for", and "while".

Functions in Python are defined using the block keyword "def", followed with the function's name as the block's name. For example:

def my_function(): print("This is a Function!") my_function()

Functions may also receive arguments (variables passed from the caller to the function).

def my_function_with_args(username, greeting): print("Hello, %s , From My Function!, I wish you %s"%(username, greeting)) my_function_with_args("Alex", "good day!")

Functions may return a value to the caller, using the keyword- 'return'.

def sum_two_numbers(a, b): return a + b sum = sum_two_numbers(3,2) print(sum)

Calling Functions in Python

Simply write the function's name followed by (), placing any required arguments within the brackets.

# Define our 3 functions def my_function(): print("Hello From My Function!")
def my_function_with_args(username, greeting): print("Hello, %s, From My Function!, I wish you %s"%(username, greeting))
def sum_two_numbers(a, b): return a + b
# print(a simple greeting) my_function()
# prints - "Hello, John Doe, From My Function!, I wish you a great year!" my_function_with_args("John Doe", "a great year!")
# after this line x will hold the value 3! x = sum_two_numbers(1,2) print("The value of x is ", x)

Exercise

In this exercise you'll use an existing function, while adding your own to create a fully functional program. Add a function named list_benefits() that returns the following list of strings:

  • "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"

  • Add a function named build_sentence(info) which receives a single argument containing a string and returns a sentence starting with the given string and ending with the string " is a benefit of functions!"

# Modify this function to return a list of strings as defined above def list_benefits(): pass
# Modify this function to concatenate to each benefit - " is a benefit of functions!" def build_sentence(benefit): pass
def name_the_benefits_of_functions(): list_of_benefits = list_benefits() for benefit in list_of_benefits: print(build_sentence(benefit))
name_the_benefits_of_functions()
# Modify this function to return a list of strings as defined above def list_benefits(): return "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"
# Modify this function to concatenate to each benefit - " is a benefit of functions!" def build_sentence(benefit): return "%s is a benefit of functions!" % benefit
def name_the_benefits_of_functions(): list_of_benefits = list_benefits() for benefit in list_of_benefits: print(build_sentence(benefit))
name_the_benefits_of_functions()
test_output_contains("More organized code is a benefit of functions!",no_output_msg="Don't forget to read the instructions.") test_output_contains("More readable code is a benefit of functions!",no_output_msg="Don't forget to read the instructions.") test_output_contains("Easier code reuse is a benefit of functions!",no_output_msg="Don't forget to read the instructions.") test_output_contains("Allowing programmers to share and connect code together is a benefit of functions!",no_output_msg="Don't forget to read the instructions.") success_msg("Excellent!")

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods