首页 > 技术文章 > python 常用方法

zhangshan33 2019-07-11 20:18 原文

python 常用方法

type使用方式及作用?

# 方法:print(type(任意数据类型))
# 作用:查看数据类型
print(type(1))
# 输出<class 'int'> 整型

print(type("a"))
# 输出<class 'str'>字符串型

print(type(True))
print(type(False))
# 输出<class 'bool'>布尔值型

print(type([1,2]))
# 输出<class 'list'>列表型

print(type((1,)))
# 输出<class 'tuple'>元组型

print(type({"1":2,"2":"3"}))
# 输出<class 'dict'>字典型

print(type({"1","2"}))
# 输出<class 'set'>集合型

id的使用方式及作用?

# 使用方法:print(id(任意数据类型))
# 作用:获取内存地址
print(id({"1","2"}))
# 输出 一串数字

len求长度

lst = [1,2,3,4,5]
使用方法for I in len(last)

locals查看当前文件的所有

print(locals())
# 输出
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x1064a2f28>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/zhangshan/Desktop/01 自定义模块.py', '__cached__': None, 'test': <module 'test' from '/Users/zhangshan/Desktop//test.py'>}

推荐阅读