-
Notifications
You must be signed in to change notification settings - Fork 1
/
function_lesson.py
84 lines (56 loc) · 2.09 KB
/
function_lesson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
A lesson on functions in Python.
For an excellent and more comprehensive explanation see:
https://realpython.com/defining-your-own-python-function/
"""
# A dictionary of customer numbers, names, and monthly spending.
customers = {
'001': ['Alex', 100.00],
'314': ['Sonya', 25.25],
'174':['Erika', 30.51],
'521': ['Anthony', 34.56],
'231': ['Joseph', 13.76]
}
print(f'keys = {customers.keys()}')
print(f'values = {customers.values()}')
print(f'The list for customer 231 is {customers["231"]}') # Note that we need to use double quotes inside the f-string
def spent(s):
value = customers[s][1]
return value
print(f"Joseph spent ${spent('231')}")
# A simple average value function with positional arguments.
def avg1(v1,v2,v3,v4):
average = (v1 + v2 + v3 + v4) / 4
return average
"""
This creates an error since there are too many positional arguments.
print(f'average = {avg1(10,20,30,40,50)}')
"""
# *args allows you to have a variable number of positional arguments.
# Think of it as creating a list of the arguments called args with a length of len(args).
def avg2(*args):
sum = 0
for item in args:
sum += item
return sum / len(args)
print(f'The average is {avg2(10,20,30,40,50,60)}')
# keyword arguments allow for default values.
# Keyword and positional arguments: positional arguments come first or you will get an error.
def total_int(principal, rate=5):
return (rate / 100) * principal
print(f'interest = {total_int(1000, 7)}')
print(f'interest = {total_int(1000)}')
# **kwargs allows for a variable number of keyword arguments.
# Think of it as creating a dictionary called kwargs with keys
# being the stings of the keyword names and the values being the value
# associate with each keyword.
def function(*args, **kwargs):
sum = 0
for item in args:
sum += item
for item in kwargs.keys():
sum += kwargs[item]
return sum
# Our function will take any number of positional arguments and keyword arguments and return the sum of the numbers.
# Note that we are assuming the arguments can be added together.
print(f'total = {function(10, 20, 30, 40, 50, a=60, b=70, c=80)}')