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

yipeiwu_com6年前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 + 2 < 5 - 7?" 
 
 
print 3 + 2 < 5 - 7 
 
 
print "What is 3 + 2?", 3 + 2 
print "What is 5 - 7?", 5 - 7 
 
 
print "Oh, that's why it's False." 
 
 
print "How about some more." 
 
 
print "Is it greater?", 5 > -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
What is 5 - 7? -2
Oh, that's why it's False.
How about some more.
Is it greater? True
Is it greatet or equal? True
is it less or equal? False


eg:用数学计算把英寸和磅转化为厘米和千克。
1英寸 = 2.54厘米,1磅 = 0.4536千克

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验证代理ip是否可用的实现方法

在使用爬虫爬取网络数据时,如果长时间对一个网站进行抓取时可能会遇到IP被封的情况,这种情况可以使用代理更换ip来突破服务器封IP的限制。 随手在百度上搜索免费代理IP,可以得到一系列的网...

python使用Matplotlib画条形图

python使用Matplotlib画条形图

本文实例为大家分享了python使用Matplotlib画条形图的具体代码,供大家参考,具体内容如下 数据 中国的四个直辖市分别为北京市、上海市、天津市和重庆市,其2017年上半年的G...

Python定时发送消息的脚本:每天跟你女朋友说晚安

Python定时发送消息的脚本:每天跟你女朋友说晚安

首先 你要有个女朋友 效果: 需要安装几个包 pip install wxpy pip install wechat_sender pip install request...

Python字典推导式将cookie字符串转化为字典解析

cookie: PHPSESSID=et4a33og7nbftv60j3v9m86cro; Hm_lvt_51e3cc975b346e7705d8c255164036b3=156155...

Python简单实现控制电脑的方法

本文实例讲述了Python简单实现控制电脑的方法。分享给大家供大家参考,具体如下: 1、windows 下,CMD的一些命令: dir:列出当前的所有文件 time:打印当前的时间 tr...