-
Notifications
You must be signed in to change notification settings - Fork 14
/
07-Functions.html
87 lines (76 loc) · 44.5 KB
/
07-Functions.html
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
77
78
79
80
81
82
83
84
85
86
87
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>07-Functions</title>
<script type="text/javascript" src="07-Functions_files/MathJax.js" charset="utf-8"></script>
<script type="text/javascript">
// MathJax disabled, set as null to distingish from *missing* MathJax,
// where it will be undefined, and should prompt a dialog later.
window.mathjax_url = "mathjax/MathJax.js";
</script>
<link rel="stylesheet" href="07-Functions_files/jquery-wijmo.css" type="text/css">
<link rel="stylesheet" href="07-Functions_files/codemirror.css">
<link rel="stylesheet" href="07-Functions_files/markdown.css">
<link rel="stylesheet" href="07-Functions_files/rst.css">
<link rel="stylesheet" href="07-Functions_files/ipython.css">
<link rel="stylesheet" href="07-Functions_files/default.css">
<link rel="stylesheet" href="07-Functions_files/prettify.css">
<link rel="stylesheet" href="07-Functions_files/boilerplate.css" type="text/css">
<link rel="stylesheet" href="07-Functions_files/layout.css" type="text/css">
<link rel="stylesheet" href="07-Functions_files/base.css" type="text/css">
<link rel="stylesheet" href="07-Functions_files/notebook.css" type="text/css">
<link rel="stylesheet" href="07-Functions_files/renderedhtml.css" type="text/css">
<meta name="read_only" content="False">
<style type="text/css">#MathJax_Zoom {position: absolute; background-color: #F0F0F0; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid black; margin: 0; font-family: serif; font-size: 85%; font-weight: normal; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; box-shadow: 5px 5px 15px #AAAAAA; -webkit-box-shadow: 5px 5px 15px #AAAAAA; -moz-box-shadow: 5px 5px 15px #AAAAAA; -khtml-box-shadow: 5px 5px 15px #AAAAAA; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_ZoomOverlay {position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
</style><style type="text/css">#MathJax_About {position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: #DDDDDD; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 15px; -webkit-border-radius: 15px; -moz-border-radius: 15px; -khtml-border-radius: 15px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_Menu {position: absolute; background-color: white; color: black; width: auto; padding: 5px 0px; border: 1px solid #CCCCCC; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 5px; -webkit-border-radius: 5px; -moz-border-radius: 5px; -khtml-border-radius: 5px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_MenuItem {padding: 1px 2em; background: transparent}
.MathJax_MenuTitle {background-color: #CCCCCC; margin: -5px 0 0 0; text-align: center; font-style: italic; font-size: 80%; color: #444444; padding: 2px 0; overflow: hidden}
.MathJax_MenuArrow {position: absolute; right: .5em; color: #666666}
.MathJax_MenuActive .MathJax_MenuArrow {color: white}
.MathJax_MenuCheck {position: absolute; left: .7em}
.MathJax_MenuRadioCheck {position: absolute; left: .7em}
.MathJax_MenuLabel {padding: 1px 2em 3px 1.33em; font-style: italic}
.MathJax_MenuRule {border-top: 1px solid #DDDDDD; margin: 4px 3px}
.MathJax_MenuDisabled {color: GrayText}
.MathJax_MenuActive {background-color: #606872; color: white}
</style><style type="text/css">#MathJax_Message {position: fixed; left: 1px; bottom: 2px; background-color: #E6E6E6; border: 1px solid #959595; margin: 0px; padding: 2px 8px; z-index: 102; color: black; font-size: 80%; width: auto; white-space: nowrap}
#MathJax_MSIE_Frame {position: absolute; top: 0; left: 0; width: 0px; z-index: 101; border: 0px; margin: 0px; padding: 0px}
.MathJax_Error {color: #CC0000; font-style: italic}
</style><style type="text/css">@media print { body { overflow: visible !important; } }.ui-widget-content { border: 0px; }</style><style type="text/css">#MathJax_Zoom {position: absolute; background-color: #F0F0F0; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid black; margin: 0; font-family: serif; font-size: 85%; font-weight: normal; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; box-shadow: 5px 5px 15px #AAAAAA; -webkit-box-shadow: 5px 5px 15px #AAAAAA; -moz-box-shadow: 5px 5px 15px #AAAAAA; -khtml-box-shadow: 5px 5px 15px #AAAAAA; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_ZoomOverlay {position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
</style><style type="text/css">#MathJax_About {position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: #DDDDDD; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 15px; -webkit-border-radius: 15px; -moz-border-radius: 15px; -khtml-border-radius: 15px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_Menu {position: absolute; background-color: white; color: black; width: auto; padding: 5px 0px; border: 1px solid #CCCCCC; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 5px; -webkit-border-radius: 5px; -moz-border-radius: 5px; -khtml-border-radius: 5px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_MenuItem {padding: 1px 2em; background: transparent}
.MathJax_MenuTitle {background-color: #CCCCCC; margin: -5px 0 0 0; text-align: center; font-style: italic; font-size: 80%; color: #444444; padding: 2px 0; overflow: hidden}
.MathJax_MenuArrow {position: absolute; right: .5em; color: #666666}
.MathJax_MenuActive .MathJax_MenuArrow {color: white}
.MathJax_MenuCheck {position: absolute; left: .7em}
.MathJax_MenuRadioCheck {position: absolute; left: .7em}
.MathJax_MenuLabel {padding: 1px 2em 3px 1.33em; font-style: italic}
.MathJax_MenuRule {border-top: 1px solid #DDDDDD; margin: 4px 3px}
.MathJax_MenuDisabled {color: GrayText}
.MathJax_MenuActive {background-color: #606872; color: white}
</style><style type="text/css">#MathJax_Message {position: fixed; left: 1px; bottom: 2px; background-color: #E6E6E6; border: 1px solid #959595; margin: 0px; padding: 2px 8px; z-index: 102; color: black; font-size: 80%; width: auto; white-space: nowrap}
#MathJax_MSIE_Frame {position: absolute; top: 0; left: 0; width: 0px; z-index: 101; border: 0px; margin: 0px; padding: 0px}
.MathJax_Error {color: #CC0000; font-style: italic}
</style></head><body style="overflow: auto;"><div tabindex="2" class="cell text_cell border-box-sizing ui-widget-content ui-corner-all"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 4px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h1>Functions in Python</h1>
<p>Functions without arguments are straighforward. You can use the <code>def</code> keyword to define a
function:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [2]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 65px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span> print 'Called myfunction'</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">def</span><span class="cm-null"> </span><span class="cm-variable">myfunction</span><span class="cm-null">():</span></pre><pre><span class="cm-null"> </span><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-string">'Called myfunction'</span></pre><pre><span class="cm-variable">myfunction</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>Called myfunction</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 1px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><p>We can also define functions with arguments:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [14]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 65px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span> print 'Called myfunction2(%r,%r)' % (a,b)</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">def</span><span class="cm-null"> </span><span class="cm-variable">myfunction2</span><span class="cm-null">(</span><span class="cm-variable">a</span><span class="cm-null">, </span><span class="cm-variable">b</span><span class="cm-null">):</span></pre><pre><span class="cm-null"> </span><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-string">'Called myfunction2(%r,%r)'</span><span class="cm-null"> </span><span class="cm-operator">%</span><span class="cm-null"> (</span><span class="cm-variable">a</span><span class="cm-null">,</span><span class="cm-variable">b</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">myfunction2</span><span class="cm-null">(</span><span class="cm-string">'avalue'</span><span class="cm-null">, </span><span class="cm-string">'bvalue'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>Called myfunction2('avalue','bvalue')</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 3px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><p>When calling a function with arguments, you may also pass the arguments <em>by name</em> rather than
<em>positionally.</em> This is often useful when there are a large number of arguments and you
don't want to remember the order in which they appear.</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [15]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>myfunction2(b='bvalue first', a='avalue second')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">myfunction2</span><span class="cm-null">(</span><span class="cm-variable">b</span><span class="cm-null">=</span><span class="cm-string">'bvalue first'</span><span class="cm-null">, </span><span class="cm-variable">a</span><span class="cm-null">=</span><span class="cm-string">'avalue second'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>Called myfunction2('avalue second','bvalue first')</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 4px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h2><code>lambda</code> functions</h2>
<p>Python provides a special form of defining functions that consist of nothing more than a single
expression using the <code>lambda</code> keyword:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [19]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 47px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>lambda_adder = lambda a,b: a+b</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">lambda_adder</span><span class="cm-null"> = </span><span class="cm-keyword">lambda</span><span class="cm-null"> </span><span class="cm-variable">a</span><span class="cm-null">,</span><span class="cm-variable">b</span><span class="cm-null">: </span><span class="cm-variable">a</span><span class="cm-operator">+</span><span class="cm-variable">b</span></pre><pre><span class="cm-variable">lambda_adder</span><span class="cm-null">(</span><span class="cm-number">1</span><span class="cm-null">, </span><span class="cm-number">2</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[19]:</div><div class="box_flex1 output_subarea output_text"><pre>3</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 5px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><p>Note the fact that <code>lambda</code> returns the function object itself. This form is often used
when a function needs to be passed as an argument to another function, as in a callback. Also
note the fact that the <code>return</code> statement is implicit.</p>
<p>The equivalent function defined with <code>def</code> would be the following</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [21]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 65px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>def lambda_adder_equiv(a,b):</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">def</span><span class="cm-null"> </span><span class="cm-variable">lambda_adder_equiv</span><span class="cm-null">(</span><span class="cm-variable">a</span><span class="cm-null">,</span><span class="cm-variable">b</span><span class="cm-null">):</span></pre><pre><span class="cm-null"> </span><span class="cm-keyword">return</span><span class="cm-null"> </span><span class="cm-variable">a</span><span class="cm-operator">+</span><span class="cm-variable">b</span></pre><pre><span class="cm-variable">lambda_adder_equiv</span><span class="cm-null">(</span><span class="cm-number">1</span><span class="cm-null">,</span><span class="cm-number">2</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[21]:</div><div class="box_flex1 output_subarea output_text"><pre>3</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 4px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h2>Default arguments</h2>
<p>You can define a function with default argument values. If a value is not passed for an argument
with a default value, the default will be used instead:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [17]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 65px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span> print 'Called myfunction3(%r,%r)' % (a,b)</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">def</span><span class="cm-null"> </span><span class="cm-variable">myfunction3</span><span class="cm-null">(</span><span class="cm-variable">a</span><span class="cm-null">, </span><span class="cm-variable">b</span><span class="cm-null">=</span><span class="cm-string">'default value'</span><span class="cm-null">):</span></pre><pre><span class="cm-null"> </span><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-string">'Called myfunction3(%r,%r)'</span><span class="cm-null"> </span><span class="cm-operator">%</span><span class="cm-null"> (</span><span class="cm-variable">a</span><span class="cm-null">,</span><span class="cm-variable">b</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">myfunction3</span><span class="cm-null">(</span><span class="cm-string">'avalue'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>Called myfunction3('avalue','default value')</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 1px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><p>You can, of course, override the default:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [18]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>myfunction3('avalue', 'bvalue')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">myfunction3</span><span class="cm-null">(</span><span class="cm-string">'avalue'</span><span class="cm-null">, </span><span class="cm-string">'bvalue'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>Called myfunction3('avalue','bvalue')</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 5px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h2>Variable arguments</h2>
<p>We can define a function that takes any number of arguments using the <code>*args</code> syntax. The
value of the <code>args</code> parameter below is any <em>positional</em> arguments that remain after
accounting for other arguments:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [26]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 82px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span> print 'Called va_adder(%r, *%r)' % (prompt, args)</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">def</span><span class="cm-null"> </span><span class="cm-variable">va_adder</span><span class="cm-null">(</span><span class="cm-variable">prompt</span><span class="cm-null">, </span><span class="cm-operator">*</span><span class="cm-variable">args</span><span class="cm-null">):</span></pre><pre><span class="cm-null"> </span><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-string">'Called va_adder(%r, *%r)'</span><span class="cm-null"> </span><span class="cm-operator">%</span><span class="cm-null"> (</span><span class="cm-variable">prompt</span><span class="cm-null">, </span><span class="cm-variable">args</span><span class="cm-null">)</span></pre><pre><span class="cm-null"> </span><span class="cm-keyword">return</span><span class="cm-null"> </span><span class="cm-variable">sum</span><span class="cm-null">(</span><span class="cm-variable">args</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">va_adder</span><span class="cm-null">(</span><span class="cm-string">'ThePrompt>'</span><span class="cm-null">, </span><span class="cm-number">1</span><span class="cm-null">,</span><span class="cm-number">2</span><span class="cm-null">,</span><span class="cm-number">3</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>Called va_adder('ThePrompt>', *(1, 2, 3))</pre></div></div><div class="hbox output_area"><div class="prompt output_prompt">Out[26]:</div><div class="box_flex1 output_subarea output_text"><pre>6</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 1px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><p>Likewise, we can call a function with a <code>tuple</code> of arguments using a similar syntax:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [25]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 82px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span> print 'Called normal_function(%r, %r)' % (a,b)</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">def</span><span class="cm-null"> </span><span class="cm-variable">normal_function</span><span class="cm-null">(</span><span class="cm-variable">a</span><span class="cm-null">,</span><span class="cm-variable">b</span><span class="cm-null">):</span></pre><pre><span class="cm-null"> </span><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-string">'Called normal_function(%r, %r)'</span><span class="cm-null"> </span><span class="cm-operator">%</span><span class="cm-null"> (</span><span class="cm-variable">a</span><span class="cm-null">,</span><span class="cm-variable">b</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">argument_tuple</span><span class="cm-null"> = (</span><span class="cm-string">'avalue'</span><span class="cm-null">, </span><span class="cm-string">'bvalue'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">normal_function</span><span class="cm-null">(</span><span class="cm-operator">*</span><span class="cm-variable">argument_tuple</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>Called normal_function('avalue', 'bvalue')</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 2px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><p>If you want to define a function with variable <em>keyword</em> arguments, you can do that as well
with the <code>**kwargs</code> syntax. In this case, the keyword arguments are passed as a <code>dict</code>:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [27]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 65px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>kw_function(a='avalue', b='bvalue')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">def</span><span class="cm-null"> </span><span class="cm-variable">kw_function</span><span class="cm-null">(</span><span class="cm-operator">**</span><span class="cm-variable">kwargs</span><span class="cm-null">):</span></pre><pre><span class="cm-null"> </span><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-variable">kwargs</span></pre><pre><span class="cm-variable">kw_function</span><span class="cm-null">(</span><span class="cm-variable">a</span><span class="cm-null">=</span><span class="cm-string">'avalue'</span><span class="cm-null">, </span><span class="cm-variable">b</span><span class="cm-null">=</span><span class="cm-string">'bvalue'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>{'a': 'avalue', 'b': 'bvalue'}</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 1px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><p>Of course, we can also pass a dictionary as the keyword arguments of a function:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [28]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 47px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>arguments = { 'a': 'avalue', 'b': 'bvalue' }</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">arguments</span><span class="cm-null"> = { </span><span class="cm-string">'a'</span><span class="cm-null">: </span><span class="cm-string">'avalue'</span><span class="cm-null">, </span><span class="cm-string">'b'</span><span class="cm-null">: </span><span class="cm-string">'bvalue'</span><span class="cm-null"> }</span></pre><pre><span class="cm-variable">normal_function</span><span class="cm-null">(</span><span class="cm-operator">**</span><span class="cm-variable">arguments</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>Called normal_function('avalue', 'bvalue')</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 9px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h3>Exercise</h3>
<p>Write a function with the signature <code>def log(format, *args, **kwargs):</code> which prints a
line, formatted according to the format string. Some sample results are below:</p>
<pre><code class="prettyprint"><span class="pun">>>></span><span class="pln"> log</span><span class="pun">(</span><span class="str">'The pair is (%r,%r)'</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">)</span><br><span class="typ">The</span><span class="pln"> pair </span><span class="kwd">is</span><span class="pln"> </span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="lit">2</span><span class="pun">)</span><br><span class="pun">>>></span><span class="pln"> log</span><span class="pun">(</span><span class="str">'The value of a is %(a)r'</span><span class="pun">,</span><span class="pln"> a</span><span class="pun">=</span><span class="str">'foo'</span><span class="pun">)</span><br><span class="typ">The</span><span class="pln"> value of a </span><span class="kwd">is</span><span class="pln"> </span><span class="str">'foo'</span><br></code></pre></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [ ]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span></span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre> </pre></div></div></div></div></div></div></div></div></div><div style="display: none;" class="output vbox"></div></div><div style="height: 30%;" class="end_space"></div></body></html>