2.2 数据类型2: Numeric & String

来源:互联网 发布:海量数据排序 编辑:程序博客网 时间:2024/05/21 22:46

2.2 数据类型2: Numeric & String

 

1. Python数据类型

     1.1 总体:numerics, sequences, mappings, classes, instances, and exceptions

     1.2 Numeric Types: int (包含boolean), float, complex

     1.3 int: unlimited length; float: 实现用double in C, 可查看 sys.float_info;             

complex: real(实部) & imaginary(虚部),用z.real 和 z.imag来取两部分

     1.4 具体运算以及法则参见:https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex

     1.5 例子

 

Python数字

数字数据类型用于存储数值。

他们是不可改变的数据类型,这意味着改变数字数据类型会分配一个新的对象。

当你指定一个值时,Number对象就会被创建:

var1 = 1

var2 = 10

您也可以使用del语句删除一些对象的引用。

del语句的语法是:

del var1[,var2[,var3[....,varN]]]]

您可以通过使用del语句删除单个或多个对象的引用。例如:

del var

del var_a, var_b

Python支持四种不同的数字类型:

  • int(有符号整型)
  • long(长整型[也可以代表八进制和十六进制])
  • float(浮点型)
  • complex(复数)

实例

一些数值类型的实例:

int

long

float

complex

10

51924361L

0.0

3.14j

100

-0x19323L

15.20

45.j

-786

0122L

-21.9

9.322e-36j

080

0xDEFABCECBDAECBFBAEl

32.3e+18

.876j

-0490

535633629843L

-90.

-.6545+0J

-0x260

-052318172735L

-32.54e100

3e+26J

0x69

-4721885298529L

70.2E-12

4.53e-7j

  • 长整型也可以使用小写"L",但是还是建议您使用大写"L",避免与数字"1"混淆。Python使用"L"来显示长整型。
  • Python还支持复数,复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型

 

-->

import sys

 

a = 3

b = 4

 

c = 5.66

d = 8.0

 

 

e = complex(c, d)

f = complex(float(a), float(b))

 

print ("a is type" , type(a))

print ("b is type" , type(b))

print ("c is type" , type(c))

print ("d is type" , type(d))

print ("e is type" , type(e))

print ("f is type" , type(f))

 

a is type <class 'int'>

b is type <class 'int'>

c is type <class 'float'>

d is type <class 'float'>

e is type <class 'complex'>

f is type <class 'complex'>

 

print(a + b)

print(d / c)

print (b / a)

print (b // a)

print (e)

print (e + f)

 

7

1.4134275618374559

1.3333333333333333

1

(5.66+8j)

(8.66+12j)

 

 

print ("e's real part is: " , e.real)

print ("e's imaginary part is: " , e.imag)

 

print (sys.float_info)

e's real part is: 5.66

e's imaginary part is: 8.0

 

sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

原创粉丝点击