零基础Python教程-如何用Python进行数学运算

今天和大家分享一下零基础Python教程—如何用Python进行数学运算,计算机本来就是用来进行数学计算的,而且无论是C语言还Python,它都会被转换成二进制数字,来让电脑识别,所以今天我们呢就学习一下如何用Python进行数学运算。

要学习数学计算然少不了运算符了,我们这就先来认识一下Python中的运算符。

名字如下:

• + plus 加号 • – minus 减号 • / slash 斜杠 • * asterisk 星号 • % percent 百分号 • < less-than 小于号 • > greater-than 大于号 • <= less-than-equal 小于等于号 • >= greater-than-equal 大于等于

有没有注意到以上只是些符号,没有运算操作呢?写完下面的练习代码后,再回到上面的列表,写出每个符号的作用。例如 + 是用来做加法运算的。

1 print "I will now count my chickens:" 
2
3 print "Hens", 25 + 30 / 6
4 print "Roosters", 100 - 25 * 3 % 4
5
6 print "Now I will count the eggs:"
7
8 print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
9
10 print "Is it true that 3 + 2 < 5 - 7?"
11
12 print 3 + 2 < 5 - 7
13
14 print "What is 3 + 2?", 3 + 2
15 print "What is 5 - 7?", 5 - 7
16
17 print "Oh, that's why it's False."
18
19 print "How about some more."
20
21 print "Is it greater?", 5 > -2
22 print "Is it greater or equal?", 5 >= -2

23 print "Is it less or equal?", 5 <= -2

通过上面代码的运行,你将得到下面的结果,自己动手操作一下是不是正确呢?

$ Python ex3.py 
I will now count my chickens:
Hens 30 Roosters 97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5 What is 5 - 7? -2
Oh, that's why it's False. How about some more.
Is it greater? True Is it greater or equal?
True Is it less or equal? False
$

以上就是一个用Python写的运算实例,大家课下可以照着源码多多练习一下。

好啦,今天的分享到这里就结束了,希望大家能够持续关注马哥教育的官网,每天都会有大量优质的内容与大家分享欧!

声明:文章来源于网络,侵删!

相关新闻

历经多年发展,已成为国内好评如潮的Linux云计算运维、SRE、Devops、网络安全、云原生、Go、Python开发专业人才培训机构!