Think Python - Chapter 03 - Functions

论坛 期权论坛     
选择匿名的用户   2021-5-23 01:16   108   0
<div class="blogpost-body" id="cnblogs_post_body">
<p><span style="color:#0000ff;"><strong><em><span style="font-family:&#39;courier new&#39;, courier;">3.1 Function calls</span></em></strong></span><br><span style="font-family:&#39;courier new&#39;, courier;">In the context of programming, a function is a named sequence of statements that performs </span><span style="font-family:&#39;courier new&#39;, courier;">a computation. When you define a function, you specify the name and the sequence of </span><span style="font-family:&#39;courier new&#39;, courier;">statements. Later, you can “call” the function by name. We have already seen one example </span><span style="font-family:&#39;courier new&#39;, courier;">of a function call:</span></p>
<div class="cnblogs_code">
  <pre class="blockcode">&gt;&gt;&gt; type(32<span style="color:#000000;">)
</span>&lt;type <span style="color:#800000;">&#39;</span><span style="color:#800000;">int</span><span style="color:#800000;">&#39;</span>&gt;</pre>
</div>
<p><span style="font-family:&#39;courier new&#39;, 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:&#39;courier new&#39;, courier;">the function. The result, for this function, is the type of the argument. </span><span style="font-family:&#39;courier new&#39;, courier;">It is common to say that a function “takes” an argument and “returns” a result. The result </span><span style="font-family:&#39;courier new&#39;, courier;">is called the return value.</span></p>
<p><span style="color:#0000ff;"><strong><em><span style="font-family:&#39;courier new&#39;, courier;">3.2 Type conversion functions</span></em></strong></span><br><span style="font-family:&#39;courier new&#39;, 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:&#39;courier new&#39;, 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">&gt;&gt;&gt; int(<span style="color:#800000;">&#39;</span><span style="color:#800000;">32</span><span style="color:#800000;">&#39;</span><span style="color:#000000;">)
</span>32
&gt;&gt;&gt; int(<span style="color:#800000;">&#39;</span><span style="color:#800000;">Hello</span><span style="color:#800000;">&#39;</span><span style="color:#000000;">)
ValueError: invalid literal </span><span style="color:#0000ff;">for</span> int(): Hello</pre>
</div>
<p><span style="font-family:&#39;courier new&#39;, 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:&#39;courier new&#39;, courier;"><span style="color:#ff0000;">fraction part</span>:</span></p>
<div class="cnblogs_code">
  <pre class="blockcode">&gt;&gt;&gt; int(3.99999<span style="color:#000000;">)
</span>3
&gt;&gt;&gt; int(-2.3<span style="color:#000000;">)
</span>-2</pre>
</div>
<p><span style="font-family:&#39;courier new&#39;, courier;"><span style="color:#ff0000;">float</span> converts integers and strings to floating-point numbers:</span></p>
<div class="cnblogs_code">
  <pre class="blockcode">&gt;&gt;&gt; float(32<span style="color:#000000;">)
</span>32.0
&gt;&gt;&gt; float(<span style="color:#800000;">&#39;</span><span style="color:#800000;">3.14159</span><span style="color:#800000;">&#39;</span><span style="color:#000000;">)
</span>3.14159</pre>
</div>
<p><span style="font-family:&#39;courier new&#39;, courier;">Finally, <span style="color:#ff0000;">str</span> converts its argument to a string:</span></p>
<div class="cnblogs_code">
  <pre class="blockcode">&gt;&gt;&gt; str(32<span style="color:#000000;">)
</span><span style="color:#800000;">&#39;</span><span style="color:#800000;">32</span><span style="color:#800000;">&#39;</span>
&gt;&gt;&gt; str(3.14159<span style="color:#000000;">)
</span><span style="color:#800000;">&#39;</span><span style="color:#800000;">3.14159</span><span style="color:#800000;">&#39;</span></pre>
</div>
<p><span style="color:#0000ff;"><strong><em><span style="font-family:&#39;courier new&#39;, courier;">3.3 Math functions</span></em></strong></span><br><span style="font-family:&#39;courier new&#39;, courier;">Python has a math module that provides most of the familiar mathematical functions. A </span><span style="font-family:&#39;courier new&#39;, courier;">module is a file that contains a collection of related functions. </span><span style="font-family:&#39;courier new&#39;, courier;">Before we can use the module, we have to import it:</span></p>
<div class="cnblogs_code">
  <pre class="blockcode">&gt;&gt;&gt; <span style="color:#0000ff;">import</span> math</pre>
</div>
<p><span style="font-family:&#39;courier new&#39;, courier;">This statement creates a module object named math. If you print the module object, you </span><span style="font-family:&#39;courier new&#39;, courier;">get some information about it:</span></p>
<div class="cnblogs_code">
  <pre class="blockcode">&gt;&gt;&gt; <span style="color:#0000ff;">print</span><span style="color:#000000;"> math
</span>&lt;module <span style="color:#800000;">&#39;</span><span style="color:#800000
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP