Python: Functions

  1. Introduction to Python
  2. Python: Choosing a Text Editor or IDE
  3. Python: Hello World
  4. Python: Variables, Strings and Numbers
  5. Python: Variable Naming
  6. Python: Math, Familiar
  7. Python: Math, Less Familiar
  8. Python: Mathematical Order of Operations
  9. Python: Introducing PEP 8
  10. Python: Text Concatenation
  11. Python: if Statements and Comparison Operators
  12. Python: else and elif statements
  13. Python: Testing Multiple Conditions
  14. Python: Testing Sets of Conditions
  15. Python: Nested if Statements
  16. Python: Lists
  17. Python: Adding To and Changing Lists
  18. Python: Lists: Take a Slice, Delete Elements, Popping Elements
  19. Python: Tuples
  20. Python: for Loops
  21. Python: Nested for Loops
  22. Python: Capturing and Formatting User Input
  23. Python: Dictionaries
  24. Python: Functions
  25. Python: While Loops
  26. Python: Data Files
  27. Python: Using Pexpect
  28. Python : Using Pexpect : ftpTestOffload.sh
  29. Python : Using Pexpect: ftpTest.py
  30. Python: DCL Conversion to Python

Go to Chapter 41 in A Smarter Way.

A function in Python is like a function in math: you put something in, it gets transformed, and you get something out.

There are two stages to using a function, defining it and calling it.

To define a function:

def add_two_numbers():
    first_number = 1
    second_number = 2
    total = first_number + second_number
    print(total)

Then we could call it like this:

add_two_numbers( )

But this gives us fixed values for the numbers.

Go to Chapter 42 in A Smarter Way.

To define a function that accepts input values:

def add_two_numbers(first_number, second_number):
    total = first_number + second_number
    print(total)

add_two_numbers(2, 3)

Because first_number and second_number aren’t assigned any values in the function def parameters, they are positional parameters. You’ll access their values using the names you give them within the parentheses.

See Chapters 43 and 44 in A Smarter Way.

To create a function that accepts input values, but also has default values for the inputs:

def call_cat(summons="Here, ", cat_name="Niko"):
    print(summons + cat_name)

call_cat()

Because these parameters are defined as key/value pairs, they are keyword parameters. You can access and change the values by using the keys as indexes.

Call this function and supply different values than the defaults:

call_cat("Hey, stinker ", "Betty")

Go to Chapter 45 in A Smarter Way.

You can mix positional and keyword parameters, as long as you list them in the right order.

Positional parameters come first.

Keyword parameters without default values come second.

Keyword parameters with default values come last.

def describe_cat(cat_name, cat_color="", eye_color="green"):
        print(cat_name, cat_color, eye_color)

describe_cat("Betty")

Go to Chapter 46 in A Smarter Way.

Functions can have an unknown number of arguments (this allows for future modifications).

You’d still call the function using keywords:

def describe_cat(cat_name, cat_color="", cat_eyes_color="green", **other_cat_details):
    print("My cat " + cat_name + " has " + cat_color + " fur and " + cat_eyes_color + " eyes.")
    for key, value in other_cat_details.items(): 
        print(key + ": " + value) 

describe_cat("Betty", cat_color="gray", cat_eyes_color="blue", coat="tabby", spayed="true")

The extra values are held in a dictionary named for the double-asterisk parameter,  in this case other_cat_details.

Go to Chapter 47 in A Smarter Way.

To return values from a function:

def add_two_numbers(first_number, second_number):
    total = first_number + second_number
    return total

total_of_two_numbers = add_two_numbers(2, 3)
print(total_of_two_numbers)

Obviously, you need to capture that returned value in a variable, in most cases.

Go to Chapter 48 in A Smarter Way.

To use the return values from a function as variables or arguments:

print(add_two_numbers(2, 3))

Go to Chapter 49 in A Smarter Way.

A global variable is defined in the main body of your code:

x = 1

A local variable is defined inside a function:

def set_x():
    x=2

Try the example on page 149 to see how this works.

Exercises

See http://www.asmarterwaytolearn.com/python/44.html
and continue through chapter 49.

  1. In examples.py, define a new function.
  2. Use at least one positional param, one keyword param with no default value, and one keyword param with a default value.
  3. Call the function and print its output.