大家好,给大家分享一下python案例讲解视频,很多人还不知道这一点。下面详细解释一下。现在让我们来看看!
前言
Python 是一种面向对象、解释型、弱类型的脚本语言,它也是一种功能强大而完善的通用型语言。
相比其他编程语言(比如 Java),Python 代码非常简单,上手非常容易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 实例
<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!
2.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 : 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
3.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 : 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
4.Python 二次方程执行以上代码输出结果为:
$ python test.py
输入 a: 1
输入 b: 5
输入 c: 6
结果为 (-3+0j) 和 (-2+0j)
该实例中,我们使用了 cmath (complex math) 模块的 sqrt() 方法 来计算 平方根。
5.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 : 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
6.Python 计算圆的面积
<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
7.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 : 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)
8.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 : 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
9.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 : 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
10.Python 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"># 用户输入数字 </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
零
今天的十个案例就分享到这了,更多案例可以查看下方目录。如需获取可私信我,或评论区留言,我看到后会一 一回复。
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
👉Python学习路线汇总👈
围绕Python所有方向的技术点做的整理,以形成各个领域的知识点汇总,这样就可以按照上面的知识点去找对应的学习资源,保证学得较为全面。
👉Python必备开发工具👈
👉Python学习视频600合集👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
👉实战案例👈
边学边练是最高效的学习方法,这时候可以搞点实战案例来学习。
👉100道Python练习题👈
检查学习结果。
👉面试刷题👈
👉资料领取👈
这份完整版的Python全套学习资料已为大家备好,朋友们如果需要可以微信扫描下方二维码添加输入"领取资料" 可免费领取全套资料【有什么需要协作的还可以随时联系我】朋友圈也会不定时的更新最前言python知识。 文章来源:https://www.toymoban.com/news/detail-848004.html
文章来源地址https://www.toymoban.com/news/detail-848004.html
前言
Python 是一种面向对象、解释型、弱类型的脚本语言,它也是一种功能强大而完善的通用型语言。
相比其他编程语言(比如 Java),Python 代码非常简单,上手非常容易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 实例
<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!
2.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 : 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
3.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 : 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
4.Python 二次方程执行以上代码输出结果为:
$ python test.py
输入 a: 1
输入 b: 5
输入 c: 6
结果为 (-3+0j) 和 (-2+0j)
该实例中,我们使用了 cmath (complex math) 模块的 sqrt() 方法 来计算 平方根。
5.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 : 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
6.Python 计算圆的面积
<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
7.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 : 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)
8.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 : 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
9.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 : 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
10.Python 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"># 用户输入数字 </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
零
今天的十个案例就分享到这了,更多案例可以查看下方目录。如需获取可私信我,或评论区留言,我看到后会一 一回复。
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
👉Python学习路线汇总👈
围绕Python所有方向的技术点做的整理,以形成各个领域的知识点汇总,这样就可以按照上面的知识点去找对应的学习资源,保证学得较为全面。
👉Python必备开发工具👈
👉Python学习视频600合集👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
👉实战案例👈
边学边练是最高效的学习方法,这时候可以搞点实战案例来学习。
👉100道Python练习题👈
检查学习结果。
👉面试刷题👈
👉资料领取👈
这份完整版的Python全套学习资料已为大家备好,朋友们如果需要可以微信扫描下方二维码添加输入"领取资料" 可免费领取全套资料【有什么需要协作的还可以随时联系我】朋友圈也会不定时的更新最前言python知识。
到了这里,关于python案例讲解视频,python简单案例分析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!