Return Values

In [4]:
def square(x):
    # result is a *local* variable
    result = x * x
    #print(result)
    return result
    
num = 10
val = square(num)
print("The result from square({0}) is: {1}".format(num, val))
The result from square(10) is: 100
In [5]:
def square2(x):
    return x ** 2
    
num = 10
val = square2(num)
print("The result from square({0}) is: {1}".format(num, val))
The result from square(10) is: 100

Distance Formula

In [8]:
from graphics import Point
import math

def distance(p1, p2):
    dist = math.sqrt(square(p2.x - p1.x) + square(p2.y - p1.y))
    return dist

point1 = Point(30, 40)
point2 = Point(54, 76)
distance(point1, point2)
Out[8]:
43.266615305567875

Returning Multiple Values

In [9]:
def sum_diff(x, y):
    # x, y are numbers
    _sum = x + y
    diff = x - y
    return _sum, diff
In [12]:
print(sum_diff(23, 33))
(56, -10)
In [13]:
values = sum_diff(23, 33)
print(values)
(56, -10)
In [15]:
values = sum_diff(23, 33)
print("The sum is {0}, and the diff is {1}".format(values[0], values[1]))
The sum is 56, and the diff is -10
In [16]:
the_sum, the_diff = sum_diff(23, 33)
print("The sum is {0}, and the diff is {1}".format(the_sum, the_diff))
The sum is 56, and the diff is -10
In [17]:
def sum_diff_mult(x, y):
    # x, y are numbers
    _sum = x + y
    diff = x - y
    mult = x * y
    return _sum, diff, mult
In [18]:
sum_diff_mult(23, 55)
Out[18]:
(78, -32, 1265)
In [19]:
s, d, m = sum_diff_mult(23, 55)
In [20]:
print(s, d, m)
78 -32 1265
In [21]:
values = sum_diff_mult(23, 55)
print(type(values))
print(values[0])
values[0] = 22
<class 'tuple'>
78
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-8fbad06f6c25> in <module>()
      2 print(type(values))
      3 print(values[0])
----> 4 values[0] = 22

TypeError: 'tuple' object does not support item assignment

None

In [22]:
def does_nothing():
    print("Foobar")
In [23]:
does_nothing()
Foobar
In [24]:
result = does_nothing()
Foobar
In [25]:
print(result)
None
In [ ]:
def does_nothing():
    print("Foobar")
    return
In [28]:
def does_nothing():
    print("Foobar")
    return None

type(None)
Out[28]:
NoneType

Keyword Parameters ("Named Parameters")

In [30]:
def calc(val1, val2, val3, op='+'):
    # val1, val2, val3 are "positional arguments"
    # 
    calc_str = "{0} {3} {1} {3} {2}".format(val1, val2, val3, op)
    print(calc_str)
    return eval(calc_str)

result = calc(34, 457, 23)
print(result)
34 + 457 + 23
514
In [31]:
result = calc(34, 457, 23, op='-')
print(result)
34 - 457 - 23
-446
In [32]:
result = calc(34, 457, 23, op='*')
print(result)
34 * 457 * 23
357374
In [33]:
result = calc(34, 457, 23, op='/')
print(result)
34 / 457 / 23
0.003234706497954524
In [34]:
def calc2(op='+', val1, val2, val3):
    # val1, val2, val3 are "positional arguments"
    # 
    calc_str = "{0} {3} {1} {3} {2}".format(val1, val2, val3, op)
    print(calc_str)
    return eval(calc_str)
  File "<ipython-input-34-0afa211fd65f>", line 1
    def calc2(op='+', val1, val2, val3):
             ^
SyntaxError: non-default argument follows default argument
In [36]:
def calc3(val1, val2, val3, op1='+', op2='+'):
    # val1, val2, val3 are "positional arguments"
    # 
    calc_str = "{0} {3} {1} {4} {2}".format(val1, val2, val3, op1, op2)
    print(calc_str)
    return eval(calc_str)

result = calc3(44, 45, 8686)
print(result)
44 + 45 + 8686
8775
In [37]:
result = calc3(44, 45, 8686, op2='*')
print(result)
44 + 45 * 8686
390914
In [38]:
result = calc3(44, 45, 8686, op1='/', op2='*')
print(result)
44 / 45 * 8686
8492.977777777778
In [40]:
result = calc3(44, 45, 8686, '/')
print(result)
44 / 45 + 8686
8686.977777777778
In [ ]: