2.2 数据类型2: Numeric & String

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

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

 

-->

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)

转载于:https://www.cnblogs.com/szm2019/p/7535635.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值