Python 数值类型

版权所有,未经许可,禁止转载


章节


Python的数值类型

Python中有三种数值类型:

  • int - 整型
  • float - 浮点型
  • complex - 复数

数值类型变量会在赋值时自动创建:

示例:

a = 6    # int
b = 8.8  # float
c = 6j   # complex

要验证Python 对象的类型,可使用type()函数:

示例:

print(type(a))
print(type(b))
print(type(c))

int

整型,是一个整数,正或负,没有小数,无限长。

示例:

a = 8
b = 4567891234576725432
c = -531431412

print(type(a))
print(type(b))
print(type(c))

float

浮点数,包含一个或多个小数,可以是正数或负数。

示例:

a = 8.88
b = 5.0
c = -4324.432

print(type(a))
print(type(b))
print(type(c))

浮点数也可以是科学计数法表示的数字,用“e”表示10的幂。

示例:

a = 555e6
b = 35E2
c = -678.8e32

print(type(a))
print(type(b))
print(type(c))

complex

复数的虚数部分用“j”表示:

示例:

a = 7+9j
b = 3j
c = -8j

print(type(a))
print(type(b))
print(type(c))
posted @ 2019-07-03 10:06  吴吃辣  阅读(345)  评论(0编辑  收藏  举报