Python数据类型-其他


1、type

查看对象的类,或对象所具备的功能

python 的所有数据类型都是类,可以通过 type() 查看该变量的数据类型:

>>> n=1 
>>> type(n) 
<type 'int'> 
>>> n="runoob" 
>>> type(n) 
<type 'str'>
tmep = "alex"
t = type(temp)
print(t)  # 字符串类型


2、dir

temp = "alex"
b = dir(temp)
print(b)
#显示出当前数据类型的所有方法
‘’‘
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
’‘’


3、help

temp = "alex"
print(help(type(temp)))
# 显示出当前数据类型的所有方法的使用介绍
'''
截取部分
class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str
 |
 |  Create a new string object from the given object. If encoding or
 |  errors is specified, then the object must expose a data buffer
 |  that will be decoded using the given encoding and error handler.
 |  Otherwise, returns the result of object.__str__() (if defined)
 |  or repr(object).
 |  encoding defaults to sys.getdefaultencoding().
 |  errors defaults to 'strict'.
 |
 |  Methods defined here:
 |
 |  __add__(self, value, /)
 |      Return self+value.
 |
 |  __contains__(self, key, /)
 |      Return key in self.
 |
 |  __eq__(self, value, /)
 |      Return self==value.
略
'''

4、直接点击

temp = "alex"
temp.upper()
# 在 IDE(pycharm) 里面通过 ctrl+左键,点击 upper,可以跳转到源码里面该方法的详细说明



   

此外还可以用 isinstance 来判断:
isinstance是Python中的一个内建函数。是用来判断一个对象的变量类型。

例1:
a = 111
print(isinstance(a, int))  # return True


例2:
class A:
    pass


class B(A):
    pass

print(type(A()) == A)          # returns True

print(isinstance(B(), A))      # returns True

print(type(B()) == A)          # returns False

print(isinstance(A(), A))      # returns True

isinstance 和 type 的区别在于:
      •  type()不会认为子类是一种父类类型。
      •  isinstance()会认为子类是一种父类类型。


5、布尔值:bool

在Python中,可以直接用True、False表示布尔值(请注意大小写),也可以通过布尔运算计算出来:

>>> True 

True 

>>> False 

False 

>>> 3 > 2 

True 

>>> 3 > 5 

False


布尔值可以用and、or和not运算。

and运算是与运算,只有所有都为True,and运算结果才是True:

>>> True and True 

True 

>>> True and False 

False 

>>> False and False 

False 

>>> 5 > 3 and 3 > 1 

True


or运算是或运算,只要其中有一个为True,or运算结果就是True:

>>> True or True 

True 

>>> True or False 

True 

>>> False or False 

False 

>>> 5 > 3 or 1 > 3 

True

not运算是非运算,它是一个单目运算符,把True变成False,False变成True:

>>> not True 

False 

>>> not False 

True 

>>> not 1 > 2 

True

布尔值经常用在条件判断中,比如:

if age >= 18: 

    print('adult') 

else: 

    print('teenager')


6、空值


空值是Python里一个特殊的值,用None表示。None不能理解为0,因为0是有意义的,而None是一个特殊的空值。
此外,Python还提供了列表、字典等多种数据类型,还允许创建自定义数据类型
以下情况都为False

     None
     [] 空列表
     {} 空字典
     () 空元组,空集合
     '' 空字符串
     0
原文地址:https://www.cnblogs.com/tootooman/p/8955547.html