Skip to content

Commit

Permalink
new posts python-#2~Huxpro#6
Browse files Browse the repository at this point in the history
  • Loading branch information
mikelyou committed Jan 2, 2020
1 parent 8531ca3 commit c77832c
Show file tree
Hide file tree
Showing 47 changed files with 4,694 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
I"�<h2 id="函数">函数</h2>

<h5 id="定义函数">定义函数</h5>

<p>使用<code class="highlighter-rouge">def</code>关键字来定义函数,通过<code class="highlighter-rouge">return</code>关键字来返回一个值,举个栗子:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="k">def</span> <span class="nf">fac</span><span class="p">(</span><span class="n">num</span><span class="p">):</span> <span class="c1"># 阶乘,实际上Python的math模块中有factorial函数,这里仅做演示
</span> <span class="n">result</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">num</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
<span class="n">result</span> <span class="o">*=</span> <span class="n">n</span>
<span class="k">return</span> <span class="n">result</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<ul>
<li>Python 函数与其他语言的区别在于,在Python中,函数的参数可以有默认值,也支持使用可变参数,所以Python并不需要像其他语言一样支持函数的重载,因为我们在定义一个函数的时候可以让它有多种不同的使用方式(听不懂)</li>
<li>如果在调用函数的时候,没有传入对应参数的值,将使用该参数的默认值(如果有的话)</li>
</ul>

<h2 id="模块">模块</h2>

<p>由于Python没有函数重载的概念,那么后面的定义会覆盖之前的定义,也就意味着两个函数同名函数实际上只有一个是存在的。</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="rouge-code"><pre><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
<span class="k">print</span><span class="p">(</span><span class="s">'hello, world!'</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
<span class="k">print</span><span class="p">(</span><span class="s">'goodbye, world!'</span><span class="p">)</span>

<span class="c1"># 下面的代码会输出什么呢?
</span><span class="n">foo</span><span class="p">()</span> <span class="c1"># goodbye, world!
</span></pre></td></tr></tbody></table></code></pre></div></div>

<blockquote>
<p>整篇除了函数的定义之外都不太懂,看 <a href="https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/06.%E5%87%BD%E6%95%B0%E5%92%8C%E6%A8%A1%E5%9D%97%E7%9A%84%E4%BD%BF%E7%94%A8.md">课件</a></p>
</blockquote>

<hr />
<p><strong>Friendly reminder:</strong> This series are my own study notes of <strong><a href="https://github.com/jackfrued/Python-100-Days">Python-100-Days</a></strong>. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from <a href="https://github.com/jackfrued/Python-100-Days">Python-100-Days</a> will not be noted due to huge workload. <strong>The whole series are not allowed to be reproduced</strong>. View <strong><a href="https://mikelyou.com/2020/01/02/python-learning-00-readme/">python-learning-readme</a></strong>.</p>
:ET
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
I"R<p><strong>Friendly reminder:</strong> This series are my own study notes of <strong><a href="https://github.com/jackfrued/Python-100-Days">Python-100-Days</a></strong>. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from <a href="https://github.com/jackfrued/Python-100-Days">Python-100-Days</a> will not be noted due to huge workload. <strong>The whole series are not allowed to be reproduced</strong>. View <strong><a href="https://mikelyou.com/2020/01/02/python-learning-00-readme/">python-learning-readme</a></strong>.</p>

<h2 id="tools-used">Tools used:</h2>

<ul>
<li>Python 3.8 Win</li>
<li>PyCharm</li>
<li><a href="https://github.com/jackfrued/Python-100-Days">Python-100-Days</a></li>
<li><a href="http://pingce.ayyz.cn:9000/vijos/Index.asp">Ayyz-Vijos</a></li>
<li><a href="https://help.github.com/cn/github/writing-on-github/basic-writing-and-formatting-syntax#ignoring-markdown-formatting">MarkDown help</a></li>
</ul>

<h2 id="catalog">Catalog:</h2>

<ul>
<li><a href="https://mikelyou.com/2020/01/02/python-learning-00-readme/">#00 - Readme</a></li>
<li><a href="https://mikelyou.com/2020/01/02/python-learning-01-notes-variables-operators/">#01 - Notes, Variables and Operators</a></li>
</ul>
:ET
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
I"L<p><strong>Friendly reminder:</strong> This series are my own study notes of <strong><a href="https://github.com/jackfrued/Python-100-Days">Python-100-Days</a></strong>. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from <a href="https://github.com/jackfrued/Python-100-Days">Python-100-Days</a> will not be noted due to huge workload. <strong>The whole series are not allowed to be reproduced</strong>. View <strong><a href="https://mikelyou.com/2020/01/02/python-learning-00-readme/">python-learning-readme</a></strong>.</p>

<h2 id="tools-used">Tools used:</h2>

<ul>
<li>Python 3.8 Win</li>
<li>PyCharm</li>
<li><a href="https://github.com/jackfrued/Python-100-Days">Python-100-Days</a></li>
<li><a href="http://pingce.ayyz.cn:9000/vijos/Index.asp">Ayyz-Vijos</a></li>
<li><a href="https://help.github.com/cn/github/writing-on-github/basic-writing-and-formatting-syntax#ignoring-markdown-formatting">MarkDown help</a></li>
</ul>

<h2 id="catalog">Catalog:</h2>

<ul>
<li><a href="https://mikelyou.com/2020/01/02/python-learning-00-readme/">#00 - Readme</a></li>
<li><a href="https://mikelyou.com/2020/01/02/python-learning-01-note-variable-operator/">#01 - Note, Variable and Operator</a></li>
</ul>
:ET
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
I"'<h4 id="赋值">赋值</h4>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="n">a</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">b</span> <span class="o">=</span> <span class="mi">3</span>
<span class="n">a</span> <span class="o">+=</span> <span class="n">b</span> <span class="c1"># 相当于:a = a + b
</span><span class="n">a</span> <span class="o">*=</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">2</span> <span class="c1"># 相当于:a = a * (a + 2)
</span></pre></td></tr></tbody></table></code></pre></div></div>

<h4 id="输入input">输入<code class="highlighter-rouge">input</code></h4>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="n">a</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">input</span><span class="p">(</span><span class="s">'a = '</span><span class="p">))</span> <span class="c1">#读取为int类型
</span><span class="n">f</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="nb">input</span><span class="p">(</span><span class="s">'请输入华氏温度: '</span><span class="p">))</span> <span class="c1">#显示输入提示
</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">input</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="c1">#读取在同一行的多个数字
</span></pre></td></tr></tbody></table></code></pre></div></div>

