<div class="blogpost-body" id="cnblogs_post_body">
<p><span style="color:#0000ff;"><strong><em><span style="font-family:'courier new', courier;">3.1 Function calls</span></em></strong></span><br><span style="font-family:'courier new', courier;">In the context of programming, a function is a named sequence of statements that performs </span><span style="font-family:'courier new', courier;">a computation. When you define a function, you specify the name and the sequence of </span><span style="font-family:'courier new', courier;">statements. Later, you can “call” the function by name. We have already seen one example </span><span style="font-family:'courier new', courier;">of a function call:</span></p>
<div class="cnblogs_code">
<pre class="blockcode">>>> type(32<span style="color:#000000;">)
</span><type <span style="color:#800000;">'</span><span style="color:#800000;">int</span><span style="color:#800000;">'</span>></pre>
</div>
<p><span style="font-family:'courier new', courier;">The name of the function is <span style="color:#ff0000;">type</span>. The expression in parentheses is called the argument of </span><span style="font-family:'courier new', courier;">the function. The result, for this function, is the type of the argument. </span><span style="font-family:'courier new', courier;">It is common to say that a function “takes” an argument and “returns” a result. The result </span><span style="font-family:'courier new', courier;">is called the return value.</span></p>
<p><span style="color:#0000ff;"><strong><em><span style="font-family:'courier new', courier;">3.2 Type conversion functions</span></em></strong></span><br><span style="font-family:'courier new', courier;">Python provides <span style="color:#ff0000;">built-in functions</span> that convert values from one type to another. The <span style="color:#ff0000;">int </span></span><span style="font-family:'courier new', courier;"><span style="color:#ff0000;">function</span> takes any value and converts it to an integer, if it can, or complains otherwise:</span></p>
<div class="cnblogs_code">
<pre class="blockcode">>>> int(<span style="color:#800000;">'</span><span style="color:#800000;">32</span><span style="color:#800000;">'</span><span style="color:#000000;">)
</span>32
>>> int(<span style="color:#800000;">'</span><span style="color:#800000;">Hello</span><span style="color:#800000;">'</span><span style="color:#000000;">)
ValueError: invalid literal </span><span style="color:#0000ff;">for</span> int(): Hello</pre>
</div>
<p><span style="font-family:'courier new', courier;"><span style="color:#ff0000;">int</span> can convert floating-point values to integers, but <span style="color:#ff0000;">it doesn’t round off; it chops off the </span></span><span style="font-family:'courier new', courier;"><span style="color:#ff0000;">fraction part</span>:</span></p>
<div class="cnblogs_code">
<pre class="blockcode">>>> int(3.99999<span style="color:#000000;">)
</span>3
>>> int(-2.3<span style="color:#000000;">)
</span>-2</pre>
</div>
<p><span style="font-family:'courier new', courier;"><span style="color:#ff0000;">float</span> converts integers and strings to floating-point numbers:</span></p>
<div class="cnblogs_code">
<pre class="blockcode">>>> float(32<span style="color:#000000;">)
</span>32.0
>>> float(<span style="color:#800000;">'</span><span style="color:#800000;">3.14159</span><span style="color:#800000;">'</span><span style="color:#000000;">)
</span>3.14159</pre>
</div>
<p><span style="font-family:'courier new', courier;">Finally, <span style="color:#ff0000;">str</span> converts its argument to a string:</span></p>
<div class="cnblogs_code">
<pre class="blockcode">>>> str(32<span style="color:#000000;">)
</span><span style="color:#800000;">'</span><span style="color:#800000;">32</span><span style="color:#800000;">'</span>
>>> str(3.14159<span style="color:#000000;">)
</span><span style="color:#800000;">'</span><span style="color:#800000;">3.14159</span><span style="color:#800000;">'</span></pre>
</div>
<p><span style="color:#0000ff;"><strong><em><span style="font-family:'courier new', courier;">3.3 Math functions</span></em></strong></span><br><span style="font-family:'courier new', courier;">Python has a math module that provides most of the familiar mathematical functions. A </span><span style="font-family:'courier new', courier;">module is a file that contains a collection of related functions. </span><span style="font-family:'courier new', courier;">Before we can use the module, we have to import it:</span></p>
<div class="cnblogs_code">
<pre class="blockcode">>>> <span style="color:#0000ff;">import</span> math</pre>
</div>
<p><span style="font-family:'courier new', courier;">This statement creates a module object named math. If you print the module object, you </span><span style="font-family:'courier new', courier;">get some information about it:</span></p>
<div class="cnblogs_code">
<pre class="blockcode">>>> <span style="color:#0000ff;">print</span><span style="color:#000000;"> math
</span><module <span style="color:#800000;">'</span><span style="color:#800000 |
|