python 查看帮助和变量的强制转换

查看帮助

dir() 函数

查看对象都有哪些属性和方法

用法:把要查询的对象写入()括号中即可


print(dir([]))  (查看列表的方法)

执行:
C:Python27python.exe D:/Python/type-of-data.py
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Process finished with exit code 0

help()

内置函数,查看函数对象模块用途的详细说明

用法:在help() 括号中填写参数即可

查看一个模块的帮助(打开模块的帮助文档)
print(help('sys'))

查看一个数据类型的帮助(返回字符串的方法及详细说明)
print(help(str))

查看具体方法的帮助(在python console中使用)
>>> a = ['a', 'b']
>>> help(a.append)
Help on built-in function append:
append(...)
    L.append(object) -- append object to end

pycharm中Ctrl + 左键

点中要查看帮助方法,按Ctrl + 鼠标左键

其它

type() 查看变量类型

变量的强制转换

变量的类型

int str bool dict {} list [] tuple ()

int(x [,base ])         将x转换为一个整数
long(x [,base ])        将x转换为一个长整数
float(x )               将x转换到一个浮点数
complex(real [,imag ])  创建一个复数
str(x )                 将对象 x 转换为字符串
repr(x )                将对象 x 转换为表达式字符串
eval(str )              用来计算在字符串中的有效Python表达式,并返回一个对象
tuple(s )               将序列 s 转换为一个元组
list(s )                将序列 s 转换为一个列表
chr(x )                 将一个整数转换为一个字符
unichr(x )              将一个整数转换为Unicode字符
ord(x )                 将一个字符转换为它的整数值
hex(x )                 将一个整数转换为一个十六进制字符串
oct(x )                 将一个整数转换为一个八进制字符串

原文地址:https://www.cnblogs.com/lijunjiang2015/p/7732871.html