• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

简单讲解Python中的数字类型及基本的数学计算

python 搞代码 4年前 (2022-01-07) 37次浏览 已收录 0个评论

这篇文章主要介绍了Python中的数字类型及基本的数学计算,与其他语言一样,除法相对复杂些,不过本文并未就此深入,需要的朋友可以参考下

Python有四种类型的数字:
1.整型 

 a = 2 print a 

2.长整型 

 b = 123456789 print b 

3.浮点数 

 c = 3.2E2 print c 

4.复数 复数为实数的推广,它使任一多项式都有根。复数当中有个“虚数单位”j,它是-1的一个平方根。任一复数都可表达为x+yj,其中x及y皆为实数,分别称为复数之“实部”和“虚部”。  

 d = (2+3j) print d 

 

计算示例:
每种程序语言都有数学计算方法,数学符号通用,大家都知道。直接上代码吧:

 print "I will now count my chickens:" print "Hens", 25 + 30 / 6 print "Roosters", 100 - 25 * 3 % 4 print "Now I will count the eggs:" print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 print "Is it true that 3 + <a style="color:transparent">来源gao($daima.com搞@代@#码网</a>2 <5 - 7?" print 3 + 2  -2 print "Is it greatet or equal?", 5 >= -2 print "is it less or equal?", 5 <= -2 

运行结果应该是这样的:

 root@he-desktop:~/mystuff# 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 -2 oh, that's why it's false. how about some more. it greater? true greatet or equal? less <pre></div><p><br /><strong>eg:用数学计算把英寸和磅转化为厘米和千克。<br /></strong>1英寸 = 2.54厘米,1磅 = 0.4536千克</p><div class="gaodaimacode"><pre class="prettyprint linenums"> my_height_centimeter = my_height * 2.54 my_weight_kilo = my_weight * 0.4536 print "He's %d centimeters tall." % my_height_centimeter print "He's %d kilos heavy." % my_weight_kilo 

以上就是简单讲解Python中的数字类型及基本的数学计算的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:简单讲解Python中的数字类型及基本的数学计算

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址