← 返回首页
Python3基础教程(六)
发表时间:2022-03-23 11:37:55
Number

1.Number类型

Python3 支持 int、float、bool、complex(复数)。在Python 3里,只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。像大多数语言一样,数值类型的赋值和计算都是很直观的。内置的 type() 和 isinstance() 函数可以用来查询变量所指的对象类型。

实例:

# -*- coding: utf-8 -*-
# @Time : 2022/3/23 11:22
# @File : number.py
# @Software : PyCharm

class Father:
    pass


class Son(Father):
    pass


a = 100
f = 3.14
flag = True
c = 4 + 3j

print(type(a))
print(type(f))
print(type(flag))
print(type(c))
print("------------------")
print(isinstance(a, int))
print("------------------")
print(type(Father()) == Father)
print(type(Son()) == Son)
print(type(Son()) == Father)

# type()不会认为子类是一种父类类型。
# isinstance()会认为子类是一种父类类型。
print("------------------")
print(isinstance(Son(), Son))
print(isinstance(Son(), Father))
print("------------------")
# Python3 中,bool 是 int 的子类,True 和 False 可以和数字相加, True==1、False==0 会返回 True,但可以通过 is 来判断类型。
print(True == 1)
print(True + 1)
print(False == 0)
print(False + 1)
print("------------------")
#数学运算
print( 5 + 4)
print( 5 - 4)
print( 5 * 4)
print( 5 / 4)
#地板除//永远是整数,并且是向下取整
print( 5 // 4)
print( 5 % 4)

运行结果:

<class 'int'>
<class 'float'>
<class 'bool'>
<class 'complex'>
------------------
True
------------------
True
True
False
------------------
True
True
------------------
True
2
True
1
------------------
9
1
20
1.25
1
1