Functions & Keyboard Input
-
Functions
Part 1
-
What Is It?
- A function is a named sequence of statements that performs a computation.
- When you define a function, you specify the name and the sequence of statements.
- Later, you can “call” the function by name.
-
Why Use Functions?
- Makes a program smaller by eliminating repetitive code.
- Makes a program easier to read and debug.
- Makes code reusable.
-
Defining a function
def best_ice_cream():
print("Mint chocolate chip.")
- def is a keyword that indicates that this is a function definition.
- The name of the function is best_ice_cream.
- The empty parentheses after the name indicate that this function doesn’t take any arguments.
-
Defining a function Continued
- The first line of the function definition is called the header.
- The rest is called the body.
- The header has to end with a colon and the body has to be indented.
- By convention, indentation is always four spaces.
-
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.
- Letters, numbers and underscore are legal.
- The first character can’t be a number.
- You can’t use a keyword as the name of a function.
- You should avoid having a variable and a function with the same name.
-
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
- Some functions require one or more arguments.
- math.power takes two, the base and the exponent.
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
-
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'