


Python培训
400-996-5531
计算机本来就是用来进行数学计算的,而且无论是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写的运算实例,大家课下可以照着源码多多练习一下。
免责声明:内容和图片源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。
填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!
Copyright © 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有