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 30Roosters 97Now I will count the eggs:7Is it true that 3 + 2 < 5 - 7?FalseWhat is 3 + 2? 5What is 5 - 7? -2Oh, that's why it's False.How about some more.Is it greater? TrueIs it greatet or equal? Trueis 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