<h4 id="输出print">输出<code class="highlighter-rouge">print</code></h4>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span> <span class="c1">#输出结果会以空格隔开
</span><span class="k">print</span><span class="p">(</span><span class="s">'</span><span class="si">%.1</span><span class="s">f'</span> <span class="o">%</span> <span class="n">a</span><span class="p">)</span> <span class="c1">#输出float型变量a,保留一位小数
</span></pre></td></tr></tbody></table></code></pre></div></div>
<blockquote>
<p><a href="https://www.cnblogs.com/fat39/p/7159881.html">Python格式化输出</a></p>
</blockquote>

<ul>
<li>使用<code class="highlighter-rouge">end</code>函数使<code class="highlighter-rouge">print</code>不换行
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="k">print</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="s">" "</span><span class="p">)</span> <span class="c1">#1 2 3 4 5
</span></pre></td></tr></tbody></table></code></pre></div> </div>
</li>
</ul>

<hr />
<p><strong>Friendly reminder:</strong> This series are my own study notes of <strong><a href="https://github.com/jackfrued/Python-100-Days">Python-100-Days</a></strong>. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from <a href="https://github.com/jackfrued/Python-100-Days">Python-100-Days</a> will not be noted due to huge workload. <strong>The whole series are not allowed to be reproduced</strong>. View <strong><a href="https://mikelyou.com/2020/01/02/python-learning-00-readme/">python-learning-readme</a></strong>.</p>
:ET
Loading

0 comments on commit c77832c

Please sign in to comment.