当前位置:   article > 正文

【Python学习实践教程】10个Python经典项目实战,练手必备_python项目案例

python项目案例

  前言

Python 是一种面向对象、解释型、弱类型的脚本语言,它也是一种功能强大而完善的通用型语言。

相比其他编程语言(比如 Java),Python 代码非常简单,上手非常容易。比如我们要完成某个功能,如果用 Java 需要 100 行代码,但用 Python 可能只需要 20 行代码,这是 Python 具有巨大吸引力的一大特点。

Python 的两大特色是清晰的语法和可扩展性:

  • Python 的语法非常清晰,它甚至不是一种格式自由的语言。例如,它要求 if 语句的下一行必须向右缩进,否则不能通过编译。
  • Python的可扩展性体现为它的模块,Python 具有脚本语言中最丰富和强大的类库(这些类库被形象地称为“batteries included,内置电池”),这些类库覆盖了文件 I/O、GUI、网络编程、数据库访问、文本操作等绝大部分应用场景。

此外,Python 的社区也很发达,即使一些小众的应用场景,Python 往往也有对应的开源模块来提供解决方案。

Python 作为一门解释型的语言,它天生具有跨平台的特征,只要为平台提供了相应的 Python 解释器,Python 就可以在该平台上运行。(解释型语言几乎天然是跨平台的。)

那Python这么火爆,几乎可以说是人人都要学Python,但是学是一回事,用又是另一回事了。理论终将是纸上谈兵,是骡子是马得拉出来溜溜。为了帮助大家更快的掌握Python,我给大家准备了10个Python经典项目实战,附带源码等。快来看看吧

1.Python Hello World 实例

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
  2. <span style="color:#5c6370"># Filename : helloworld.py </span>
  3. <span style="color:#5c6370"># author by : www.runoob.com </span>
  4. <span style="color:#5c6370"># 该实例输出 Hello World! print('Hello World!')</span>
  5. </code></span></span>

执行以上代码输出结果为:

Hello World!

2.Python 数字求和

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
  2. <span style="color:#5c6370"># Filename : test.py </span>
  3. <span style="color:#5c6370"># author by : www.runoob.com </span>
  4. <span style="color:#5c6370"># 用户输入数字 num1 = input('输入第一个数字:')</span>
  5. 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>
  6. <span style="color:#5c6370"># 求和 sum = float(num1) + float(num2) </span>
  7. <span style="color:#5c6370"># 显示计算结果 print('数字 {0} 和 {1} 相加结果为:{2}'.format(num1, num2, sum))</span>
  8. </code></span></span>

执行以上代码输出结果为:

输入第一个数字:1.5
输入第二个数字:2.5 数字 1.5 和 2.5
相加结果为:4.0

3.Python 平方根

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
  2. <span style="color:#5c6370"># Filename : test.py </span>
  3. <span style="color:#5c6370"># author by : www.runoob.com </span>
  4. 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>
  5. <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>
  6. </code></span></span>

执行以上代码输出结果为:

$ python test.py
请输入一个数字:4
4.000 的平方根为 2.000

4.Python 二次方程执行以上代码输出结果为:

$ python test.py
输入 a: 1
输入 b: 5
输入 c: 6
结果为 (-3+0j) 和 (-2+0j)
该实例中,我们使用了 cmath (complex math) 模块的 sqrt() 方法 来计算 平方根。

5.Python 计算三角形的面积

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
  2. <span style="color:#5c6370"># Filename : test.py </span>
  3. <span style="color:#5c6370"># author by : www.runoob.com </span>
  4. 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>
  5. 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>
  6. 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>
  7. <span style="color:#5c6370"># 计算半周长 </span>
  8. 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>
  9. <span style="color:#5c6370"># 计算面积 </span>
  10. 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>
  11. <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>
  12. </code></span></span>

执行以上代码输出结果为:

$ python test.py
输入三角形第一边长: 5
输入三角形第二边长: 6
输入三角形第三边长: 7
三角形面积为 14.70

6.Python 计算圆的面积

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># 定义一个方法来计算圆的面积 </span>
  2. <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>
  3. PI <span style="color:#669900">=</span> <span style="color:#98c379">3.142</span>
  4. <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>
  5. <span style="color:#5c6370"># 调用方法 </span>
  6. <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>
  7. </code></span></span>

以上实例输出结果为:

圆的面积为 78.550000

7.Python 随机数生成

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
  2. <span style="color:#5c6370"># Filename : test.py </span>
  3. <span style="color:#5c6370"># author by : www.runoob.com</span>
  4. <span style="color:#5c6370"># 生成 0 ~ 9 之间的随机数 </span>
  5. <span style="color:#5c6370"># 导入 random(随机数) 模块 </span>
  6. <span style="color:#c678dd">import</span> random
  7. <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>
  8. </code></span></span>

执行以上代码输出结果为:

4

以上实例我们使用了 random 模块的 randint() 函数来生成随机数,你每次 执行后都返回不同的数字(0 到 9),该函数的语法为: random.randint(a,b)

