赞
踩
Python 是一种面向对象、解释型、弱类型的脚本语言,它也是一种功能强大而完善的通用型语言。
相比其他编程语言(比如 Java),Python 代码非常简单,上手非常容易。比如我们要完成某个功能,如果用 Java 需要 100 行代码,但用 Python 可能只需要 20 行代码,这是 Python 具有巨大吸引力的一大特点。
Python 的两大特色是清晰的语法和可扩展性:
此外,Python 的社区也很发达,即使一些小众的应用场景,Python 往往也有对应的开源模块来提供解决方案。
Python 作为一门解释型的语言,它天生具有跨平台的特征,只要为平台提供了相应的 Python 解释器,Python 就可以在该平台上运行。(解释型语言几乎天然是跨平台的。)
那Python这么火爆,几乎可以说是人人都要学Python,但是学是一回事,用又是另一回事了。理论终将是纸上谈兵,是骡子是马得拉出来溜溜。为了帮助大家更快的掌握Python,我给大家准备了10个Python经典项目实战,附带源码等。快来看看吧
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
- <span style="color:#5c6370"># Filename : helloworld.py </span>
- <span style="color:#5c6370"># author by : www.runoob.com </span>
- <span style="color:#5c6370"># 该实例输出 Hello World! print('Hello World!')</span>
- </code></span></span>
执行以上代码输出结果为:
Hello World!
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
- <span style="color:#5c6370"># Filename : test.py </span>
- <span style="color:#5c6370"># author by : www.runoob.com </span>
- <span style="color:#5c6370"># 用户输入数字 num1 = input('输入第一个数字:')</span>
- num2 <span style="color:#669900">=</span> <span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">'输入第二个数字:'</span><span style="color:#999999">)</span>
- <span style="color:#5c6370"># 求和 sum = float(num1) + float(num2) </span>
- <span style="color:#5c6370"># 显示计算结果 print('数字 {0} 和 {1} 相加结果为:{2}'.format(num1, num2, sum))</span>
- </code></span></span>
执行以上代码输出结果为:
输入第一个数字:1.5
输入第二个数字:2.5 数字 1.5 和 2.5
相加结果为:4.0
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
- <span style="color:#5c6370"># Filename : test.py </span>
- <span style="color:#5c6370"># author by : www.runoob.com </span>
- num <span style="color:#669900">=</span> <span style="color:#669900">float</span><span style="color:#999999">(</span><span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">'请输入一个数字:'</span><span style="color:#999999">)</span><span style="color:#999999">)</span> num_sqrt <span style="color:#669900">=</span> num <span style="color:#669900">**</span> <span style="color:#98c379">0.5</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">' %0.3f 的平方根为 %0.3f'</span><span style="color:#669900">%</span><span style="color:#999999">(</span>num <span style="color:#999999">,</span>num_sqrt<span style="color:#999999">)</span><span style="color:#999999">)</span>
- </code></span></span>
执行以上代码输出结果为:
$ python test.py
请输入一个数字:4
4.000 的平方根为 2.000
$ python test.py
输入 a: 1
输入 b: 5
输入 c: 6
结果为 (-3+0j) 和 (-2+0j)
该实例中,我们使用了 cmath (complex math) 模块的 sqrt() 方法 来计算 平方根。
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
- <span style="color:#5c6370"># Filename : test.py </span>
- <span style="color:#5c6370"># author by : www.runoob.com </span>
- a <span style="color:#669900">=</span> <span style="color:#669900">float</span><span style="color:#999999">(</span><span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">'输入三角形第一边长: '</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
- b <span style="color:#669900">=</span> <span style="color:#669900">float</span><span style="color:#999999">(</span><span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">'输入三角形第二边长: '</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
- c <span style="color:#669900">=</span> <span style="color:#669900">float</span><span style="color:#999999">(</span><span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">'输入三角形第三边长: '</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
- <span style="color:#5c6370"># 计算半周长 </span>
- s <span style="color:#669900">=</span> <span style="color:#999999">(</span>a <span style="color:#669900">+</span> b <span style="color:#669900">+</span> c<span style="color:#999999">)</span> <span style="color:#669900">/</span> <span style="color:#98c379">2</span>
- <span style="color:#5c6370"># 计算面积 </span>
- area <span style="color:#669900">=</span> <span style="color:#999999">(</span>s<span style="color:#669900">*</span><span style="color:#999999">(</span>s<span style="color:#669900">-</span>a<span style="color:#999999">)</span><span style="color:#669900">*</span><span style="color:#999999">(</span>s<span style="color:#669900">-</span>b<span style="color:#999999">)</span><span style="color:#669900">*</span><span style="color:#999999">(</span>s<span style="color:#669900">-</span>c<span style="color:#999999">)</span><span style="color:#999999">)</span> <span style="color:#669900">**</span> <span style="color:#98c379">0.5</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'三角形面积为 %0.2f'</span> <span style="color:#669900">%</span>area<span style="color:#999999">)</span>
- </code></span></span>
执行以上代码输出结果为:
$ python test.py
输入三角形第一边长: 5
输入三角形第二边长: 6
输入三角形第三边长: 7
三角形面积为 14.70
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># 定义一个方法来计算圆的面积 </span>
- <span style="color:#c678dd">def</span> <span style="color:#61aeee">findArea</span><span style="color:#999999">(</span>r<span style="color:#999999">)</span><span style="color:#999999">:</span>
- PI <span style="color:#669900">=</span> <span style="color:#98c379">3.142</span>
- <span style="color:#c678dd">return</span> PI <span style="color:#669900">*</span> <span style="color:#999999">(</span>r<span style="color:#669900">*</span>r<span style="color:#999999">)</span>
- <span style="color:#5c6370"># 调用方法 </span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"圆的面积为 %.6f"</span> <span style="color:#669900">%</span> findArea<span style="color:#999999">(</span><span style="color:#98c379">5</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
- </code></span></span>
以上实例输出结果为:
圆的面积为 78.550000
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
- <span style="color:#5c6370"># Filename : test.py </span>
- <span style="color:#5c6370"># author by : www.runoob.com</span>
- <span style="color:#5c6370"># 生成 0 ~ 9 之间的随机数 </span>
- <span style="color:#5c6370"># 导入 random(随机数) 模块 </span>
- <span style="color:#c678dd">import</span> random
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span>random<span style="color:#999999">.</span>randint<span style="color:#999999">(</span><span style="color:#98c379">0</span><span style="color:#999999">,</span><span style="color:#98c379">9</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
- </code></span></span>
执行以上代码输出结果为:
4
以上实例我们使用了 random 模块的 randint() 函数来生成随机数,你每次 执行后都返回不同的数字(0 到 9),该函数的语法为: random.randint(a,b)
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
- <span style="color:#5c6370"># Filename : test.py </span>
- <span style="color:#5c6370"># author by : www.runoob.com </span>
- <span style="color:#5c6370"># 用户输入摄氏温度 </span>
- <span style="color:#5c6370"># 接收用户输入 </span>
- celsius <span style="color:#669900">=</span> <span style="color:#669900">float</span><span style="color:#999999">(</span><span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">'输入摄氏温度: '</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
- <span style="color:#5c6370"># 计算华氏温度 </span>
- fahrenheit <span style="color:#669900">=</span> <span style="color:#999999">(</span>celsius <span style="color:#669900">*</span> <span style="color:#98c379">1.8</span><span style="color:#999999">)</span> <span style="color:#669900">+</span> <span style="color:#98c379">32</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'%0.1f 摄氏温度转为华氏温度为 %0.1f '</span> <span style="color:#669900">%</span><span style="color:#999999">(</span>celsius<span style="color:#999999">,</span>fahrenheit<span style="color:#999999">)</span><span style="color:#999999">)</span>
- </code></span></span>
执行以上代码输出结果为:
输入摄氏温度: 38 38.0
摄氏温度转为华氏温度为 100.4
以上实例中,摄氏温度转华氏温度的公式为 celsius * 1.8 = fahrenheit - 32。所以得到以下式子:
celsius = (fahrenheit - 32) / 1.8
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
- <span style="color:#5c6370"># Filename : test.py </span>
- <span style="color:#5c6370"># author by : www.runoob.com </span>
- <span style="color:#5c6370"># 用户输入 </span>
- x <span style="color:#669900">=</span> <span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">'输入 x 值: '</span><span style="color:#999999">)</span>
- y <span style="color:#669900">=</span> <span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">'输入 y 值: '</span><span style="color:#999999">)</span>
- <span style="color:#5c6370"># 创建临时变量,并交换 </span>
- temp <span style="color:#669900">=</span> x
- x <span style="color:#669900">=</span> y
- y <span style="color:#669900">=</span> temp
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'交换后 x 的值为: {}'</span><span style="color:#999999">.</span><span style="color:#669900">format</span><span style="color:#999999">(</span>x<span style="color:#999999">)</span><span style="color:#999999">)</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'交换后 y 的值为: {}'</span><span style="color:#999999">.</span><span style="color:#669900">format</span><span style="color:#999999">(</span>y<span style="color:#999999">)</span><span style="color:#999999">)</span>
- </code></span></span>
执行以上代码输出结果为:
输入 x 值: 2
输入 y 值: 3
交换后 x 的值为: 3
交换后 y 的值为: 2
以上实例中,我们创建了临时变量 temp ,并将 x 的值存储在 temp 变量中, 接着将 y 值赋给 x,最后将 temp 赋值给 y 变量。
不使用临时变量 我们也可以不创建临时变量,用一个非常优雅的方式来交换变量:
x,y = y,x
所以以上实例就可以修改为:
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python">实例
- <span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
- <span style="color:#5c6370"># Filename : test.py </span>
- <span style="color:#5c6370"># author by : www.runoob.com </span>
- <span style="color:#5c6370"># 用户输入</span>
- x <span style="color:#669900">=</span> <span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">'输入 x 值: '</span><span style="color:#999999">)</span>
- y <span style="color:#669900">=</span> <span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">'输入 y 值: '</span><span style="color:#999999">)</span>
- <span style="color:#5c6370"># 不使用临时变量 </span>
- x<span style="color:#999999">,</span>y <span style="color:#669900">=</span> y<span style="color:#999999">,</span>x
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'交换后 x 的值为: {}'</span><span style="color:#999999">.</span><span style="color:#669900">format</span><span style="color:#999999">(</span>x<span style="color:#999999">)</span><span style="color:#999999">)</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'交换后 y 的值为: {}'</span><span style="color:#999999">.</span><span style="color:#669900">format</span><span style="color:#999999">(</span>y<span style="color:#999999">)</span><span style="color:#999999">)</span>
- </code></span></span>
执行以上代码输出结果为:
输入 x 值: 1
输入 y 值: 2
交换后 x 的值为: 2
交换后 y 的值为: 1
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># Filename : test.py </span>
- <span style="color:#5c6370"># author by : www.runoob.com </span>
- <span style="color:#5c6370"># 用户输入数字 </span>
- num <span style="color:#669900">=</span> <span style="color:#669900">float</span><span style="color:#999999">(</span><span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">"输入一个数字: "</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
- <span style="color:#c678dd">if</span> num <span style="color:#669900">></span> <span style="color:#98c379">0</span><span style="color:#999999">:</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"正数"</span><span style="color:#999999">)</span>
- <span style="color:#c678dd">elif</span> num <span style="color:#669900">==</span> <span style="color:#98c379">0</span><span style="color:#999999">:</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"零"</span><span style="color:#999999">)</span>
- <span style="color:#c678dd">else</span><span style="color:#999999">:</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"负数"</span><span style="color:#999999">)</span>
- </code></span></span>
执行以上代码输出结果为:
输入一个数字: 3
正数
我们也可以使用内嵌 if 语句来实现:
- <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># Filename :test.py </span>
- <span style="color:#5c6370"># author by : www.runoob.com </span>
- <span style="color:#5c6370"># 内嵌 if 语句 </span>
- num <span style="color:#669900">=</span> <span style="color:#669900">float</span><span style="color:#999999">(</span><span style="color:#669900">input</span><span style="color:#999999">(</span><span style="color:#669900">"输入一个数字: "</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
- <span style="color:#c678dd">if</span> num <span style="color:#669900">>=</span> <span style="color:#98c379">0</span><span style="color:#999999">:</span>
- <span style="color:#c678dd">if</span> num <span style="color:#669900">==</span> <span style="color:#98c379">0</span><span style="color:#999999">:</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"零"</span><span style="color:#999999">)</span>
- <span style="color:#c678dd">else</span><span style="color:#999999">:</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"正数"</span><span style="color:#999999">)</span>
- <span style="color:#c678dd">else</span><span style="color:#999999">:</span>
- <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"负数"</span><span style="color:#999999">)</span>
- </code></span></span>
执行以上代码输出结果为:
输入一个数字: 0
零
今天的十个案例就分享到这了,更多案例可以查看下方目录。如需获取可私信我,或评论区留言,我看到后会一 一回复。
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。