Skip to the content.

Functions & Keyboard Input

-

Functions

Part 1

-

What Is It?

-

Why Use Functions?

-

Defining a function

def best_ice_cream():
    print("Mint chocolate chip.")

-

Defining a function Continued

-

Function Type

Defining a function creates a function object, which has type function:

type(best_ice_cream)
# <type 'function'>

print(best_ice_cream)
# <function best_ice_cream at 0x1073652a8>

-

Function Name Rules

The rules for function names are the same as for variable names.

-

Function Calls

def best_ice_cream():
    print("Mint chocolate chip.")

best_ice_cream()
# Mint chocolate chip.

The function definition has to run before the function gets called.

-

Functions

Part 2

-

Arguments

import math
result = math.pow(2, 3)
print(result)
# 8.0

-

Parameters

Inside the function, arguments are assigned to variables called parameters.

def favorite_ice_cream(fav_ice_cream):
    print("Your favorite ice cream is " + fav_ice_cream)

favorite_ice_cream("mint chocolate chip")
# Your favorite ice cream is mint chocolate chip

-

Functions Can Return Values

def adder(a, b):
    return a + b

sum = adder(1, 2)
print(sum)
# 3

-

Functions Can Call Functions

def say_hello():
    print("Hello")

def say_hello_two_times():
    say_hello()
    say_hello()

say_hello()
# Hello

say_hello_two_times()
# Hello
# Hello

-

Variable Scopes

Variables created inside of a function are local.

name = 'Alice'

def print_name():
    name = 'Bob'
    print(name)

def print_other_name():
    name = 'John'
    print(name)

print_name()
# Bob 

print_other_name()
# John

print(name)
# Alice

-

docstring Revisited

A docstring is a string at the beginning of a function that explains the interface.

def add(x, y):
    """
    Adds two integers and returns their result.
    x and y are integers.
    """
    return x + y

result = add(1, 2)
print(result)

-

Built-in Functions

Built-in Function Docs

Moar

-

Functions

Part 3

-

Anonymous (Lambda) Functions

Lambda expressions are used to create anonymous functions.

-

Lambdas Continued

Suppose you have a single statement function that doubles a given number.

def double(x):
    return x * 2

double(5)
# 10

The above function can be simplified as a lambda with the following syntax:
lambda parameters: expression

double_anon = lambda x: x * 2
double_anon(5)
# 10

-

Currying

Currying provides a way of automatically manage how arguments are passed to functions.

def add_numbers(x, y):
    return x + y

def add_ten(y):
    return add_numbers(10, y)

add_twenty = lambda y: add_numbers(20, y)

add_numbers(1, 2)
# 3

add_ten(10)
# 20

add_twenty(5)
# 25

-

Closures

A Closure is a function object that remembers values in enclosing scopes even if they are not present in memory.

def exponentiator(exponent):
    def raise_x(x):
        return x ** exponent
    return raise_x


square = exponentiator(2)
square(2)
# 4
square(4)
# 16
cube = exponentiator(3)
cube(2)
# 8
cube(3)
# 27

-

Keyboard Input

-

Python 3

name = input("What is your name?\n")
# What is your name?
Seymour
print(name)
# Seymour

Python 2

want = raw_input("What do you want\n")
# What do you want
feed me
print(want)
# 'feed me'

-

The End

Parrot