8.Python 摄氏温度转华氏温度

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
  2. <span style="color:#5c6370"># Filename : test.py </span>
  3. <span style="color:#5c6370"># author by : www.runoob.com </span>
  4. <span style="color:#5c6370"># 用户输入摄氏温度 </span>
  5. <span style="color:#5c6370"># 接收用户输入 </span>
  6. 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>
  7. <span style="color:#5c6370"># 计算华氏温度 </span>
  8. 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>
  9. <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>
  10. </code></span></span>

执行以上代码输出结果为:

输入摄氏温度: 38 38.0
摄氏温度转为华氏温度为 100.4

以上实例中,摄氏温度转华氏温度的公式为 celsius * 1.8 = fahrenheit - 32。所以得到以下式子:

celsius = (fahrenheit - 32) / 1.8

9.Python 交换变量

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
  2. <span style="color:#5c6370"># Filename : test.py </span>
  3. <span style="color:#5c6370"># author by : www.runoob.com </span>
  4. <span style="color:#5c6370"># 用户输入 </span>
  5. 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>
  6. 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>
  7. <span style="color:#5c6370"># 创建临时变量,并交换 </span>
  8. temp <span style="color:#669900">=</span> x
  9. x <span style="color:#669900">=</span> y
  10. y <span style="color:#669900">=</span> temp
  11. <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>
  12. <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>
  13. </code></span></span>

执行以上代码输出结果为:

输入 x 值: 2
输入 y 值: 3
交换后 x 的值为: 3
交换后 y 的值为: 2

以上实例中,我们创建了临时变量 temp ,并将 x 的值存储在 temp 变量中, 接着将 y 值赋给 x,最后将 temp 赋值给 y 变量。

不使用临时变量 我们也可以不创建临时变量,用一个非常优雅的方式来交换变量:
x,y = y,x
所以以上实例就可以修改为:

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python">实例
  2. <span style="color:#5c6370"># -*- coding: UTF-8 -*- </span>
  3. <span style="color:#5c6370"># Filename : test.py </span>
  4. <span style="color:#5c6370"># author by : www.runoob.com </span>
  5. <span style="color:#5c6370"># 用户输入</span>
  6. 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>
  7. 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>
  8. <span style="color:#5c6370"># 不使用临时变量 </span>
  9. x<span style="color:#999999">,</span>y <span style="color:#669900">=</span> y<span style="color:#999999">,</span>x
  10. <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>
  11. <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>
  12. </code></span></span>

执行以上代码输出结果为:

输入 x 值: 1
输入 y 值: 2
交换后 x 的值为: 2
交换后 y 的值为: 1

10.Python if 语句

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># Filename : test.py </span>
  2. <span style="color:#5c6370"># author by : www.runoob.com </span>
  3. <span style="color:#5c6370"># 用户输入数字 </span>
  4. 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>
  5. <span style="color:#c678dd">if</span> num <span style="color:#669900">></span> <span style="color:#98c379">0</span><span style="color:#999999">:</span>
  6. <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"正数"</span><span style="color:#999999">)</span>
  7. <span style="color:#c678dd">elif</span> num <span style="color:#669900">==</span> <span style="color:#98c379">0</span><span style="color:#999999">:</span>
  8. <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"零"</span><span style="color:#999999">)</span>
  9. <span style="color:#c678dd">else</span><span style="color:#999999">:</span>
  10. <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"负数"</span><span style="color:#999999">)</span>
  11. </code></span></span>

执行以上代码输出结果为:

输入一个数字: 3
正数

我们也可以使用内嵌 if 语句来实现:

  1. <span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># Filename :test.py </span>
  2. <span style="color:#5c6370"># author by : www.runoob.com </span>
  3. <span style="color:#5c6370"># 内嵌 if 语句 </span>
  4. 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>
  5. <span style="color:#c678dd">if</span> num <span style="color:#669900">>=</span> <span style="color:#98c379">0</span><span style="color:#999999">:</span>
  6. <span style="color:#c678dd">if</span> num <span style="color:#669900">==</span> <span style="color:#98c379">0</span><span style="color:#999999">:</span>
  7. <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"零"</span><span style="color:#999999">)</span>
  8. <span style="color:#c678dd">else</span><span style="color:#999999">:</span>
  9. <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"正数"</span><span style="color:#999999">)</span>
  10. <span style="color:#c678dd">else</span><span style="color:#999999">:</span>
  11. <span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"负数"</span><span style="color:#999999">)</span>
  12. </code></span></span>

执行以上代码输出结果为:

输入一个数字: 0

今天的十个案例就分享到这了,更多案例可以查看下方目录。如需获取可私信我,或评论区留言,我看到后会一 一回复。

温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
6d2182873d474f7cad1e2dfed56f9e6b.png
19bb7eca304e4bbb936b6b2472c66880.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/171790?site

推荐阅读
相关标签